-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathSimpleFormIteratorItem.tsx
182 lines (174 loc) · 7.19 KB
/
SimpleFormIteratorItem.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
import * as React from 'react';
import {
Children,
cloneElement,
MouseEvent,
MouseEventHandler,
isValidElement,
ReactElement,
ReactNode,
useMemo,
} from 'react';
import { Typography } from '@material-ui/core';
import classNames from 'classnames';
import { Record } from 'ra-core';
import { ClassesOverride } from '../../types';
import FormInput from '../../form/FormInput';
import { useSimpleFormIteratorStyles } from './useSimpleFormIteratorStyles';
import { useSimpleFormIterator } from './useSimpleFormIterator';
import { ArrayInputContextValue } from './ArrayInputContext';
import {
SimpleFormIteratorItemContext,
SimpleFormIteratorItemContextValue,
} from './SimpleFormIteratorItemContext';
export const SimpleFormIteratorItem = React.forwardRef(
(props: SimpleFormIteratorItemProps, ref: any) => {
const {
basePath,
children,
classes,
disabled,
disableReordering,
disableRemove,
getItemLabel,
index,
margin,
member,
record,
removeButton,
reOrderButtons,
resource,
source,
variant,
} = props;
const { total, reOrder, remove } = useSimpleFormIterator();
// Returns a boolean to indicate whether to disable the remove button for certain fields.
// If disableRemove is a function, then call the function with the current record to
// determining if the button should be disabled. Otherwise, use a boolean property that
// enables or disables the button for all of the fields.
const disableRemoveField = (record: Record) => {
if (typeof disableRemove === 'boolean') {
return disableRemove;
}
return disableRemove && disableRemove(record);
};
// remove field and call the onClick event of the button passed as removeButton prop
const handleRemoveButtonClick = (
originalOnClickHandler: MouseEventHandler,
index: number
) => (event: MouseEvent) => {
remove(index);
if (originalOnClickHandler) {
originalOnClickHandler(event);
}
};
const context = useMemo<SimpleFormIteratorItemContextValue>(
() => ({
index,
total,
reOrder: newIndex => reOrder(index, newIndex),
remove: () => remove(index),
}),
[index, total, reOrder, remove]
);
return (
<SimpleFormIteratorItemContext.Provider value={context}>
<li className={classes.line} ref={ref}>
<div>
<div className={classes.indexContainer}>
<Typography
variant="body1"
className={classes.index}
>
{getItemLabel(index)}
</Typography>
{!disabled &&
!disableReordering &&
cloneElement(reOrderButtons, {
index,
max: total,
reOrder,
className: classNames(
'button-reorder',
`button-reorder-${source}-${index}`
),
})}
</div>
</div>
<section className={classes.form}>
{Children.map(
children,
(input: ReactElement, index2) => {
if (!isValidElement<any>(input)) {
return null;
}
const { source, ...inputProps } = input.props;
return (
<FormInput
basePath={
input.props.basePath || basePath
}
input={cloneElement(input, {
source: source
? `${member}.${source}`
: member,
index: source ? undefined : index2,
label:
typeof input.props.label ===
'undefined'
? source
? `resources.${resource}.fields.${source}`
: undefined
: input.props.label,
disabled,
...inputProps,
})}
record={record}
resource={resource}
variant={variant}
margin={margin}
/>
);
}
)}
</section>
{!disabled && !disableRemoveField(record) && (
<span className={classes.action}>
{cloneElement(removeButton, {
onClick: handleRemoveButtonClick(
removeButton.props.onClick,
index
),
className: classNames(
'button-remove',
`button-remove-${source}-${index}`
),
})}
</span>
)}
</li>
</SimpleFormIteratorItemContext.Provider>
);
}
);
export type DisableRemoveFunction = (record: Record) => boolean;
export type SimpleFormIteratorItemProps = ArrayInputContextValue & {
basePath: string;
children?: ReactNode;
classes?: ClassesOverride<typeof useSimpleFormIteratorStyles>;
disabled?: boolean;
disableRemove?: boolean | DisableRemoveFunction;
disableReordering?: boolean;
getItemLabel?: (index: number) => string;
index: number;
margin?: 'none' | 'normal' | 'dense';
member: string;
onRemoveField: (index: number) => void;
onReorder: (origin: number, destination: number) => void;
record: Record;
removeButton?: ReactElement;
reOrderButtons?: ReactElement;
resource: string;
source: string;
variant?: 'standard' | 'outlined' | 'filled';
};