-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CareUI: Adds a higher-order
Form
component (not used anywhere) (#4633)
* remove `reducerProps` to make things simpler * Implements `Form` HOC and `FormContext` * removes unused reducer props from search input * RequiredFieldValidator to support custom message * consume form context and resolve props internally * rename 'props' to 'field' to strictly break if incorrect migration * migrate existing fields * fix month form field
- Loading branch information
1 parent
030d6ec
commit 819614b
Showing
18 changed files
with
346 additions
and
229 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { isEmpty, omitBy } from "lodash"; | ||
import { useEffect, useReducer, useState } from "react"; | ||
import { classNames } from "../../Utils/utils"; | ||
import { Cancel, Submit } from "../Common/components/ButtonV2"; | ||
import { FieldValidator } from "./FieldValidators"; | ||
import { FormContext, FormContextValue } from "./FormContext"; | ||
import { FieldChangeEvent } from "./FormFields/Utils"; | ||
import { FormDetails, FormErrors, formReducer, FormReducer } from "./Utils"; | ||
|
||
type Props<T extends FormDetails> = { | ||
context: FormContext<T>; | ||
className?: string; | ||
defaults: T; | ||
asyncGetDefaults?: (() => Promise<T>) | false; | ||
onlyChild?: boolean; | ||
validate?: (form: T) => FormErrors<T>; | ||
onSubmit: (form: T) => Promise<FormErrors<T> | void>; | ||
onCancel?: () => void; | ||
noPadding?: true; | ||
disabled?: boolean; | ||
submitLabel?: string; | ||
cancelLabel?: string; | ||
children: (props: FormContextValue<T>) => React.ReactNode; | ||
}; | ||
|
||
const Form = <T extends FormDetails>({ | ||
asyncGetDefaults, | ||
validate, | ||
...props | ||
}: Props<T>) => { | ||
const initial = { form: props.defaults, errors: {} }; | ||
const [isLoading, setIsLoading] = useState(!!asyncGetDefaults); | ||
const [state, dispatch] = useReducer<FormReducer<T>>(formReducer, initial); | ||
|
||
useEffect(() => { | ||
if (!asyncGetDefaults) return; | ||
|
||
asyncGetDefaults().then((form) => { | ||
dispatch({ type: "set_form", form }); | ||
setIsLoading(false); | ||
}); | ||
}, [asyncGetDefaults]); | ||
|
||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (validate) { | ||
const errors = omitBy(validate(state.form), isEmpty) as FormErrors<T>; | ||
|
||
if (Object.keys(errors).length) { | ||
dispatch({ type: "set_errors", errors }); | ||
return; | ||
} | ||
} | ||
|
||
const errors = await props.onSubmit(state.form); | ||
if (errors) { | ||
dispatch({ | ||
type: "set_errors", | ||
errors: { ...state.errors, ...errors }, | ||
}); | ||
} | ||
}; | ||
|
||
const { Provider, Consumer } = props.context; | ||
const disabled = isLoading || props.disabled; | ||
|
||
return ( | ||
<form | ||
onSubmit={handleSubmit} | ||
className={classNames( | ||
"bg-white rounded w-full max-w-3xl mx-auto", | ||
!props.noPadding && "px-8 md:px-16 py-5 md:py-11", | ||
props.className | ||
)} | ||
noValidate | ||
> | ||
<Provider | ||
value={(name: keyof T, validate?: FieldValidator<T[keyof T]>) => { | ||
return { | ||
name, | ||
id: name, | ||
onChange: ({ name, value }: FieldChangeEvent<T[keyof T]>) => | ||
dispatch({ | ||
type: "set_field", | ||
name, | ||
value, | ||
error: validate && validate(value), | ||
}), | ||
value: state.form[name], | ||
error: state.errors[name], | ||
disabled, | ||
}; | ||
}} | ||
> | ||
{props.onlyChild ? ( | ||
<Consumer>{props.children}</Consumer> | ||
) : ( | ||
<> | ||
<div className="my-6 flex flex-col gap-4"> | ||
<Consumer>{props.children}</Consumer> | ||
</div> | ||
<div className="flex justify-end gap-2"> | ||
<Cancel | ||
className="w-full md:w-auto" | ||
onClick={props.onCancel} | ||
label={props.cancelLabel ?? "Cancel"} | ||
/> | ||
<Submit | ||
type="submit" | ||
className="w-full md:w-auto" | ||
disabled={disabled} | ||
label={props.submitLabel ?? "Submit"} | ||
/> | ||
</div> | ||
</> | ||
)} | ||
</Provider> | ||
</form> | ||
); | ||
}; | ||
|
||
export default Form; |
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,19 @@ | ||
import { Context, createContext } from "react"; | ||
import { FieldError, FieldValidator } from "./FieldValidators"; | ||
import { FormDetails } from "./Utils"; | ||
|
||
export type FormContextValue<T extends FormDetails> = ( | ||
name: keyof T, | ||
validate?: FieldValidator<T[keyof T]> | ||
) => { | ||
id: keyof T; | ||
name: keyof T; | ||
onChange: any; | ||
value: any; | ||
error: FieldError | undefined; | ||
}; | ||
|
||
export type FormContext<T extends FormDetails> = Context<FormContextValue<T>>; | ||
|
||
export const createFormContext = <T extends FormDetails>() => | ||
createContext<FormContextValue<T>>(undefined as any); |
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
Oops, something went wrong.