|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + Store, |
| 4 | + FormInstance, |
| 5 | + FieldData, |
| 6 | + ValidateMessages, |
| 7 | + Callbacks, |
| 8 | + InternalFormInstance, |
| 9 | +} from './interface'; |
| 10 | +import useForm from './useForm'; |
| 11 | +import FieldContext, { HOOK_MARK } from './FieldContext'; |
| 12 | +import FormContext, { FormContextProps } from './FormContext'; |
| 13 | + |
| 14 | +type BaseFormProps = Omit<React.FormHTMLAttributes<HTMLFormElement>, 'onSubmit'>; |
| 15 | + |
| 16 | +export interface StateFormProps extends BaseFormProps { |
| 17 | + initialValues?: Store; |
| 18 | + form?: FormInstance; |
| 19 | + children?: (() => JSX.Element | React.ReactNode) | React.ReactNode; |
| 20 | + fields?: FieldData[]; |
| 21 | + name?: string; |
| 22 | + validateMessages?: ValidateMessages; |
| 23 | + onValuesChange?: Callbacks['onValuesChange']; |
| 24 | + onFieldsChange?: Callbacks['onFieldsChange']; |
| 25 | + onFinish?: (values: Store) => void; |
| 26 | +} |
| 27 | + |
| 28 | +const StateForm: React.FunctionComponent<StateFormProps> = ( |
| 29 | + { |
| 30 | + name, |
| 31 | + initialValues, |
| 32 | + fields, |
| 33 | + form, |
| 34 | + children, |
| 35 | + validateMessages, |
| 36 | + onValuesChange, |
| 37 | + onFieldsChange, |
| 38 | + onFinish, |
| 39 | + ...restProps |
| 40 | + }: StateFormProps, |
| 41 | + ref, |
| 42 | +) => { |
| 43 | + const formContext: FormContextProps = React.useContext(FormContext); |
| 44 | + |
| 45 | + // We customize handle event since Context will makes all the consumer re-render: |
| 46 | + // https://reactjs.org/docs/context.html#contextprovider |
| 47 | + const [formInstance] = useForm(form); |
| 48 | + const { |
| 49 | + useSubscribe, |
| 50 | + setInitialValues, |
| 51 | + setCallbacks, |
| 52 | + setValidateMessages, |
| 53 | + } = (formInstance as InternalFormInstance).getInternalHooks(HOOK_MARK); |
| 54 | + |
| 55 | + // Pass ref with form instance |
| 56 | + React.useImperativeHandle(ref, () => formInstance); |
| 57 | + |
| 58 | + // Register form into Context |
| 59 | + React.useEffect(() => { |
| 60 | + return formContext.registerForm(name, formInstance); |
| 61 | + }, [name]); |
| 62 | + |
| 63 | + // Pass props to store |
| 64 | + setValidateMessages({ |
| 65 | + ...formContext.validateMessages, |
| 66 | + ...validateMessages, |
| 67 | + }); |
| 68 | + setCallbacks({ |
| 69 | + onValuesChange, |
| 70 | + onFieldsChange: (changedFields: FieldData[], ...rest) => { |
| 71 | + formContext.triggerFormChange(name, changedFields); |
| 72 | + |
| 73 | + if (onFieldsChange) { |
| 74 | + onFieldsChange(changedFields, ...rest); |
| 75 | + } |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + // Initial store value when first mount |
| 80 | + const mountRef = React.useRef(null); |
| 81 | + if (!mountRef.current) { |
| 82 | + mountRef.current = true; |
| 83 | + setInitialValues(initialValues); |
| 84 | + } |
| 85 | + |
| 86 | + // Prepare children by `children` type |
| 87 | + let childrenNode = children; |
| 88 | + const childrenRenderProps = typeof children === 'function'; |
| 89 | + if (childrenRenderProps) { |
| 90 | + const values = formInstance.getFieldsValue(); |
| 91 | + childrenNode = (children as any)(values, formInstance); |
| 92 | + } |
| 93 | + |
| 94 | + // Not use subscribe when using render props |
| 95 | + useSubscribe(!childrenRenderProps); |
| 96 | + |
| 97 | + // Listen if fields provided. We use ref to save prev data here to avoid additional render |
| 98 | + const prevFieldsRef = React.useRef<FieldData[] | undefined>(); |
| 99 | + if (prevFieldsRef.current !== fields) { |
| 100 | + formInstance.setFields(fields || []); |
| 101 | + } |
| 102 | + prevFieldsRef.current = fields; |
| 103 | + |
| 104 | + return ( |
| 105 | + <form |
| 106 | + {...restProps} |
| 107 | + onSubmit={event => { |
| 108 | + event.preventDefault(); |
| 109 | + event.stopPropagation(); |
| 110 | + |
| 111 | + formInstance |
| 112 | + .validateFields() |
| 113 | + .then(values => { |
| 114 | + if (onFinish) { |
| 115 | + onFinish(values); |
| 116 | + } |
| 117 | + }) |
| 118 | + // Do nothing about submit catch |
| 119 | + .catch(e => e); |
| 120 | + }} |
| 121 | + > |
| 122 | + <FieldContext.Provider value={formInstance as InternalFormInstance}> |
| 123 | + {childrenNode} |
| 124 | + </FieldContext.Provider> |
| 125 | + </form> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +export default StateForm; |
0 commit comments