Skip to content

Commit 3e4a7c1

Browse files
committed
feat: make accept the model propName
1 parent d6b3d91 commit 3e4a7c1

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

.changeset/loud-frogs-explode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
feat(dx): make `syncVModel` accept the model propName

packages/vee-validate/src/useField.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,21 @@ export interface FieldOptions<TValue = unknown> {
5555
validateOnMount?: boolean;
5656
bails?: boolean;
5757
type?: InputType;
58+
/**
59+
* @deprecated Use `checkedValue` instead.
60+
*/
5861
valueProp?: MaybeRef<TValue>;
5962
checkedValue?: MaybeRef<TValue>;
6063
uncheckedValue?: MaybeRef<TValue>;
6164
label?: MaybeRef<string | undefined>;
6265
controlled?: boolean;
6366
standalone?: boolean;
6467
keepValueOnUnmount?: MaybeRef<boolean | undefined>;
68+
/**
69+
* @deprecated Pass the model prop name to `syncVModel` instead.
70+
*/
6571
modelPropName?: string;
66-
syncVModel?: boolean;
72+
syncVModel?: boolean | string;
6773
form?: FormContext;
6874
}
6975

@@ -107,7 +113,6 @@ function _useField<TValue = unknown>(
107113
uncheckedValue,
108114
controlled,
109115
keepValueOnUnmount,
110-
modelPropName,
111116
syncVModel,
112117
form: controlForm,
113118
} = normalizeOptions(opts);
@@ -148,7 +153,7 @@ function _useField<TValue = unknown>(
148153
const errorMessage = computed(() => errors.value[0]);
149154

150155
if (syncVModel) {
151-
useVModel({ value, prop: modelPropName, handleChange });
156+
useVModel({ value, prop: syncVModel, handleChange });
152157
}
153158

154159
/**
@@ -451,15 +456,15 @@ function normalizeOptions<TValue>(opts: Partial<FieldOptions<TValue>> | undefine
451456
label: undefined,
452457
validateOnValueUpdate: true,
453458
keepValueOnUnmount: undefined,
454-
modelPropName: 'modelValue',
455459
syncVModel: false,
456460
controlled: true,
457461
});
458462

459-
const isVModelSynced = opts?.syncVModel ?? true;
463+
const isVModelSynced = !!opts?.syncVModel;
464+
const modelPropName = typeof opts?.syncVModel === 'string' ? opts.syncVModel : opts?.modelPropName || 'modelValue';
460465
const initialValue =
461466
isVModelSynced && !('initialValue' in (opts || {}))
462-
? getCurrentModelValue(getCurrentInstance(), opts?.modelPropName || 'modelValue')
467+
? getCurrentModelValue(getCurrentInstance(), modelPropName)
463468
: opts?.initialValue;
464469

465470
if (!opts) {
@@ -469,13 +474,15 @@ function normalizeOptions<TValue>(opts: Partial<FieldOptions<TValue>> | undefine
469474
// TODO: Deprecate this in next major release
470475
const checkedValue = 'valueProp' in opts ? opts.valueProp : opts.checkedValue;
471476
const controlled = 'standalone' in opts ? !opts.standalone : opts.controlled;
477+
const syncVModel = opts?.modelPropName || opts?.syncVModel || false;
472478

473479
return {
474480
...defaults(),
475481
...(opts || {}),
476482
initialValue,
477483
controlled: controlled ?? true,
478484
checkedValue,
485+
syncVModel,
479486
} as FieldOptions<TValue>;
480487
}
481488

@@ -537,22 +544,22 @@ function useFieldWithChecked<TValue = unknown>(
537544
}
538545

539546
interface ModelOpts<TValue> {
540-
prop?: string;
547+
prop: string | boolean;
541548
value: Ref<TValue>;
542549
handleChange: FieldContext['handleChange'];
543550
}
544551

545552
function useVModel<TValue = unknown>({ prop, value, handleChange }: ModelOpts<TValue>) {
546553
const vm = getCurrentInstance();
547554
/* istanbul ignore next */
548-
if (!vm) {
555+
if (!vm || !prop) {
549556
if (__DEV__) {
550557
console.warn('Failed to setup model events because `useField` was not called in setup.');
551558
}
552559
return;
553560
}
554561

555-
const propName = prop || 'modelValue';
562+
const propName = typeof prop === 'string' ? prop : 'modelValue';
556563
const emitName = `update:${propName}`;
557564

558565
// Component doesn't have a model prop setup (must be defined on the props)

packages/vee-validate/tests/useField.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ describe('useField()', () => {
604604
modelValue: String,
605605
},
606606
setup() {
607-
const { value, errorMessage } = useField('field');
607+
const { value, errorMessage } = useField('field', undefined, { syncVModel: true });
608608

609609
return {
610610
value,

0 commit comments

Comments
 (0)