-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!(Form): show Action state in submit button
- Loading branch information
Showing
11 changed files
with
138 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/components/src/components/Action/ActionStateContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { FC, PropsWithChildren } from "react"; | ||
import React, { useEffect } from "react"; | ||
import { ActionModel } from "@/components/Action/models/ActionModel"; | ||
import Action from "@/components/Action/index"; | ||
|
||
interface Props extends PropsWithChildren { | ||
isStarted?: boolean; | ||
hasSucceeded?: boolean; | ||
hasFailedWithError?: unknown; | ||
} | ||
|
||
export const ActionStateContext: FC<Props> = (props) => { | ||
const { isStarted, hasFailedWithError, hasSucceeded, children } = props; | ||
const action = ActionModel.useNew({}); | ||
|
||
useEffect(() => { | ||
if (hasSucceeded) { | ||
void action.state.onSucceeded(); | ||
} else if (hasFailedWithError) { | ||
void action.state.onFailed(hasFailedWithError); | ||
} else if (isStarted) { | ||
void action.state.onAsyncStart(); | ||
} | ||
}, [isStarted, hasFailedWithError, hasSucceeded]); | ||
|
||
return <Action actionModel={action}>{children}</Action>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
packages/components/src/integrations/react-hook-form/components/Controller/index.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/components/src/integrations/react-hook-form/components/Field/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Field } from "@/integrations/react-hook-form/components/Field/Field"; | ||
export * from "./Field"; | ||
export default Field; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 59 additions & 5 deletions
64
packages/components/src/integrations/react-hook-form/components/Form/Form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,74 @@ | ||
import type { ComponentProps, PropsWithChildren } from "react"; | ||
import type { | ||
ComponentProps, | ||
FormEventHandler, | ||
PropsWithChildren, | ||
} from "react"; | ||
import { useEffect } from "react"; | ||
import React from "react"; | ||
import type { FieldValues, UseFormReturn } from "react-hook-form"; | ||
import { useFormState } from "react-hook-form"; | ||
import { FormContextProvider } from "@/integrations/react-hook-form/components/context/formContext"; | ||
import { ActionStateContext } from "@/components/Action/ActionStateContext"; | ||
|
||
export type FormOnSubmitHandler<F extends FieldValues> = Parameters< | ||
UseFormReturn<F>["handleSubmit"] | ||
>[0]; | ||
|
||
interface Props<F extends FieldValues> | ||
extends ComponentProps<"form">, | ||
extends Omit<ComponentProps<"form">, "onSubmit">, | ||
PropsWithChildren { | ||
form?: UseFormReturn<F>; | ||
form: UseFormReturn<F>; | ||
onSubmit: FormOnSubmitHandler<F>; | ||
} | ||
|
||
export function Form<F extends FieldValues>(props: Props<F>) { | ||
const { form, children, ...formProps } = props; | ||
const { form, children, onSubmit, ...formProps } = props; | ||
|
||
const { isValid, isSubmitted, isSubmitting, isSubmitSuccessful, errors } = | ||
useFormState(form); | ||
|
||
const unwatchedFormState = form.control._formState; | ||
|
||
useEffect(() => { | ||
if (isSubmitted && isValid) { | ||
form.reset(undefined, { | ||
keepIsSubmitted: false, | ||
keepIsSubmitSuccessful: false, | ||
keepDefaultValues: true, | ||
keepValues: true, | ||
keepDirtyValues: true, | ||
keepIsValid: true, | ||
keepDirty: true, | ||
keepErrors: true, | ||
keepTouched: true, | ||
keepIsValidating: true, | ||
keepSubmitCount: true, | ||
}); | ||
} | ||
}, [isSubmitted, isValid]); | ||
|
||
const handleOnSubmit: FormEventHandler = (e) => { | ||
if (unwatchedFormState.isSubmitting || unwatchedFormState.isValidating) { | ||
e.preventDefault(); | ||
} else { | ||
form.handleSubmit(onSubmit)(e); | ||
} | ||
}; | ||
|
||
const submitError = isSubmitted ? errors : undefined; | ||
const submitSucceeded = isSubmitted && isSubmitSuccessful; | ||
|
||
return ( | ||
<FormContextProvider value={{ form }}> | ||
<form {...formProps}>{children}</form> | ||
<form {...formProps} onSubmit={handleOnSubmit}> | ||
<ActionStateContext | ||
isStarted={isSubmitting} | ||
hasFailedWithError={submitError} | ||
hasSucceeded={submitSucceeded} | ||
> | ||
{children} | ||
</ActionStateContext> | ||
</form> | ||
</FormContextProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./components/Controller"; | ||
export * from "./components/Field"; | ||
export * from "./components/Form"; |