Skip to content

Commit

Permalink
feat(VFormSelect): refactor underlying logic to support uncontrolled …
Browse files Browse the repository at this point in the history
…input state
  • Loading branch information
gretchelin authored and gravitano committed Jan 10, 2023
1 parent f601cab commit 35da005
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/forms/src/form-select/VFormSelect.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import {toRefs, PropType, watch, computed} from 'vue';
import {toRefs, PropType, watch, computed, ref} from 'vue';
import {useField} from 'vee-validate';
import type {VFormSelectItem} from './types';
Expand Down Expand Up @@ -89,6 +89,7 @@ const {
rules,
validationMode,
} = toRefs(props);
const uncontrolledValue = ref();
const isEagerValidation = computed(() => {
return validationMode.value === 'eager';
Expand All @@ -99,15 +100,20 @@ const message = computed(() => {
});
const {
value: inputValue,
value: vvValue,
errorMessage,
validate,
setValue,
} = useField(name, rules, {
initialValue: value.value || modelValue.value,
validateOnValueUpdate: !isEagerValidation.value,
});
watch(inputValue, (val) => {
watch(uncontrolledValue, (val) => {
if (name?.value) {
setValue(val);
}
emit('update:modelValue', val);
if (errorMessage.value && isEagerValidation.value) {
Expand All @@ -116,7 +122,11 @@ watch(inputValue, (val) => {
});
watch(modelValue, (val) => {
inputValue.value = val;
uncontrolledValue.value = val;
});
watch(vvValue, (val) => {
uncontrolledValue.value = val;
});
const getValue = (option: string | Record<string, any>) => {
Expand Down Expand Up @@ -149,7 +159,7 @@ const handleBlur = () => {
{{ label }}
</label>
<select
v-model="inputValue"
v-model="uncontrolledValue"
@blur="handleBlur"
class="v-input-control"
:disabled="disabled"
Expand Down

0 comments on commit 35da005

Please sign in to comment.