-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
FormSubmitButton
to handle form submission consistently (#28701)
Closes #28256 Signed-off-by: jchong <jhchong92@gmail.com>
- Loading branch information
Showing
12 changed files
with
118 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
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
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
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
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,47 @@ | ||
import { Button, ButtonProps } from "@patternfly/react-core"; | ||
import { PropsWithChildren } from "react"; | ||
import { FieldValues, FormState } from "react-hook-form"; | ||
|
||
export type FormSubmitButtonProps = Omit<ButtonProps, "isDisabled"> & { | ||
formState: FormState<FieldValues>; | ||
allowNonDirty?: boolean; | ||
allowInvalid?: boolean; | ||
isDisabled?: boolean; | ||
}; | ||
|
||
const isSubmittable = ( | ||
formState: FormState<FieldValues>, | ||
allowNonDirty: boolean, | ||
allowInvalid: boolean, | ||
) => { | ||
return ( | ||
(formState.isValid || allowInvalid) && | ||
(formState.isDirty || allowNonDirty) && | ||
!formState.isLoading && | ||
!formState.isValidating && | ||
!formState.isSubmitting | ||
); | ||
}; | ||
|
||
export const FormSubmitButton = ({ | ||
formState, | ||
isDisabled = false, | ||
allowInvalid = false, | ||
allowNonDirty = false, | ||
children, | ||
...rest | ||
}: PropsWithChildren<FormSubmitButtonProps>) => { | ||
return ( | ||
<Button | ||
variant="primary" | ||
isDisabled={ | ||
(formState && !isSubmittable(formState, allowNonDirty, allowInvalid)) || | ||
isDisabled | ||
} | ||
{...rest} | ||
type="submit" | ||
> | ||
{children} | ||
</Button> | ||
); | ||
}; |
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