-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Array.tsx
230 lines (209 loc) · 6.2 KB
/
Array.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import React, { useEffect, useReducer, useCallback, useState } from 'react';
import { DragDropContext, Droppable } from 'react-beautiful-dnd';
import withCondition from '../../withCondition';
import Button from '../../../elements/Button';
import DraggableSection from '../../DraggableSection';
import reducer from '../rowReducer';
import { useForm } from '../../Form/context';
import buildStateFromSchema from '../../Form/buildStateFromSchema';
import useFieldType from '../../useFieldType';
import Error from '../../Error';
import { array } from '../../../../../fields/validations';
import Banner from '../../../elements/Banner';
import { Props, RenderArrayProps } from './types';
import './index.scss';
const baseClass = 'field-type array';
const ArrayFieldType: React.FC<Props> = (props) => {
const {
label,
name,
path: pathFromProps,
fields,
fieldTypes,
validate = array,
required,
maxRows,
minRows,
labels = {
singular: 'Row',
plural: 'Rows',
},
permissions,
admin: {
readOnly,
},
} = props;
const [rows, dispatchRows] = useReducer(reducer, []);
const { getDataByPath, dispatchFields } = useForm();
const path = pathFromProps || name;
const memoizedValidate = useCallback((value) => {
const validationResult = validate(value, { minRows, maxRows, required });
return validationResult;
}, [validate, maxRows, minRows, required]);
const [disableFormData, setDisableFormData] = useState(false);
const {
showError,
errorMessage,
value,
setValue,
} = useFieldType({
path,
validate: memoizedValidate,
disableFormData,
ignoreWhileFlattening: true,
});
const addRow = useCallback(async (rowIndex) => {
const subFieldState = await buildStateFromSchema(fields);
dispatchFields({ type: 'ADD_ROW', rowIndex, subFieldState, path });
dispatchRows({ type: 'ADD', rowIndex });
setValue(value as number + 1);
}, [dispatchRows, dispatchFields, fields, path, setValue, value]);
const removeRow = useCallback((rowIndex) => {
dispatchRows({ type: 'REMOVE', rowIndex });
dispatchFields({ type: 'REMOVE_ROW', rowIndex, path });
}, [dispatchRows, dispatchFields, path]);
const moveRow = useCallback((moveFromIndex, moveToIndex) => {
dispatchRows({ type: 'MOVE', moveFromIndex, moveToIndex });
dispatchFields({ type: 'MOVE_ROW', moveFromIndex, moveToIndex, path });
}, [dispatchRows, dispatchFields, path]);
const onDragEnd = useCallback((result) => {
if (!result.destination) return;
const sourceIndex = result.source.index;
const destinationIndex = result.destination.index;
moveRow(sourceIndex, destinationIndex);
}, [moveRow]);
useEffect(() => {
const data = getDataByPath(path);
dispatchRows({ type: 'SET_ALL', data: data || [] });
}, [getDataByPath, path]);
useEffect(() => {
setValue(rows?.length || 0);
if (rows?.length === 0) {
setDisableFormData(false);
} else {
setDisableFormData(true);
}
}, [rows, setValue]);
return (
<RenderArray
onDragEnd={onDragEnd}
label={label}
showError={showError}
errorMessage={errorMessage}
rows={rows}
labels={labels}
addRow={addRow}
removeRow={removeRow}
moveRow={moveRow}
path={path}
name={name}
fieldTypes={fieldTypes}
fields={fields}
permissions={permissions}
value={value as number}
readOnly={readOnly}
minRows={minRows}
maxRows={maxRows}
required={required}
/>
);
};
const RenderArray = React.memo((props: RenderArrayProps) => {
const {
onDragEnd,
label,
showError,
errorMessage,
rows,
labels,
addRow,
removeRow,
moveRow,
path,
fieldTypes,
fields,
permissions,
value,
readOnly,
minRows,
maxRows,
required,
} = props;
const hasMaxRows = maxRows && rows.length >= maxRows;
return (
<DragDropContext onDragEnd={onDragEnd}>
<div
className={baseClass}
>
<header className={`${baseClass}__header`}>
<h3>{label}</h3>
<Error
showError={showError}
message={errorMessage}
/>
</header>
<Droppable droppableId="array-drop">
{(provided) => (
<div
ref={provided.innerRef}
{...provided.droppableProps}
>
{rows.length > 0 && rows.map((row, i) => (
<DraggableSection
readOnly={readOnly}
key={row.key}
id={row.key}
blockType="array"
label={label}
isOpen={row.open}
rowCount={rows.length}
rowIndex={i}
addRow={addRow}
removeRow={removeRow}
moveRow={moveRow}
parentPath={path}
fieldTypes={fieldTypes}
fieldSchema={fields}
permissions={permissions}
hasMaxRows={hasMaxRows}
/>
))}
{(rows.length < minRows || (required && rows.length === 0)) && (
<Banner type="error">
This field requires at least
{' '}
{minRows
? `${minRows} ${labels.plural}`
: `1 ${labels.singular}`}
</Banner>
)}
{(rows.length === 0 && readOnly) && (
<Banner>
This field has no
{' '}
{labels.plural}
.
</Banner>
)}
{provided.placeholder}
</div>
)}
</Droppable>
{(!readOnly && (!hasMaxRows)) && (
<div className={`${baseClass}__add-button-wrap`}>
<Button
onClick={() => addRow(value)}
buttonStyle="icon-label"
icon="plus"
iconStyle="with-border"
iconPosition="left"
>
{`Add ${labels.singular}`}
</Button>
</div>
)}
</div>
</DragDropContext>
);
});
export default withCondition(ArrayFieldType);