-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
SelectWidget.js
138 lines (126 loc) · 3.47 KB
/
SelectWidget.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
/* eslint-disable react/prop-types */
import React from "react";
import _ from "lodash";
import { Form } from "semantic-ui-react";
import { utils } from '@rjsf/core';
import PropTypes from "prop-types";
import { getSemanticProps } from "../util";
const { asNumber, guessType } = utils;
const nums = new Set(["number", "integer"]);
/**
* * Returns and creates an array format required for semantic drop down
* @param {array} enumOptions- array of items for the dropdown
* @param {array} enumDisabled - array of enum option values to disable
* @returns {*}
*/
function createDefaultValueOptionsForDropDown(enumOptions, enumDisabled) {
const disabledOptions = enumDisabled || [];
let options = [];
// eslint-disable-next-line no-shadow
options = _.map(enumOptions, ({ label, value }) => ({
disabled: disabledOptions.indexOf(value) !== -1,
key: label,
text: label,
value,
}));
return options;
}
/**
* This is a silly limitation in the DOM where option change event values are
* always retrieved as strings.
*/
const processValue = (schema, value) => {
// "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" || 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 => guessType(x) === "number")) {
return asNumber(value);
} else if (schema.enum.every(x => guessType(x) === "boolean")) {
return value === "true";
}
}
return value;
};
function SelectWidget(props) {
const {
schema,
id,
label,
options,
name,
required,
disabled,
readonly,
value,
multiple,
placeholder,
autofocus,
onChange,
onBlur,
onFocus,
} = props;
const semanticProps = getSemanticProps({ options });
const { enumDisabled, enumOptions } = options;
const emptyValue = multiple ? [] : "";
const dropdownOptions = createDefaultValueOptionsForDropDown(
enumOptions,
enumDisabled
);
const _onChange = (
event,
// eslint-disable-next-line no-shadow
{ value }
) => onChange && onChange(processValue(schema, value));
// eslint-disable-next-line no-shadow
const _onBlur = ({ target: { value } }) =>
onBlur && onBlur(id, processValue(schema, value));
const _onFocus = ({
// eslint-disable-next-line no-shadow
target: { value },
}) => onFocus && onFocus(id, processValue(schema, value));
return (
<Form.Dropdown
key={id}
name={name}
label={label || schema.title}
multiple={typeof multiple === "undefined" ? false : multiple}
value={typeof value === "undefined" ? emptyValue : value}
disabled={disabled}
placeholder={placeholder}
{...semanticProps}
required={required}
autoFocus={autofocus}
readOnly={readonly}
options={dropdownOptions}
onChange={_onChange}
onBlur={_onBlur}
onFocus={_onFocus}
/>
);
}
SelectWidget.defaultProps = {
options: {
semantic: {
inverted: "false",
fluid: true,
selection: true,
scrolling: true,
upward: false,
},
},
};
SelectWidget.propTypes = {
options: PropTypes.object,
};
export default SelectWidget;