/
SelectFieldBinding.tsx
116 lines (103 loc) · 3.88 KB
/
SelectFieldBinding.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
import React from "react";
import {
FormControl,
FormHelperText,
InputLabel,
MenuItem,
Select as MuiSelect
} from "@mui/material";
import { EnumType, FieldProps } from "../../models";
import { FieldDescription } from "../index";
import { LabelWithIcon } from "../components";
import { useClearRestoreValue } from "../../hooks";
import { isEnumValueDisabled } from "../../core/util/enums";
import { EnumValuesChip } from "../../preview";
type SelectProps<T extends EnumType> = FieldProps<T>;
/**
* If `enumValues` are set in the string config, this field renders a select
* where each option is a colored chip.
*
* This is one of the internal components that get mapped natively inside forms
* and tables to the specified properties.
* @category Form fields
*/
export function SelectFieldBinding<T extends EnumType>({
propertyKey,
value,
setValue,
error,
showError,
disabled,
autoFocus,
touched,
property,
includeDescription,
shouldAlwaysRerender
}: SelectProps<T>) {
const enumValues = property.enumValues;
useClearRestoreValue({
property,
value,
setValue
});
return (
<FormControl
fullWidth
required={property.validation?.required}
error={showError}
disabled={disabled}
sx={{
"& .MuiInputLabel-root": {
mt: 1 / 2,
ml: 1 / 2
},
"& .MuiInputLabel-shrink": {
mt: 2
}
}}
>
<InputLabel id={`${propertyKey}-select-label`}>
<LabelWithIcon property={property}/>
</InputLabel>
<MuiSelect
sx={{
minHeight: "64px"
}}
variant={"filled"}
labelId={`${propertyKey}-select-label`}
autoFocus={autoFocus}
value={value ?? ""}
disabled={disabled}
onChange={(evt: any) => {
const eventValue = evt.target.value;
const newValue = eventValue
? (property.dataType === "number" ? parseFloat(eventValue) : eventValue)
: null;
return setValue(newValue);
}}
renderValue={(enumKey: any) => {
return <EnumValuesChip
enumKey={enumKey}
enumValues={enumValues}
small={false}/>;
}
}>
{enumValues && enumValues.map((enumConfig) => {
return (
<MenuItem key={`select_${propertyKey}_${enumConfig.id}`}
value={enumConfig.id}
disabled={isEnumValueDisabled(enumConfig)}>
<EnumValuesChip
enumKey={enumConfig.id}
enumValues={enumValues}
small={true}/>
</MenuItem>
);
})}
</MuiSelect>
{includeDescription &&
<FieldDescription property={property}/>}
{showError && <FormHelperText>{error}</FormHelperText>}
</FormControl>
);
}