Skip to content

Commit

Permalink
fix: component blur event and respect model update config closes #4346
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jul 3, 2023
1 parent 510a7a9 commit 6a1dc9b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-planes-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vee-validate': patch
---

fix: component blur event and respect model update config closes #4346
7 changes: 2 additions & 5 deletions packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,11 +872,8 @@ export function useForm<
}

function onUpdateModelValue(value: TValue) {
setFieldValue(pathState.path as Path<TValues>, value as PathValue<TValues, TPath>);
const validateOnModelUpdate = evalConfig().validateOnModelUpdate ?? getConfig().validateOnModelUpdate;
if (validateOnModelUpdate) {
validateField(pathState.path as Path<TValues>);
}
setFieldValue(pathState.path as Path<TValues>, value as PathValue<TValues, TPath>, validateOnModelUpdate);
}

const props = computed(() => {
Expand All @@ -894,13 +891,13 @@ export function useForm<

const model = config?.model || 'modelValue';
const base = {
onBlur,
[model]: pathState.value,
[`onUpdate:${model}`]: onUpdateModelValue,
};

if (config?.mapProps) {
return {
onBlur,
...base,
...config.mapProps(omit(pathState, PRIVATE_PATH_STATE_KEYS)),
} as BaseComponentBinds<TValue> & TExtras;
Expand Down
2 changes: 1 addition & 1 deletion packages/vee-validate/tests/helpers/ModelComp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const ModelComp = {
props: ['modelValue', 'name', 'test'],
emits: ['blur', 'update:modelValue'],
inheritAttrs: false,
template: `<input type="text" :name="name" :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" @blur="$emit('blur')">
template: `<input type="text" :name="name" :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" @blur="$emit('blur');console.log(modelValue, '____' + modelValue + '____' )">
<div v-if="test">{{ test }}</div>`,
};

Expand Down
11 changes: 8 additions & 3 deletions packages/vee-validate/tests/useForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ describe('useForm()', () => {
}),
});

const field = defineComponentBinds('name', { validateOnModelUpdate: true });
const field = defineComponentBinds('name', { validateOnModelUpdate: false });

return { field, values, errors };
},
Expand All @@ -854,10 +854,15 @@ describe('useForm()', () => {
await flushPromises();
const errorEl = document.getElementById('errors');
const valuesEl = document.getElementById('values');
setValue(document.querySelector('input') as any, '');
const input = document.querySelector('input') as HTMLInputElement;
setValue(input, '');
await flushPromises();
expect(errorEl?.textContent).toBe('');
dispatchEvent(input, 'blur');
await flushPromises();
expect(errorEl?.textContent).toBe('name is a required field');
setValue(document.querySelector('input') as any, '123');
setValue(input, '123');
dispatchEvent(input, 'blur');
await flushPromises();
expect(errorEl?.textContent).toBe('');
expect(valuesEl?.textContent).toBe('123');
Expand Down

0 comments on commit 6a1dc9b

Please sign in to comment.