Skip to content

Commit

Permalink
fix: correctly handle having a default form id but only sending it to…
Browse files Browse the repository at this point in the history
… the server when it isn't the default
  • Loading branch information
airjp73 committed Nov 12, 2024
1 parent 2a8c7ce commit da073db
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
10 changes: 8 additions & 2 deletions packages/core/src/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ export const getAllErrors = (state: FormStoreValue) => {
return Object.fromEntries(fieldsWithErrors);
};

export const getFormId = (state: FormStoreValue) => state.formProps.id;
export const getFormId = (state: FormStoreValue) =>
state.formProps.id ?? state.defaultFormId;

export const getFormProps = (state: FormStoreValue) => state.formProps;
export const getFormIdOption = (state: FormStoreValue) => state.formProps.id;

export const getFormProps = (state: FormStoreValue) => ({
...state.formProps,
id: state.formProps.id ?? state.defaultFormId,
});

export const getFormAction = (state: FormStoreValue) => state.formProps.action;
15 changes: 11 additions & 4 deletions packages/core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { GenericObject, preprocessFormData } from "./native-form-data/flatten";
import { MultiValueMap } from "./native-form-data/MultiValueMap";
import { insert, move, remove, replace, toSwapped } from "./arrayUtil";
import { getFieldDefaultValue, getFieldValue } from "./getters";
import { F } from "vitest/dist/reporters-yx5ZTtEV";

export type FieldSerializer = (value: unknown) => string;

Expand Down Expand Up @@ -152,7 +151,7 @@ export type ResolverQueue = ReturnType<typeof createResolverQueue>;

export type StoreFormProps = {
action?: string;
id: string;
id?: string;
};

export type StoreFlags = {
Expand All @@ -175,6 +174,7 @@ type StoreState = {
submitSource: "state" | "dom";
formProps: StoreFormProps;
flags: StoreFlags;
defaultFormId: string;
};

export type SubmitterOptions = {
Expand Down Expand Up @@ -455,7 +455,11 @@ export const createFormStateStore = ({
arrayUpdateKeys: {},
validationBehaviorConfig,
submitSource,
formProps,
formProps: {
...formProps,
id: formProps.id,
},
defaultFormId: genKey(),
flags,

/////// Validation
Expand Down Expand Up @@ -862,7 +866,10 @@ export const createFormStateStore = ({
set((state) => {
state.submitSource = submitSource;
state.validationBehaviorConfig = validationBehaviorConfig;
state.formProps = formProps;
state.formProps = {
...formProps,
id: formProps.id ?? state.formProps.id,
};
state.flags = flags;
});
},
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getFormProps,
SubmitterOptions,
FORM_ID_FIELD_NAME,
getFormIdOption,
} from "@rvf/core";
import {
StringToPathTuple,
Expand Down Expand Up @@ -685,7 +686,7 @@ export const makeBaseFormApi = <FormInputData,>({
},

renderFormIdInput: () => {
const formId = getFormId(trackedState);
const formId = getFormIdOption(trackedState);
if (!formId) return null;
return <input type="hidden" name={FORM_ID_FIELD_NAME} value={formId} />;
},
Expand Down
11 changes: 4 additions & 7 deletions packages/react/src/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ export function useForm<
otherFormProps,
reloadDocument,
validationBehaviorConfig,
id: providedFormId,
id,
} = options;

const defaultFormId = useId();

const [form] = useState<FormScope<unknown>>(() => {
const rvf = createFormScope({
defaultValues: options.defaultValues ?? {},
Expand All @@ -198,7 +196,7 @@ export function useForm<
submitSource: submitSource ?? "dom",
formProps: {
action,
id: providedFormId ?? defaultFormId,
id,
...otherFormProps,
},
flags: {
Expand Down Expand Up @@ -260,7 +258,7 @@ export function useForm<
: undefined,
formProps: {
action,
id: providedFormId ?? defaultFormId,
id,
...otherFormProps,
},
flags: {
Expand All @@ -275,8 +273,7 @@ export function useForm<
whenSubmitted,
whenTouched,
action,
providedFormId,
defaultFormId,
id,
disableFocusOnError,
otherFormProps,
reloadDocument,
Expand Down
7 changes: 2 additions & 5 deletions packages/remix/src/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ export function useForm<FormInputData extends FieldValues, FormOutputData>(
): FormApi<FormInputData> {
let rvf: FormApi<FormInputData>;

const defaultId = useId();
const formId = rvfOpts.id ?? defaultId;

const { fetcher, submitSource = "dom" } = rvfOpts;
const serverStuff = useServerValidationErrors({
formId,
formId: rvfOpts.id,
defaultValues: rvfOpts.defaultValues as any,
fetcher: rvfOpts.fetcher,
});
Expand Down Expand Up @@ -152,7 +149,7 @@ export function useForm<FormInputData extends FieldValues, FormOutputData>(
rvf = useFormReact<FormInputData, FormOutputData, void>({
...rvfOpts,
...serverStuff,
id: formId,
id: rvfOpts.id,
otherFormProps: {
method: rvfOpts.method,
encType: rvfOpts.encType,
Expand Down

0 comments on commit da073db

Please sign in to comment.