|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { useFormApi, FieldArray } from '@data-driven-forms/react-form-renderer'; |
| 4 | + |
| 5 | +import Grid from '@material-ui/core/Grid'; |
| 6 | +import Button from '@material-ui/core/Button'; |
| 7 | +import Typography from '@material-ui/core/Typography'; |
| 8 | +import FormControl from '@material-ui/core/FormControl'; |
| 9 | +import FormHelperText from '@material-ui/core/FormHelperText'; |
| 10 | +import { makeStyles } from '@material-ui/core/styles'; |
| 11 | + |
| 12 | +import { useFieldApi } from '@data-driven-forms/react-form-renderer'; |
| 13 | + |
| 14 | +import FormFieldGrid from '../common/form-field-grid'; |
| 15 | +import clsx from 'clsx'; |
| 16 | + |
| 17 | +const useFielArrayStyles = makeStyles({ |
| 18 | + formControl: { |
| 19 | + width: '100%' |
| 20 | + }, |
| 21 | + centerText: { |
| 22 | + display: 'flex', |
| 23 | + justifyContent: 'center' |
| 24 | + }, |
| 25 | + buttonsToEnd: { |
| 26 | + display: 'flex', |
| 27 | + justifyContent: 'flex-end' |
| 28 | + }, |
| 29 | + header: { |
| 30 | + display: 'flex' |
| 31 | + }, |
| 32 | + label: { |
| 33 | + flexGrow: 1 |
| 34 | + }, |
| 35 | + fieldArrayGroup: { |
| 36 | + marginBottom: 32 |
| 37 | + } |
| 38 | +}); |
| 39 | + |
| 40 | +const ArrayItem = ({ fields, fieldIndex, name, remove, length, minItems, removeLabel }) => { |
| 41 | + const { renderForm } = useFormApi(); |
| 42 | + const classes = useFielArrayStyles(); |
| 43 | + |
| 44 | + const editedFields = fields.map((field, index) => { |
| 45 | + const computedName = field.name ? `${name}.${field.name}` : name; |
| 46 | + return { ...field, name: computedName, key: `${computedName}-${index}` }; |
| 47 | + }); |
| 48 | + |
| 49 | + return ( |
| 50 | + <Grid container spacing={3}> |
| 51 | + <Grid item xs={12}> |
| 52 | + <Grid container spacing={3}> |
| 53 | + {renderForm([editedFields])} |
| 54 | + </Grid> |
| 55 | + </Grid> |
| 56 | + <Grid item xs={12} className={classes.buttonsToEnd}> |
| 57 | + <Button color="secondary" onClick={() => remove(fieldIndex)} disabled={length <= minItems}> |
| 58 | + {removeLabel} |
| 59 | + </Button> |
| 60 | + </Grid> |
| 61 | + </Grid> |
| 62 | + ); |
| 63 | +}; |
| 64 | + |
| 65 | +ArrayItem.propTypes = { |
| 66 | + name: PropTypes.string, |
| 67 | + fieldIndex: PropTypes.number.isRequired, |
| 68 | + fields: PropTypes.arrayOf(PropTypes.object), |
| 69 | + remove: PropTypes.func.isRequired, |
| 70 | + length: PropTypes.number, |
| 71 | + minItems: PropTypes.number, |
| 72 | + removeLabel: PropTypes.node.isRequired |
| 73 | +}; |
| 74 | + |
| 75 | +const defaultButtonLabels = { |
| 76 | + add: 'ADD', |
| 77 | + remove: 'REMOVE' |
| 78 | +}; |
| 79 | + |
| 80 | +const DynamicArray = ({ ...props }) => { |
| 81 | + const { |
| 82 | + arrayValidator, |
| 83 | + label, |
| 84 | + description, |
| 85 | + fields: formFields, |
| 86 | + defaultItem, |
| 87 | + meta, |
| 88 | + minItems, |
| 89 | + maxItems, |
| 90 | + noItemsMessage, |
| 91 | + FormFieldGridProps, |
| 92 | + FormControlProps, |
| 93 | + buttonLabels, |
| 94 | + ...rest |
| 95 | + } = useFieldApi(props); |
| 96 | + |
| 97 | + const combinedButtonLabels = { |
| 98 | + ...defaultButtonLabels, |
| 99 | + ...buttonLabels |
| 100 | + }; |
| 101 | + |
| 102 | + const classes = useFielArrayStyles(); |
| 103 | + |
| 104 | + const { dirty, submitFailed, error } = meta; |
| 105 | + const isError = (dirty || submitFailed) && error && typeof error === 'string'; |
| 106 | + |
| 107 | + return ( |
| 108 | + <FormFieldGrid {...FormFieldGridProps} className={clsx(classes.fieldArrayGroup, FormFieldGridProps.classname)}> |
| 109 | + <FormControl component="fieldset" error={isError} {...FormControlProps} className={clsx(classes.formControl, FormControlProps.className)}> |
| 110 | + <FieldArray key={rest.input.name} name={rest.input.name} validate={arrayValidator}> |
| 111 | + {({ fields: { map, value = [], push, remove } }) => ( |
| 112 | + <Grid container spacing={3}> |
| 113 | + {label && ( |
| 114 | + <Grid item xs={12} className={classes.header}> |
| 115 | + <Typography variant="h6" className={classes.label}> |
| 116 | + {label} |
| 117 | + </Typography> |
| 118 | + <Button color="primary" onClick={() => push(defaultItem)} disabled={value.length >= maxItems}> |
| 119 | + {combinedButtonLabels.add} |
| 120 | + </Button> |
| 121 | + </Grid> |
| 122 | + )} |
| 123 | + {description && ( |
| 124 | + <Grid item xs={12}> |
| 125 | + <Typography variant="subtitle1">{description}</Typography> |
| 126 | + </Grid> |
| 127 | + )} |
| 128 | + {value.length <= 0 && ( |
| 129 | + <Grid item xs={12}> |
| 130 | + <Typography variant="body1" gutterBottom className={classes.centerText}> |
| 131 | + {noItemsMessage} |
| 132 | + </Typography> |
| 133 | + </Grid> |
| 134 | + )} |
| 135 | + <Grid item xs={12}> |
| 136 | + {map((name, index) => ( |
| 137 | + <ArrayItem |
| 138 | + key={`${name}-${index}`} |
| 139 | + fields={formFields} |
| 140 | + name={name} |
| 141 | + fieldIndex={index} |
| 142 | + remove={remove} |
| 143 | + length={value.length} |
| 144 | + minItems={minItems} |
| 145 | + removeLabel={combinedButtonLabels.remove} |
| 146 | + /> |
| 147 | + ))} |
| 148 | + {isError && ( |
| 149 | + <Grid item xs={12}> |
| 150 | + <FormHelperText>{error}</FormHelperText> |
| 151 | + </Grid> |
| 152 | + )} |
| 153 | + </Grid> |
| 154 | + </Grid> |
| 155 | + )} |
| 156 | + </FieldArray> |
| 157 | + </FormControl> |
| 158 | + </FormFieldGrid> |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +DynamicArray.propTypes = { |
| 163 | + label: PropTypes.node, |
| 164 | + description: PropTypes.node, |
| 165 | + fields: PropTypes.arrayOf(PropTypes.object).isRequired, |
| 166 | + defaultItem: PropTypes.any, |
| 167 | + minItems: PropTypes.number, |
| 168 | + maxItems: PropTypes.number, |
| 169 | + noItemsMessage: PropTypes.node, |
| 170 | + FormControlProps: PropTypes.object, |
| 171 | + FormFieldGridProps: PropTypes.object, |
| 172 | + buttonLabels: PropTypes.object |
| 173 | +}; |
| 174 | + |
| 175 | +DynamicArray.defaultProps = { |
| 176 | + maxItems: Infinity, |
| 177 | + minItems: 0, |
| 178 | + noItemsMessage: 'No items added', |
| 179 | + FormControlProps: {}, |
| 180 | + FormFieldGridProps: {} |
| 181 | +}; |
| 182 | + |
| 183 | +export default DynamicArray; |
0 commit comments