Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added v-model support #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/vue2/src/hcaptcha.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import {loadApiEndpointIfNotAlready} from './hcaptcha-script';

export default {
name: 'VueHcaptcha',
model: {
prop: 'value',
event: 'input',
},
props: {
value: {
type: String,
default: undefined,
},
sitekey: {
type: String,
required: true
Expand Down Expand Up @@ -150,13 +158,16 @@ export default {
const token = this.hcaptcha.getResponse(this.widgetId);
const eKey = this.hcaptcha.getRespKey(this.widgetId);
this.$emit('verify', token, eKey);
this.$emit('input', token);
Copy link
Contributor

@DSergiu DSergiu Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v-model would allow 2way binding which I believe would not make sense for the component.

One solution could be to listen to the verify and expire events and bind the token to a local data in your parent component. e.g.

<h-captcha
  ...
  @verify="(token) => {hCaptchaToken = token}"
  @expire="hCaptchaToken =null"
/>

// use hCaptchaToken for "required" in "vee-validate"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the component doesn't accept the token as input, I think using v-model would make it simpler to use. Another alternative that would solve the same problem would be to use token.sync instead of v-model. Thus, it would not be necessary to create event-listeners and callbacks in the component call

},
onExpired() {
this.$emit('expired');
this.$emit('input', null);
},
onChallengeExpired() {
// vue3 will transform this `camelCase` event name into `kebab-case`
this.$emit('challengeExpired');
this.$emit('input', null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a challenge expires the token status does not change.

},
onOpen() {
this.$emit('opened');
Expand Down