|
| 1 | +import * as React from 'react'; |
| 2 | +import { type ReactNode, useEffect } from 'react'; |
| 3 | +import { useFieldArray, useFormContext } from 'react-hook-form'; |
| 4 | +import type { InputProps } from '../../form/useInput'; |
| 5 | +import { composeSyncValidators } from '../../form/validation/validate'; |
| 6 | +import { useGetValidationErrorMessage } from '../../form/validation/useGetValidationErrorMessage'; |
| 7 | +import { useApplyInputDefaultValues } from '../../form/useApplyInputDefaultValues'; |
| 8 | +import { useFormGroupContext } from '../../form/groups/useFormGroupContext'; |
| 9 | +import { useFormGroups } from '../../form/groups/useFormGroups'; |
| 10 | +import { |
| 11 | + OptionalResourceContextProvider, |
| 12 | + SourceContextProvider, |
| 13 | + type SourceContextValue, |
| 14 | + useSourceContext, |
| 15 | +} from '../../core'; |
| 16 | +import { ArrayInputContext } from './ArrayInputContext'; |
| 17 | + |
| 18 | +/** |
| 19 | + * To edit arrays of data embedded inside a record, <ArrayInput> creates a list of sub-forms. |
| 20 | + * |
| 21 | + * @example |
| 22 | + * |
| 23 | + * import { ArrayInput, SimpleFormIterator, DateInput, TextInput } from 'react-admin'; |
| 24 | + * |
| 25 | + * <ArrayInput source="backlinks"> |
| 26 | + * <SimpleFormIterator> |
| 27 | + * <DateInput source="date" /> |
| 28 | + * <TextInput source="url" /> |
| 29 | + * </SimpleFormIterator> |
| 30 | + * </ArrayInput> |
| 31 | + * |
| 32 | + * <ArrayInput> allows the edition of embedded arrays, like the backlinks field |
| 33 | + * in the following post record: |
| 34 | + * |
| 35 | + * { |
| 36 | + * id: 123 |
| 37 | + * backlinks: [ |
| 38 | + * { |
| 39 | + * date: '2012-08-10T00:00:00.000Z', |
| 40 | + * url: 'http://example.com/foo/bar.html', |
| 41 | + * }, |
| 42 | + * { |
| 43 | + * date: '2012-08-14T00:00:00.000Z', |
| 44 | + * url: 'https://blog.johndoe.com/2012/08/12/foobar.html', |
| 45 | + * } |
| 46 | + * ] |
| 47 | + * } |
| 48 | + * |
| 49 | + * <ArrayInput> expects a single child, which must be a *form iterator* component. |
| 50 | + * A form iterator is a component accepting a fields object as passed by |
| 51 | + * react-hook-form-arrays's useFieldArray() hook, and defining a layout for |
| 52 | + * an array of fields. For instance, the <SimpleFormIterator> component |
| 53 | + * displays an array of fields in an unordered list (<ul>), one sub-form by |
| 54 | + * list item (<li>). It also provides controls for adding and removing |
| 55 | + * a sub-record (a backlink in this example). |
| 56 | + * |
| 57 | + * @see {@link https://react-hook-form.com/docs/usefieldarray} |
| 58 | + */ |
| 59 | +export const ArrayInputBase = (props: ArrayInputBaseProps) => { |
| 60 | + const { |
| 61 | + children, |
| 62 | + defaultValue = [], |
| 63 | + isPending, |
| 64 | + loading, |
| 65 | + resource: resourceFromProps, |
| 66 | + source: arraySource, |
| 67 | + validate, |
| 68 | + } = props; |
| 69 | + |
| 70 | + const formGroupName = useFormGroupContext(); |
| 71 | + const formGroups = useFormGroups(); |
| 72 | + const parentSourceContext = useSourceContext(); |
| 73 | + const finalSource = parentSourceContext.getSource(arraySource); |
| 74 | + |
| 75 | + const sanitizedValidate = Array.isArray(validate) |
| 76 | + ? composeSyncValidators(validate) |
| 77 | + : validate; |
| 78 | + const getValidationErrorMessage = useGetValidationErrorMessage(); |
| 79 | + |
| 80 | + const { getValues } = useFormContext(); |
| 81 | + |
| 82 | + const fieldProps = useFieldArray({ |
| 83 | + name: finalSource, |
| 84 | + rules: { |
| 85 | + validate: async value => { |
| 86 | + if (!sanitizedValidate) return true; |
| 87 | + const error = await sanitizedValidate( |
| 88 | + value, |
| 89 | + getValues(), |
| 90 | + props |
| 91 | + ); |
| 92 | + |
| 93 | + if (!error) return true; |
| 94 | + return getValidationErrorMessage(error); |
| 95 | + }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + useEffect(() => { |
| 100 | + if (formGroups && formGroupName != null) { |
| 101 | + formGroups.registerField(finalSource, formGroupName); |
| 102 | + } |
| 103 | + |
| 104 | + return () => { |
| 105 | + if (formGroups && formGroupName != null) { |
| 106 | + formGroups.unregisterField(finalSource, formGroupName); |
| 107 | + } |
| 108 | + }; |
| 109 | + }, [finalSource, formGroups, formGroupName]); |
| 110 | + |
| 111 | + useApplyInputDefaultValues({ |
| 112 | + inputProps: { ...props, defaultValue }, |
| 113 | + isArrayInput: true, |
| 114 | + fieldArrayInputControl: fieldProps, |
| 115 | + }); |
| 116 | + |
| 117 | + // The SourceContext will be read by children of ArrayInput to compute their composed source and label |
| 118 | + // |
| 119 | + // <ArrayInput source="orders" /> => SourceContext is "orders" |
| 120 | + // <SimpleFormIterator> => SourceContext is "orders.0" |
| 121 | + // <DateInput source="date" /> => final source for this input will be "orders.0.date" |
| 122 | + // </SimpleFormIterator> |
| 123 | + // </ArrayInput> |
| 124 | + // |
| 125 | + const sourceContext = React.useMemo<SourceContextValue>( |
| 126 | + () => ({ |
| 127 | + // source is the source of the ArrayInput child |
| 128 | + getSource: (source: string) => { |
| 129 | + if (!source) { |
| 130 | + // SimpleFormIterator calls getSource('') to get the arraySource |
| 131 | + return parentSourceContext.getSource(arraySource); |
| 132 | + } |
| 133 | + |
| 134 | + // We want to support nesting and composition with other inputs (e.g. TranslatableInputs, ReferenceOneInput, etc), |
| 135 | + // we must also take into account the parent SourceContext |
| 136 | + // |
| 137 | + // <ArrayInput source="orders" /> => SourceContext is "orders" |
| 138 | + // <SimpleFormIterator> => SourceContext is "orders.0" |
| 139 | + // <DateInput source="date" /> => final source for this input will be "orders.0.date" |
| 140 | + // <ArrayInput source="items" /> => SourceContext is "orders.0.items" |
| 141 | + // <SimpleFormIterator> => SourceContext is "orders.0.items.0" |
| 142 | + // <TextInput source="reference" /> => final source for this input will be "orders.0.items.0.reference" |
| 143 | + // </SimpleFormIterator> |
| 144 | + // </ArrayInput> |
| 145 | + // </SimpleFormIterator> |
| 146 | + // </ArrayInput> |
| 147 | + return parentSourceContext.getSource( |
| 148 | + `${arraySource}.${source}` |
| 149 | + ); |
| 150 | + }, |
| 151 | + // if Array source is items, and child source is name, .0.name => resources.orders.fields.items.name |
| 152 | + getLabel: (source: string) => |
| 153 | + parentSourceContext.getLabel(`${arraySource}.${source}`), |
| 154 | + }), |
| 155 | + [parentSourceContext, arraySource] |
| 156 | + ); |
| 157 | + |
| 158 | + if (isPending && loading !== undefined && loading !== false) { |
| 159 | + return loading; |
| 160 | + } |
| 161 | + |
| 162 | + return ( |
| 163 | + <ArrayInputContext.Provider value={fieldProps}> |
| 164 | + <OptionalResourceContextProvider value={resourceFromProps}> |
| 165 | + <SourceContextProvider value={sourceContext}> |
| 166 | + {children} |
| 167 | + </SourceContextProvider> |
| 168 | + </OptionalResourceContextProvider> |
| 169 | + </ArrayInputContext.Provider> |
| 170 | + ); |
| 171 | +}; |
| 172 | + |
| 173 | +export const getArrayInputError = error => { |
| 174 | + if (Array.isArray(error)) { |
| 175 | + return undefined; |
| 176 | + } |
| 177 | + return error; |
| 178 | +}; |
| 179 | + |
| 180 | +export interface ArrayInputBaseProps |
| 181 | + extends Omit<InputProps, 'disabled' | 'readOnly'> { |
| 182 | + children: ReactNode; |
| 183 | + loading?: ReactNode; |
| 184 | + isFetching?: boolean; |
| 185 | + isLoading?: boolean; |
| 186 | + isPending?: boolean; |
| 187 | +} |
0 commit comments