Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
MillanWangGadget committed Sep 12, 2024
1 parent 0b52c96 commit 77553ee
Show file tree
Hide file tree
Showing 4 changed files with 37 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
25 changes: 20 additions & 5 deletions packages/react/src/auto/AutoForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,22 @@ 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;

//
//
// Somehow, these are at different depths. Need the model level containing the modelField stuff.
// QUESTION - Can global actions or custom params ever be affected by this??
//
//

return yupResolver(validationSchema(action.inputFields));
}, [metadata]);
// return yupResolver(validationSchema(allFieldMetadata));
}, [metadata, allFieldMetadata]);
};

/**
Expand Down Expand Up @@ -146,6 +156,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 +185,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 +227,7 @@ export const useAutoForm = <
});
}

console.log("fieldsToSend :", fieldsToSend);
return fieldsToSend;
},
onError: onFailure,
Expand Down Expand Up @@ -254,6 +268,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
1 change: 1 addition & 0 deletions packages/react/src/validationSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export const isFailedJSONParse = (value: any): value is FailedJSONParse => {
export const validationSchema = (fields: FieldMetadata[]) => {
const validators: Record<string, ISchema<any>> = {};
for (const field of fields) {
console.log("field.apiIdentifier :", field.apiIdentifier);
validators[field.apiIdentifier] = validatorForField(field);
}
return object(validators);
Expand Down

0 comments on commit 77553ee

Please sign in to comment.