Skip to content

Commit

Permalink
feat: allow setting a name for the form (#4933)
Browse files Browse the repository at this point in the history
* feat: allow setting a name for the form

Resolves #4930

* chore: default the name to `Form`
  • Loading branch information
genu authored Nov 8, 2024
1 parent abf2af1 commit b81ec50
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/vee-validate/src/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const FormImpl = /** #__PURE__ */ defineComponent({
type: Boolean,
default: false,
},
name: {
type: String,
default: 'Form',
},
},
setup(props, ctx) {
const validationSchema = toRef(props, 'validationSchema');
Expand Down Expand Up @@ -108,6 +112,7 @@ const FormImpl = /** #__PURE__ */ defineComponent({
initialTouched: props.initialTouched,
validateOnMount: props.validateOnMount,
keepValuesOnUnmount: keepValues,
name: props.name,
});

const submitForm = handleSubmit((_, { evt }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/vee-validate/src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ function mapFormForDevtoolsInspector(form: PrivateFormContext): CustomInspectorN

return {
id: encodeNodeId(form),
label: 'Form',
label: form.name,
children,
tags: [
{
Expand Down
1 change: 1 addition & 0 deletions packages/vee-validate/src/types/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export interface PrivateFormContext<
TValues extends GenericObject = GenericObject,
TOutput extends GenericObject = TValues,
> extends FormActions<TValues> {
name: string;
formId: number;
values: TValues;
initialValues: Ref<Partial<TValues>>;
Expand Down
3 changes: 3 additions & 0 deletions packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface FormOptions<
initialTouched?: FlattenAndSetPathsType<TValues, boolean>;
validateOnMount?: boolean;
keepValuesOnUnmount?: MaybeRef<boolean>;
name?: string;
}

let FORM_COUNTER = 0;
Expand All @@ -117,6 +118,7 @@ export function useForm<
| TypedSchema<TValues, TOutput>,
>(opts?: FormOptions<TValues, TOutput, TSchema>): FormContext<TValues, TOutput> {
const formId = FORM_COUNTER++;
const name = opts?.name || 'Form';

// Prevents fields from double resetting their values, which causes checkboxes to toggle their initial value
let FIELD_ID_COUNTER = 0;
Expand Down Expand Up @@ -638,6 +640,7 @@ export function useForm<
}

const formCtx: PrivateFormContext<TValues, TOutput> = {
name,
formId,
values: formValues,
controlledValues,
Expand Down

0 comments on commit b81ec50

Please sign in to comment.