Skip to content

Commit

Permalink
fix: pass down listeners to the input node closes #3048
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Nov 25, 2020
1 parent 8efbde4 commit 2526a63
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
32 changes: 17 additions & 15 deletions packages/vee-validate/src/Field.ts
Expand Up @@ -94,26 +94,28 @@ export const Field = defineComponent({
: handleInput;

const { validateOnInput, validateOnChange, validateOnBlur, validateOnModelUpdate } = getConfig();
const baseOnBlur = [handleBlur, ctx.attrs.onBlur, validateOnBlur ? validateField : undefined].filter(Boolean);
const baseOnInput = [
onInputHandler,
valueTick,
validateOnInput ? onChangeHandler : undefined,
ctx.attrs.onInput,
].filter(Boolean);
const baseOnChange = [
onInputHandler,
valueTick,
validateOnChange ? onChangeHandler : undefined,
ctx.attrs.onChange,
].filter(Boolean);

const makeSlotProps = () => {
const fieldProps: Record<string, any> = {
name: props.name,
onBlur: [handleBlur],
onInput: [onInputHandler, valueTick],
onChange: [onInputHandler, valueTick],
onBlur: baseOnBlur,
onInput: baseOnInput,
onChange: baseOnChange,
};

if (validateOnInput) {
fieldProps.onInput.push(onChangeHandler);
}

if (validateOnChange) {
fieldProps.onChange.push(onChangeHandler);
}

if (validateOnBlur) {
fieldProps.onBlur.push(validateField);
}

if (validateOnModelUpdate) {
fieldProps['onUpdate:modelValue'] = [onChangeHandler, valueTick];
}
Expand Down
20 changes: 20 additions & 0 deletions packages/vee-validate/tests/Field.spec.ts
Expand Up @@ -677,4 +677,24 @@ describe('<Field />', () => {
await flushPromises();
expect(span?.textContent).toBe('true');
});

// #3048
test('proxies native listeners', async () => {
const onBlur = jest.fn();
mountWithHoc({
setup() {
return {
onBlur,
};
},
template: `
<Field name="field" @blur="onBlur" />
`,
});

await flushPromises();
const input = document.querySelector('input');
dispatchEvent(input as HTMLInputElement, 'blur');
expect(onBlur).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 2526a63

Please sign in to comment.