Skip to content

Commit

Permalink
fix: touched state pathless form
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Aug 3, 2024
1 parent 325c30f commit fc085eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/form/useFormField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function useFormField<TValue = unknown>(opts?: Partial<FormFieldOptions<T
const getPath = () => toValue(opts?.path);
const { fieldValue, pathlessValue, setValue } = useFieldValue(getPath, form, opts?.initialValue);
const { isTouched, pathlessTouched, setTouched } = useFieldTouched(getPath, form);
const { errors, setErrors, isValid, errorMessage } = useFieldValidity(getPath, form);
const { errors, setErrors, isValid, errorMessage, pathlessValidity } = useFieldValidity(getPath, form);

const isDirty = computed(() => {
if (!form) {
Expand Down Expand Up @@ -124,7 +124,7 @@ export function useFormField<TValue = unknown>(opts?: Partial<FormFieldOptions<T
value: cloneDeep(oldPath ? tf.getFieldValue(oldPath) : pathlessValue.value),
touched: oldPath ? tf.isFieldTouched(oldPath) : pathlessTouched.value,
disabled: toValue(opts?.disabled) ?? false,
errors: oldPath ? [...tf.getFieldErrors(oldPath)] : errors.value,
errors: [...(oldPath ? tf.getFieldErrors(oldPath) : pathlessValidity.errors.value)],
};
});
}
Expand Down Expand Up @@ -188,7 +188,7 @@ function createFormTouchedRef(getPath: Getter<string | undefined>, form: FormCon
const isTouched = computed(() => {
const path = getPath();

return path ? form.isFieldTouched(path) : false;
return path ? form.isFieldTouched(path) : pathlessTouched.value;
}) as Ref<boolean>;

function setTouched(value: boolean) {
Expand Down Expand Up @@ -293,6 +293,7 @@ function createFormValidityRef(getPath: Getter<string | undefined>, form: FormCo
}

return {
pathlessValidity,
errors,
setErrors,
};
Expand All @@ -301,10 +302,15 @@ function createFormValidityRef(getPath: Getter<string | undefined>, form: FormCo
function createLocalValidity() {
const errors = shallowRef<string[]>([]);

return {
const api = {
errors,
setErrors(messages: Arrayable<string>) {
errors.value = messages ? normalizeArrayable(messages) : [];
},
};

return {
pathlessValidity: api,
...api,
};
}
7 changes: 1 addition & 6 deletions packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
<div class="flex gap-4 relative">
<form class="w-full">
<div v-for="(field, idx) in fields" :key="field.id" class="flex items-center">
<InputText
:name="`field[${idx}]`"
:label="`Field ${idx}`"
required
:disabled="values.field?.[idx] === 'hello'"
/>
<InputText :label="`Field ${idx}`" required />

<button type="button" class="bg-red-500 rounded text-white p-2" @click="fields.splice(idx, 1)">X</button>
</div>
Expand Down

0 comments on commit fc085eb

Please sign in to comment.