Skip to content

Commit

Permalink
fix: respect model modifiers when emitting the value closes #4333
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jun 28, 2023
1 parent 81125a0 commit c3698f0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-peaches-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vee-validate': patch
---

fix: respect model modifiers when emitting the value closes #4333
6 changes: 4 additions & 2 deletions packages/vee-validate/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ function _useField<TValue = unknown>(
validateValidStateOnly();
}

const vm = getCurrentInstance();

function setValue(newValue: TValue, shouldValidate = true) {
value.value = newValue;
value.value = vm && syncVModel ? applyModelModifiers<TValue>(newValue, vm.props.modelModifiers) : newValue;
const validateFn = shouldValidate ? validateWithStateMutation : validateValidStateOnly;
validateFn();
}
Expand Down Expand Up @@ -583,7 +585,7 @@ function useVModel<TValue = unknown>({ prop, value, handleChange }: ModelOpts<TV
}

const newValue = (propValue as any) === IS_ABSENT ? undefined : propValue;
if (isEqual(newValue, applyModelModifiers(value.value, vm.props.modelModifiers))) {
if (isEqual(newValue, value.value)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vee-validate/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ export function debounceAsync<TFunction extends (...args: any) => Promise<any>,
};
}

export function applyModelModifiers(value: unknown, modifiers: unknown) {
export function applyModelModifiers<TValue = unknown>(value: TValue, modifiers: unknown): TValue {
if (!isObject(modifiers)) {
return value;
}

if (modifiers.number) {
return toNumber(value as string);
return toNumber(value as string) as TValue;
}

return value;
Expand Down
45 changes: 45 additions & 0 deletions packages/vee-validate/tests/useField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,51 @@ describe('useField()', () => {
expect(input?.value).toBe('test');
});

// #4333
test('should emit modified values with model modifiers being applied as a prop', async () => {
const model = ref('');
const InputComponent = defineComponent({
props: {
modelValue: String,
modelModifiers: null,
},
setup() {
const { value, errorMessage } = useField('field', undefined, { syncVModel: true });

return {
value,
errorMessage,
};
},
template: `
<input v-model="value" />
`,
});

const onModelUpdated = vi.fn();

mountWithHoc({
components: {
InputComponent,
},
setup() {
return {
onModelUpdated,
model,
};
},
template: `
<InputComponent :model-value="model" @update:model-value="onModelUpdated" :model-modifiers="{ number: true }" />
`,
});

const input = document.querySelector('input');

setValue(input as any, '123');
await flushPromises();
expect(onModelUpdated).toHaveBeenLastCalledWith(123);
});

test('can disable model events', async () => {
const model = ref('');
const InputComponent = defineComponent({
Expand Down

0 comments on commit c3698f0

Please sign in to comment.