-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathSelectInput.tsx
316 lines (299 loc) · 9.11 KB
/
SelectInput.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import * as React from 'react';
import { useCallback, FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import MenuItem from '@material-ui/core/MenuItem';
import { TextFieldProps } from '@material-ui/core/TextField';
import { makeStyles } from '@material-ui/core/styles';
import {
useInput,
FieldTitle,
useTranslate,
ChoicesInputProps,
useChoices,
warning,
} from 'ra-core';
import ResettableTextField from './ResettableTextField';
import InputHelperText from './InputHelperText';
import sanitizeInputRestProps from './sanitizeInputRestProps';
import Labeled from './Labeled';
import { LinearProgress } from '../layout';
const sanitizeRestProps = ({
addLabel,
afterSubmit,
allowNull,
beforeSubmit,
choices,
className,
crudGetMatching,
crudGetOne,
data,
filter,
filterToQuery,
formatOnBlur,
isEqual,
limitChoicesToValue,
multiple,
name,
pagination,
perPage,
ref,
reference,
render,
setFilter,
setPagination,
setSort,
sort,
subscription,
type,
validateFields,
validation,
value,
...rest
}: any) => sanitizeInputRestProps(rest);
const useStyles = makeStyles(
theme => ({
input: {
minWidth: theme.spacing(20),
},
}),
{ name: 'RaSelectInput' }
);
/**
* An Input component for a select box, using an array of objects for the options
*
* Pass possible options as an array of objects in the 'choices' attribute.
*
* By default, the options are built from:
* - the 'id' property as the option value,
* - the 'name' property as the option text
* @example
* const choices = [
* { id: 'M', name: 'Male' },
* { id: 'F', name: 'Female' },
* ];
* <SelectInput source="gender" choices={choices} />
*
* You can also customize the properties to use for the option name and value,
* thanks to the 'optionText' and 'optionValue' attributes.
* @example
* const choices = [
* { _id: 123, full_name: 'Leo Tolstoi', sex: 'M' },
* { _id: 456, full_name: 'Jane Austen', sex: 'F' },
* ];
* <SelectInput source="author_id" choices={choices} optionText="full_name" optionValue="_id" />
*
* `optionText` also accepts a function, so you can shape the option text at will:
* @example
* const choices = [
* { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
* { id: 456, first_name: 'Jane', last_name: 'Austen' },
* ];
* const optionRenderer = choice => `${choice.first_name} ${choice.last_name}`;
* <SelectInput source="author_id" choices={choices} optionText={optionRenderer} />
*
* `optionText` also accepts a React Element, that will be cloned and receive
* the related choice as the `record` prop. You can use Field components there.
* @example
* const choices = [
* { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
* { id: 456, first_name: 'Jane', last_name: 'Austen' },
* ];
* const FullNameField = ({ record }) => <span>{record.first_name} {record.last_name}</span>;
* <SelectInput source="gender" choices={choices} optionText={<FullNameField />}/>
*
* The choices are translated by default, so you can use translation identifiers as choices:
* @example
* const choices = [
* { id: 'M', name: 'myroot.gender.male' },
* { id: 'F', name: 'myroot.gender.female' },
* ];
*
* However, in some cases (e.g. inside a `<ReferenceInput>`), you may not want
* the choice to be translated. In that case, set the `translateChoice` prop to false.
* @example
* <SelectInput source="gender" choices={choices} translateChoice={false}/>
*
* The object passed as `options` props is passed to the material-ui <Select> component
*
* You can disable some choices by providing a `disableValue` field which name is `disabled` by default
* @example
* const choices = [
* { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
* { id: 456, first_name: 'Jane', last_name: 'Austen' },
* { id: 976, first_name: 'William', last_name: 'Rinkerd', disabled: true },
* ];
*
* @example
* const choices = [
* { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
* { id: 456, first_name: 'Jane', last_name: 'Austen' },
* { id: 976, first_name: 'William', last_name: 'Rinkerd', not_available: true },
* ];
* <SelectInput source="gender" choices={choices} disableValue="not_available" />
*
*/
const SelectInput: FunctionComponent<SelectInputProps> = props => {
const {
allowEmpty,
choices = [],
classes: classesOverride,
className,
disableValue,
emptyText,
emptyValue,
format,
helperText,
label,
loaded,
loading,
onBlur,
onChange,
onFocus,
options,
optionText,
optionValue,
parse,
resource,
source,
translateChoice,
validate,
...rest
} = props;
const translate = useTranslate();
const classes = useStyles(props);
warning(
source === undefined,
`If you're not wrapping the SelectInput inside a ReferenceInput, you must provide the source prop`
);
warning(
choices === undefined,
`If you're not wrapping the SelectInput inside a ReferenceInput, you must provide the choices prop`
);
const { getChoiceText, getChoiceValue } = useChoices({
optionText,
optionValue,
translateChoice,
});
const { id, input, isRequired, meta } = useInput({
format,
onBlur,
onChange,
onFocus,
parse,
resource,
source,
validate,
...rest,
});
const { touched, error, submitError } = meta;
const renderEmptyItemOption = useCallback(() => {
return React.isValidElement(emptyText)
? React.cloneElement(emptyText)
: emptyText === ''
? ' ' // em space, forces the display of an empty line of normal height
: translate(emptyText, { _: emptyText });
}, [emptyText, translate]);
const renderMenuItemOption = useCallback(choice => getChoiceText(choice), [
getChoiceText,
]);
if (loading) {
return (
<Labeled
id={id}
label={label}
source={source}
resource={resource}
className={className}
isRequired={isRequired}
meta={meta}
input={input}
>
<LinearProgress />
</Labeled>
);
}
return (
<ResettableTextField
id={id}
{...input}
select
label={
label !== '' &&
label !== false && (
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
/>
)
}
className={`${classes.input} ${className}`}
clearAlwaysVisible
error={!!(touched && (error || submitError))}
helperText={
<InputHelperText
touched={touched}
error={error || submitError}
helperText={helperText}
/>
}
{...options}
{...sanitizeRestProps(rest)}
>
{allowEmpty ? (
<MenuItem
value={emptyValue}
key="null"
aria-label={translate('ra.action.clear_input_value')}
title={translate('ra.action.clear_input_value')}
>
{renderEmptyItemOption()}
</MenuItem>
) : null}
{choices.map(choice => (
<MenuItem
key={getChoiceValue(choice)}
value={getChoiceValue(choice)}
disabled={get(choice, disableValue)}
>
{renderMenuItemOption(choice)}
</MenuItem>
))}
</ResettableTextField>
);
};
SelectInput.propTypes = {
allowEmpty: PropTypes.bool,
emptyText: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
emptyValue: PropTypes.any,
choices: PropTypes.arrayOf(PropTypes.object),
classes: PropTypes.object,
className: PropTypes.string,
label: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
options: PropTypes.object,
optionText: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.element,
]).isRequired,
optionValue: PropTypes.string.isRequired,
disableValue: PropTypes.string,
resettable: PropTypes.bool,
resource: PropTypes.string,
source: PropTypes.string,
translateChoice: PropTypes.bool,
};
SelectInput.defaultProps = {
emptyText: '',
emptyValue: '',
options: {},
optionText: 'name',
optionValue: 'id',
translateChoice: true,
disableValue: 'disabled',
};
export type SelectInputProps = ChoicesInputProps<TextFieldProps> &
Omit<TextFieldProps, 'label' | 'helperText'>;
export default SelectInput;