-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
ConstraintOperatorSelect.tsx
148 lines (134 loc) · 4.07 KB
/
ConstraintOperatorSelect.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
Select,
MenuItem,
FormControl,
InputLabel,
SelectChangeEvent,
styled,
} from '@mui/material';
import {
Operator,
stringOperators,
semVerOperators,
dateOperators,
numOperators,
inOperators,
} from 'constants/operators';
import React, { useState } from 'react';
import { formatOperatorDescription } from 'component/common/ConstraintAccordion/ConstraintOperator/formatOperatorDescription';
interface IConstraintOperatorSelectProps {
options: Operator[];
value: Operator;
onChange: (value: Operator) => void;
}
const StyledValueContainer = styled('div')(({ theme }) => ({
lineHeight: 1.1,
marginTop: -2,
marginBottom: -10,
}));
const StyledLabel = styled('div')(({ theme }) => ({
fontSize: theme.fontSizes.smallBody,
}));
const StyledDescription = styled('div')(({ theme }) => ({
fontSize: theme.fontSizes.smallerBody,
color: theme.palette.neutral.main,
overflow: 'hidden',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
}));
const StyledFormInput = styled(FormControl)(({ theme }) => ({
[theme.breakpoints.between(1101, 1365)]: {
width: '170px',
marginRight: theme.spacing(0.5),
},
}));
const StyledMenuItem = styled(MenuItem, {
shouldForwardProp: prop => prop !== 'separator',
})<{ separator: boolean }>(({ theme, separator }) =>
separator
? {
position: 'relative',
overflow: 'visible',
marginTop: theme.spacing(2),
'&:before': {
content: '""',
display: 'block',
position: 'absolute',
top: theme.spacing(-1),
left: 0,
right: 0,
borderTop: '1px solid',
borderTopColor: theme.palette.tertiary.contrast,
},
}
: {}
);
const StyledOptionContainer = styled('div')(({ theme }) => ({
lineHeight: 1.2,
}));
export const ConstraintOperatorSelect = ({
options,
value,
onChange,
}: IConstraintOperatorSelectProps) => {
const [open, setOpen] = useState(false);
const onSelectChange = (event: SelectChangeEvent) => {
onChange(event.target.value as Operator);
};
const renderValue = () => {
return (
<StyledValueContainer>
<StyledLabel>{value}</StyledLabel>
<StyledDescription>
{formatOperatorDescription(value)}
</StyledDescription>
</StyledValueContainer>
);
};
return (
<StyledFormInput variant="outlined" size="small" fullWidth>
<InputLabel htmlFor="operator-select">Operator</InputLabel>
<Select
id="operator-select"
name="operator"
label="Operator"
value={value}
open={open}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
onChange={onSelectChange}
renderValue={renderValue}
>
{options.map(operator => (
<StyledMenuItem
key={operator}
value={operator}
separator={needSeparatorAbove(options, operator)}
>
<StyledOptionContainer>
<StyledLabel>{operator}</StyledLabel>
<StyledDescription>
{formatOperatorDescription(operator)}
</StyledDescription>
</StyledOptionContainer>
</StyledMenuItem>
))}
</Select>
</StyledFormInput>
);
};
const needSeparatorAbove = (options: Operator[], option: Operator): boolean => {
if (option === options[0]) {
return false;
}
return operatorGroups.some(group => {
return group[0] === option;
});
};
const operatorGroups = [
inOperators,
stringOperators,
numOperators,
dateOperators,
semVerOperators,
];