Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MillanWangGadget committed Sep 11, 2024
1 parent 981af30 commit d0285dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/react/spec/auto/PolarisAutoForm.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export const ExcludedWithDefaultValues = {
export const Included = {
args: {
action: api.widget.create,
include: ["name", "inventoryCount"],
// include: ["name", "inventoryCount"],
include: ["name"],
},
};

Expand Down
16 changes: 11 additions & 5 deletions packages/react/src/auto/AutoForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ export type AutoFormProps<
/**
* React hook for getting the validation schema for a list of fields
*/
export const useValidationResolver = (metadata: ActionMetadata | GlobalActionMetadata | undefined) => {
const useValidationResolver = (metadata: ActionMetadata | GlobalActionMetadata | undefined, allFieldMetadata: FieldMetadata[]) => {
console.log("metadata :", metadata);
return useMemo(() => {
if (!metadata) return undefined;
const action = isActionMetadata(metadata) ? metadata.action : metadata;
return yupResolver(validationSchema(action.inputFields));
}, [metadata]);
}, [metadata, allFieldMetadata]);
};

/**
Expand Down Expand Up @@ -146,6 +147,7 @@ export const useAutoForm = <

// filter down the fields to render only what we want to render for this form
const fields = useFormFields(metadata, props);
console.log("fields :", fields);
const isDeleteAction = metadata && isActionMetadata(metadata) && metadata.action.isDeleteAction;
const isGlobalAction = action.type === "globalAction";
const operatesWithRecordId = !!(metadata && isActionMetadata(metadata) && metadata.action.operatesWithRecordIdentity);
Expand Down Expand Up @@ -174,13 +176,15 @@ export const useAutoForm = <
reset,
setValue,
getValues,

formState: { isSubmitSuccessful, isLoading, isReady, isSubmitting, touchedFields, errors },
formState: { isSubmitSuccessful, isLoading, isReady, isSubmitting, touchedFields, errors: formStateErrors },
originalFormMethods,
} = useActionForm(action, {
defaultValues: defaultValues as any,
findBy: "findBy" in props ? props.findBy : undefined,
resolver: useValidationResolver(metadata),
resolver: useValidationResolver(
metadata,
fields.map(({ metadata }) => metadata)
),
send: () => {
const fieldsToSend = fields
.filter(({ path, metadata }) => {
Expand Down Expand Up @@ -214,6 +218,7 @@ export const useAutoForm = <
});
}

console.log("fieldsToSend :", fieldsToSend);
return fieldsToSend;
},
onError: onFailure,
Expand Down Expand Up @@ -254,6 +259,7 @@ export const useAutoForm = <
fields: fields.filter(removeIdFieldsUnlessUpsertWithoutFindBy(isUpsertWithFindBy)),
submit,
formError,
formStateErrors,
isSubmitting,
isSubmitSuccessful,
isLoading,
Expand Down
16 changes: 14 additions & 2 deletions packages/react/src/auto/polaris/PolarisAutoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@ const PolarisAutoFormComponent = <
findBy,
...rest
} = props as AutoFormProps<GivenOptions, SchemaT, ActionFunc> & Omit<Partial<FormProps>, "action"> & { findBy: any };
const { metadata, fetchingMetadata, metadataError, fields, submit, formError, isSubmitting, isSubmitSuccessful, originalFormMethods } =
useAutoForm(props);
const {
metadata,
fetchingMetadata,
metadataError,
fields,
submit,
isSubmitting,
isSubmitSuccessful,
originalFormMethods,
formError,
formStateErrors,
} = useAutoForm(props);

const autoFormMetadataContext: AutoFormMetadataContext = {
findBy,
Expand Down Expand Up @@ -124,6 +134,8 @@ const PolarisAutoFormComponent = <

return (
<AutoFormMetadataContext.Provider value={autoFormMetadataContext}>
<p>formStateErrors: {JSON.stringify(formStateErrors)}</p>
<p>formError: {JSON.stringify(formError)}</p>
<FormProvider {...originalFormMethods}>
<Form {...rest} onSubmit={submit}>
<BlockStack gap="400">{formContent}</BlockStack>
Expand Down

0 comments on commit d0285dd

Please sign in to comment.