-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectField.js
250 lines (250 loc) · 6.94 KB
/
SelectField.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Checkbox_1 = tslib_1.__importDefault(require("@material-ui/core/Checkbox"));
var FormControlLabel_1 = tslib_1.__importDefault(
require("@material-ui/core/FormControlLabel")
);
var FormGroup_1 = tslib_1.__importDefault(
require("@material-ui/core/FormGroup")
);
var FormLabel_1 = tslib_1.__importDefault(
require("@material-ui/core/FormLabel")
);
var MenuItem_1 = tslib_1.__importDefault(require("@material-ui/core/MenuItem"));
var Radio_1 = tslib_1.__importDefault(require("@material-ui/core/Radio"));
var RadioGroup_1 = tslib_1.__importDefault(
require("@material-ui/core/RadioGroup")
);
var react_1 = tslib_1.__importDefault(require("react"));
var Switch_1 = tslib_1.__importDefault(require("@material-ui/core/Switch"));
var TextField_1 = tslib_1.__importDefault(
require("@material-ui/core/TextField")
);
var uniforms_1 = require("uniforms");
var wrapField_1 = tslib_1.__importDefault(require("./wrapField"));
var xor = function(item, array) {
var index = array.indexOf(item);
if (index === -1) {
return array.concat([item]);
}
return array.slice(0, index).concat(array.slice(index + 1));
};
var renderSelect = function(_a) {
var InputLabelProps = _a.InputLabelProps,
allowedValues = _a.allowedValues,
disabled = _a.disabled,
error = _a.error,
errorMessage = _a.errorMessage,
fieldType = _a.fieldType,
fullWidth = _a.fullWidth,
helperText = _a.helperText,
id = _a.id,
inputProps = _a.inputProps,
label = _a.label,
labelProps = _a.labelProps,
margin = _a.margin,
name = _a.name,
native = _a.native,
onChange = _a.onChange,
placeholder = _a.placeholder,
required = _a.required,
showInlineError = _a.showInlineError,
transform = _a.transform,
value = _a.value,
variant = _a.variant,
props = tslib_1.__rest(_a, [
"InputLabelProps",
"allowedValues",
"disabled",
"error",
"errorMessage",
"fieldType",
"fullWidth",
"helperText",
"id",
"inputProps",
"label",
"labelProps",
"margin",
"name",
"native",
"onChange",
"placeholder",
"required",
"showInlineError",
"transform",
"value",
"variant"
]);
var Item = native ? "option" : MenuItem_1.default;
var hasPlaceholder = !!placeholder;
var hasValue = value !== "" && value !== undefined;
return react_1.default.createElement(
TextField_1.default,
{
disabled: !!disabled,
error: !!error,
fullWidth: fullWidth,
helperText: (error && showInlineError && errorMessage) || helperText,
InputLabelProps: tslib_1.__assign(
{ shrink: !!label && (hasPlaceholder || hasValue) },
labelProps,
InputLabelProps
),
label: label,
margin: margin,
onChange: function(event) {
return (
disabled ||
onChange(event.target.value !== "" ? event.target.value : undefined)
);
},
required: required,
select: true,
SelectProps: tslib_1.__assign(
{
displayEmpty: hasPlaceholder,
inputProps: tslib_1.__assign({ name: name, id: id }, inputProps),
multiple: fieldType === Array || undefined,
native: native
},
uniforms_1.filterDOMProps(props)
),
value: native && !value ? "" : value,
variant: variant
},
hasPlaceholder &&
react_1.default.createElement(
Item,
{ value: "", disabled: !!required },
placeholder || label
),
allowedValues.map(function(value) {
return react_1.default.createElement(
Item,
{ key: value, value: value },
transform ? transform(value) : value
);
})
);
};
var renderCheckboxes = function(_a) {
var allowedValues = _a.allowedValues,
appearance = _a.appearance,
disabled = _a.disabled,
error = _a.error,
errorMessage = _a.errorMessage,
fieldType = _a.fieldType,
id = _a.id,
inputRef = _a.inputRef,
label = _a.label,
legend = _a.legend,
name = _a.name,
onChange = _a.onChange,
showInlineError = _a.showInlineError,
transform = _a.transform,
value = _a.value,
props = tslib_1.__rest(_a, [
"allowedValues",
"appearance",
"disabled",
"error",
"errorMessage",
"fieldType",
"id",
"inputRef",
"label",
"legend",
"name",
"onChange",
"showInlineError",
"transform",
"value"
]);
var children;
var filteredProps = wrapField_1.default._filterDOMProps(
uniforms_1.filterDOMProps(props)
);
if (fieldType !== Array) {
children = react_1.default.createElement(
RadioGroup_1.default,
{
id: id,
name: name,
onChange: function(event) {
return disabled || onChange(event.target.value);
},
ref: inputRef,
value: value
},
allowedValues.map(function(item) {
return react_1.default.createElement(FormControlLabel_1.default, {
control: react_1.default.createElement(
Radio_1.default,
tslib_1.__assign({ id: id + "-" + item }, filteredProps)
),
key: item,
label: transform ? transform(item) : item,
value: item
});
})
);
} else {
var SelectionControl_1 =
appearance === "checkbox" ? Checkbox_1.default : Switch_1.default;
children = react_1.default.createElement(
FormGroup_1.default,
{ id: id },
allowedValues.map(function(item) {
return react_1.default.createElement(FormControlLabel_1.default, {
control: react_1.default.createElement(
SelectionControl_1,
tslib_1.__assign(
{
checked: value.includes(item),
id: id + "-" + item,
name: name,
onChange: function() {
return disabled || onChange(xor(item, value));
},
ref: inputRef,
value: props.name
},
filteredProps
)
),
key: item,
label: transform ? transform(item) : item
});
})
);
}
return wrapField_1.default(
tslib_1.__assign({}, props, {
component: "fieldset",
disabled: disabled,
error: error,
errorMessage: errorMessage,
showInlineError: showInlineError
}),
(legend || label) &&
react_1.default.createElement(
FormLabel_1.default,
{ component: "legend" },
legend || label
),
children
);
};
var Select = function(_a) {
var checkboxes = _a.checkboxes,
props = tslib_1.__rest(_a, ["checkboxes"]);
return checkboxes ? renderCheckboxes(props) : renderSelect(props);
};
Select.defaultProps = {
appearance: "checkbox",
fullWidth: true,
margin: "dense"
};
exports.default = uniforms_1.connectField(Select);