Skip to content

Commit

Permalink
fix: return empty array if no errors and clone retrieved errors
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Aug 3, 2024
1 parent 268f8c4 commit 325c30f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/form/formContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function createFormContext<TForm extends FormObject = FormObject>({
}

function getFieldErrors<TPath extends Path<TForm>>(path: TPath) {
return getFromPath<string[]>(errors, escapePath(path), []) || [];
return [...(getFromPath<string[]>(errors, escapePath(path), []) || [])];
}

function setFieldErrors<TPath extends Path<TForm>>(path: TPath, message: Arrayable<string>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/form/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function useForm<TForm extends FormObject = FormObject>(opts?: Partial<Fo
const { actions, onSubmitted, isSubmitting } = useFormActions(ctx, disabled);

function getError<TPath extends Path<TForm>>(path: TPath): string | undefined {
return ctx.getFieldErrors(path)?.[0];
return ctx.getFieldErrors(path)[0];
}

provide(FormKey, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/form/useFormField.ts
Original file line number Diff line number Diff line change
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: errors.value,
errors: oldPath ? [...tf.getFieldErrors(oldPath)] : errors.value,
};
});
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/form/useFormTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ const TransactionKind = {
export interface FormTransactionManager<TForm extends FormObject> {
transaction(
tr: (
formCtx: Pick<FormContext<TForm>, 'getValues' | 'getFieldValue' | 'isFieldSet' | 'isFieldTouched'>,
formCtx: Pick<
FormContext<TForm>,
'getValues' | 'getFieldValue' | 'isFieldSet' | 'isFieldTouched' | 'getFieldErrors'
>,
codes: typeof TransactionKind,
) => FormTransaction<TForm> | null,
): void;
Expand Down
7 changes: 6 additions & 1 deletion packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<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}`" :disabled="values.field?.[idx] === 'hello'" />
<InputText
:name="`field[${idx}]`"
:label="`Field ${idx}`"
required
:disabled="values.field?.[idx] === 'hello'"
/>

<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 325c30f

Please sign in to comment.