-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectField.js
More file actions
150 lines (143 loc) · 4.21 KB
/
SelectField.js
File metadata and controls
150 lines (143 loc) · 4.21 KB
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
/* eslint-disable no-unused-vars */
import * as React from 'react';
import FormControl from '@material-ui/core/FormControl';
import FormHelperText from '@material-ui/core/FormHelperText';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import MUISelect from '@material-ui/core/Select';
import { withStyles } from '@material-ui/core/styles';
import { ArrowDropDown, Lock } from '@material-ui/icons';
import isMatch from 'lodash.ismatch';
import { CheckboxField } from '@vidispine/vdt-materialui';
function HelperText({ children }) {
return children && <FormHelperText error>{children}</FormHelperText>;
}
export function MatchSource({ onChange, checked, children }) {
return (
<>
{children && <FormHelperText error>{children}</FormHelperText>}
<CheckboxField
input={{ onChange, checked }}
color="primary"
size="small"
label="Match source"
/>
</>
);
}
const styles = (theme) => ({
root: {
marginTop: theme.spacing(1),
width: '100%',
'& .MuiInputBase-root': {
fontSize: '.75rem',
},
'& .MuiInputBase-input': {
fontWeight: 500,
fontSize: '0.875rem',
},
},
inputLabel: {},
select: {},
menuItem: {},
formHelperText: {},
});
const defaultParseOptions = (opts) =>
opts.map(({ value: val, ...rest }) => ({
value: val,
...rest,
...(val !== 0 &&
val !== 'nooptions' && {
value: JSON.stringify(val),
}),
}));
function SelectField({
input: { value, name, onChange, multiple, ...inputProps } = {},
meta: { touched, error, submitError } = {},
options = [],
dependency,
match = false,
label,
classes,
className,
disabled = false,
FormControlProps = {},
InputLabelProps = {},
MenuItemProps = {},
FormHelperTextProps = {},
parseOptions = defaultParseOptions,
...props
}) {
const [opts, setOpts] = React.useState(parseOptions(options));
const [checked, setChecked] = React.useState(false);
React.useEffect(() => {
const filter = parseOptions(options);
setOpts(filter);
if (value && !filter.some(({ value: val }) => val === value))
onChange({ target: { value: 0 } });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [options]);
const onToggle = ({ target: { checked: bool } }) => {
setChecked(bool);
if (bool) onChange({ target: { value: 0 } });
};
React.useEffect(() => {
if (checked) onChange({ target: { value: 0 } });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [checked]);
return (
<FormControl
error={(error || submitError) && touched}
disabled={disabled || checked}
className={classes.root}
// eslint-disable-next-line react/jsx-props-no-spreading
{...FormControlProps}
>
<InputLabel
htmlFor={name}
className={classes.inputLabel}
// eslint-disable-next-line react/jsx-props-no-spreading
{...InputLabelProps}
>
{label}
</InputLabel>
<MUISelect
name={name}
onChange={onChange}
value={value}
multiple={multiple}
className={classes.select}
inputProps={inputProps}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
>
{opts.map(({ value: optionValue, label: optionLabel, ...optionProps }) => {
return (
<MenuItem
key={optionValue}
value={optionValue}
className={classes.menuItem}
// eslint-disable-next-line react/jsx-props-no-spreading
{...MenuItemProps}
// eslint-disable-next-line react/jsx-props-no-spreading
{...optionProps}
>
{optionLabel}
</MenuItem>
);
})}
</MUISelect>
{match && (
<MatchSource onChange={onToggle} checked={checked}>
{touched && (error || submitError) ? error || submitError : ''}
</MatchSource>
)}
{!match && (
<HelperText className={classes.formHelperText}>
{touched && (error || submitError) ? error || submitError : ''}
</HelperText>
)}
</FormControl>
);
}
export default withStyles(styles)(SelectField);