|
| 1 | +import { |
| 2 | + FormComponentProps, |
| 3 | + FormComponentSlotProps, |
| 4 | + FormDataConvertible, |
| 5 | + formDataToObject, |
| 6 | + mergeDataIntoQueryString, |
| 7 | + Method, |
| 8 | + VisitOptions, |
| 9 | +} from '@inertiajs/core' |
| 10 | +import { isEqual } from 'es-toolkit' |
| 11 | +import { createElement, FormEvent, ReactNode, useEffect, useMemo, useRef, useState } from 'react' |
| 12 | +import useForm from './useForm' |
| 13 | + |
| 14 | +type ComponentProps = (FormComponentProps & |
| 15 | + Omit<React.FormHTMLAttributes<HTMLFormElement>, keyof FormComponentProps | 'children'> & |
| 16 | + Omit<React.AllHTMLAttributes<HTMLFormElement>, keyof FormComponentProps | 'children'>) & { |
| 17 | + children: (props: FormComponentSlotProps) => ReactNode |
| 18 | +} |
| 19 | + |
| 20 | +type FormSubmitOptions = Omit<VisitOptions, 'data' | 'onPrefetched' | 'onPrefetching'> |
| 21 | + |
| 22 | +const noop = () => undefined |
| 23 | + |
| 24 | +const Form = ({ |
| 25 | + action = '', |
| 26 | + method = 'get', |
| 27 | + headers = {}, |
| 28 | + queryStringArrayFormat = 'brackets', |
| 29 | + errorBag = null, |
| 30 | + showProgress = true, |
| 31 | + transform = (data) => data, |
| 32 | + options = {}, |
| 33 | + onStart = noop, |
| 34 | + onProgress = noop, |
| 35 | + onFinish = noop, |
| 36 | + onBefore = noop, |
| 37 | + onCancel = noop, |
| 38 | + onSuccess = noop, |
| 39 | + onError = noop, |
| 40 | + onCancelToken = noop, |
| 41 | + children, |
| 42 | + ...props |
| 43 | +}: ComponentProps) => { |
| 44 | + const form = useForm({}) |
| 45 | + const formElement = useRef<HTMLFormElement>(null) |
| 46 | + |
| 47 | + const resolvedMethod = useMemo(() => { |
| 48 | + return typeof action === 'object' ? action.method : (method.toLowerCase() as Method) |
| 49 | + }, [action, method]) |
| 50 | + |
| 51 | + const [isDirty, setIsDirty] = useState(false) |
| 52 | + const defaultValues = useRef<Record<string, FormDataConvertible>>({}) |
| 53 | + |
| 54 | + const getData = (): Record<string, FormDataConvertible> => { |
| 55 | + // Convert the FormData to an object because we can't compare two FormData |
| 56 | + // instances directly (which is needed for isDirty), mergeDataIntoQueryString() |
| 57 | + // expects an object, and submitting a FormData instance directly causes problems with nested objects. |
| 58 | + return formDataToObject(new FormData(formElement.current)) |
| 59 | + } |
| 60 | + |
| 61 | + const updateDirtyState = (event: Event) => |
| 62 | + setIsDirty(event.type === 'reset' ? false : !isEqual(getData(), defaultValues.current)) |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + defaultValues.current = getData() |
| 66 | + |
| 67 | + const formEvents: Array<keyof HTMLElementEventMap> = ['input', 'change', 'reset'] |
| 68 | + |
| 69 | + formEvents.forEach((e) => formElement.current.addEventListener(e, updateDirtyState)) |
| 70 | + |
| 71 | + return () => formEvents.forEach((e) => formElement.current?.removeEventListener(e, updateDirtyState)) |
| 72 | + }, []) |
| 73 | + |
| 74 | + const submit = () => { |
| 75 | + const [url, _data] = mergeDataIntoQueryString( |
| 76 | + resolvedMethod, |
| 77 | + typeof action === 'object' ? action.url : action, |
| 78 | + getData(), |
| 79 | + queryStringArrayFormat, |
| 80 | + ) |
| 81 | + |
| 82 | + const submitOptions: FormSubmitOptions = { |
| 83 | + headers, |
| 84 | + errorBag, |
| 85 | + showProgress, |
| 86 | + onCancelToken, |
| 87 | + onBefore, |
| 88 | + onStart, |
| 89 | + onProgress, |
| 90 | + onFinish, |
| 91 | + onCancel, |
| 92 | + onSuccess, |
| 93 | + onError, |
| 94 | + ...options, |
| 95 | + } |
| 96 | + |
| 97 | + form.transform(() => transform(_data)) |
| 98 | + form.submit(resolvedMethod, url, submitOptions) |
| 99 | + } |
| 100 | + |
| 101 | + return createElement( |
| 102 | + 'form', |
| 103 | + { |
| 104 | + ...props, |
| 105 | + ref: formElement, |
| 106 | + action: typeof action === 'object' ? action.url : action, |
| 107 | + method: resolvedMethod, |
| 108 | + onSubmit: (event: FormEvent<HTMLFormElement>) => { |
| 109 | + event.preventDefault() |
| 110 | + submit() |
| 111 | + }, |
| 112 | + }, |
| 113 | + typeof children === 'function' |
| 114 | + ? children({ |
| 115 | + errors: form.errors, |
| 116 | + hasErrors: form.hasErrors, |
| 117 | + processing: form.processing, |
| 118 | + progress: form.progress, |
| 119 | + wasSuccessful: form.wasSuccessful, |
| 120 | + recentlySuccessful: form.recentlySuccessful, |
| 121 | + setError: form.setError, |
| 122 | + clearErrors: form.clearErrors, |
| 123 | + resetAndClearErrors: form.resetAndClearErrors, |
| 124 | + isDirty, |
| 125 | + reset: () => formElement.current?.reset(), |
| 126 | + submit, |
| 127 | + }) |
| 128 | + : children, |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +Form.displayName = 'InertiaForm' |
| 133 | + |
| 134 | +export default Form |
0 commit comments