-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathSelectWidget.tsx
130 lines (118 loc) · 3.41 KB
/
SelectWidget.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
import React from "react";
import Form from "react-bootstrap/Form";
import { WidgetProps } from "@rjsf/core";
import { utils } from "@rjsf/core";
const { asNumber, guessType } = utils;
const nums = new Set(["number", "integer"]);
/**
* This is a silly limitation in the DOM where option change event values are
* always retrieved as strings.
*/
const processValue = (schema: any, value: any) => {
// "enum" is a reserved word, so only "type" and "items" can be destructured
const { type, items } = schema;
if (value === "") {
return undefined;
} else if (type === "array" && items && nums.has(items.type)) {
return value.map(asNumber);
} else if (type === "boolean") {
return value === "true";
} else if (type === "number") {
return asNumber(value);
}
// If type is undefined, but an enum is present, try and infer the type from
// the enum values
if (schema.enum) {
if (schema.enum.every((x: any) => guessType(x) === "number")) {
return asNumber(value);
} else if (schema.enum.every((x: any) => guessType(x) === "boolean")) {
return value === "true";
}
}
return value;
};
const SelectWidget = ({
schema,
id,
options,
label,
required,
disabled,
readonly,
value,
multiple,
autofocus,
onChange,
onBlur,
onFocus,
placeholder,
rawErrors = [],
}: WidgetProps) => {
const { enumOptions, enumDisabled } = options;
const emptyValue = multiple ? [] : "";
function getValue(
event: React.FocusEvent | React.ChangeEvent | any,
multiple: Boolean
) {
if (multiple) {
return [].slice
.call(event.target.options as any)
.filter((o: any) => o.selected)
.map((o: any) => o.value);
} else {
return event.target.value;
}
}
return (
<Form.Group>
<Form.Label className={rawErrors.length > 0 ? "text-danger" : ""}>
{label || schema.title}
{(label || schema.title) && required ? "*" : null}
</Form.Label>
<Form.Control
as="select"
custom
id={id}
value={typeof value === "undefined" ? emptyValue : value}
required={required}
multiple={multiple}
disabled={disabled}
readOnly={readonly}
autoFocus={autofocus}
className={rawErrors.length > 0 ? "is-invalid" : ""}
onBlur={
onBlur &&
((event: React.FocusEvent) => {
const newValue = getValue(event, multiple);
onBlur(id, processValue(schema, newValue));
})
}
onFocus={
onFocus &&
((event: React.FocusEvent) => {
const newValue = getValue(event, multiple);
onFocus(id, processValue(schema, newValue));
})
}
onChange={(event: React.ChangeEvent) => {
const newValue = getValue(event, multiple);
onChange(processValue(schema, newValue));
}}>
{!multiple && schema.default === undefined && (
<option value="">{placeholder}</option>
)}
{(enumOptions as any).map(({ value, label }: any, i: number) => {
const disabled: any =
Array.isArray(enumDisabled) &&
(enumDisabled as any).indexOf(value) != -1;
return (
<option key={i} id={label} value={value} disabled={disabled}>
{label}
</option>
);
})}
</Form.Control>
</Form.Group>
);
};
export default SelectWidget;