From 954339f95a2e0bfdb73dd9dad9733b39f7d9cf9c Mon Sep 17 00:00:00 2001 From: "Gretta M.R" Date: Wed, 11 Jan 2023 16:31:19 +0700 Subject: [PATCH] fix(VSelect): fix unable to set initialValue --- packages/select/src/VSelect.stories.ts | 243 +++++++++++++++++-------- packages/select/src/VSelect.vue | 128 +++++++------ 2 files changed, 244 insertions(+), 127 deletions(-) diff --git a/packages/select/src/VSelect.stories.ts b/packages/select/src/VSelect.stories.ts index f31b18ecb..cc6c24765 100644 --- a/packages/select/src/VSelect.stories.ts +++ b/packages/select/src/VSelect.stories.ts @@ -36,8 +36,8 @@ const Template: Story = (args) => ({ return {args, value}; }, template: ` - -
Model Value: {{ value }}
+ +
Model Value: {{ value }}
`, }); @@ -156,9 +156,9 @@ export const Sizes: Story = (args) => ({ return {args}; }, template: ` - - - + + + `, }); @@ -204,27 +204,27 @@ export const Validation: Story<{}> = () => ({ }, template: `
- - -
- Submit - Reset -
-
{{ {values} }}
+ + +
+ Submit + Reset +
+
{{ {values} }}
-`, + `, }); export const TestInputState: Story<{}> = (args) => ({ @@ -235,11 +235,18 @@ export const TestInputState: Story<{}> = (args) => ({ gender: string().required().nullable().label('Gender'), }); - console.log({args}) - const modelValue = ref(''); - const {handleSubmit, resetForm, values} = args.useForm ? useForm({ + const modelValue = ref(args.setupWithInitialValue ? 'and' : undefined); + const modelValue2 = ref(args.setupWithInitialValue ? 'female' : undefined); + const initialValues = ref({ + gender: args.setupWithInitialValue ? 'unw' : undefined, + gender2: args.setupWithInitialValue ? 'male' : undefined, + init: 'unw', + }); + + const {handleSubmit, resetForm, values, meta} = args.useForm ? useForm({ + initialValues, validationSchema: schema, - }) : {handleSubmit: (cb: any) => null, resetForm: () => null, values: {}}; + }) : {handleSubmit: (cb: any) => null, resetForm: () => null, values: {}, meta: {}}; const onSubmit = handleSubmit((values: any) => { alert(JSON.stringify(values)); @@ -254,16 +261,17 @@ export const TestInputState: Story<{}> = (args) => ({ text: 'Female', value: 'female', }, - ]); - - const genres = ref([ { - text: 'Pop', - value: 'pop', + text: 'Androgynous', + value: 'and', }, { - text: 'Rock', - value: 'rock', + text: 'Gender Fluid', + value: 'gfl', + }, + { + text: 'Not willing to specify', + value: 'unw', }, ]); @@ -276,63 +284,156 @@ export const TestInputState: Story<{}> = (args) => ({ alert(val); }; - return {args, onSubmit, resetForm, values, genders, genres, modelValue, uncontrolledInput, onChange}; + const resetVVForm = () => { + if (!args.useForm) { + alert('Story is not set up with Vee Validate Form. set `useForm` control to true to try this action.'); + } + + initialValues.value = { + gender: 'gfl', + gender2: 'female', + init: 'male', + }; + + resetForm(); + }; + + return { + args, + onSubmit, + resetForm, + values, + genders, + modelValue, + modelValue2, + uncontrolledInput, + onChange, + resetVVForm, + meta, + }; }, template: ` -
-

{{args.useForm ? 'with' : 'without'}} VeeValidate Form

+ +

{{ args.useForm ? 'with' : 'without' }} VeeValidate Form

-
+ + +
-
When used without vee validate, should not change "Vmodel" value or any other value unless +
When used without vee validate, should not change "Vmodel" value or any other value unless explicitly implemented
- - -
+
-
Should update form values under "gender" key AND "modelValue"
+
Should update form values under "modelValue" only
+
+ +
Should update form values under "gender2" key (with vee validate) AND "modelValue2"
+
-
+
-
Should not change any value unless explicitly implemented
+
Should not change any value unless explicitly implemented
-
- Submit - Reset +
+ +
+ CAUTION! Setting initialValue in VeeValidate won't affect inputs that are defined WITHOUT name + prop being defined! +
For unnamed input, use modelValue or value prop instead +
+ +
+
+ +
+ +
+ +
+ +
+ +
- -
{{ {values, modelValue} }}
+ +
+ Submit + Reset +
+ +
{{ {
+      values,
+      modelValue,
+      modelValue2,
+      initialValue: meta?.initialValues
+    } }}
`, }); TestInputState.args = { useForm: false, + setupWithInitialValue: false, + searchable: false, + clearable: false, }; \ No newline at end of file diff --git a/packages/select/src/VSelect.vue b/packages/select/src/VSelect.vue index bf0fba9dd..73850560d 100644 --- a/packages/select/src/VSelect.vue +++ b/packages/select/src/VSelect.vue @@ -1,4 +1,4 @@ - - +