-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReactSelectChip.tsx
206 lines (188 loc) Β· 5.14 KB
/
ReactSelectChip.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @module common/components
*/
import Chip from "@material-ui/core/Chip";
import MenuItem from "@material-ui/core/MenuItem";
import Paper from "@material-ui/core/Paper";
import {emphasize} from "@material-ui/core/styles/colorManipulator";
import {Theme} from "@material-ui/core/styles/createMuiTheme";
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
import CancelIcon from "@material-ui/icons/Cancel";
import makeStyles from "@material-ui/styles/makeStyles";
import useTheme from "@material-ui/styles/useTheme";
import classNames from "classnames";
import * as React from "react";
import {Creatable} from "react-select";
import {CreatableProps} from "react-select/lib/Creatable";
import {Props as SelectableProps} from "react-select/lib/Select";
/* @ignore */
const useStyles = makeStyles((theme: Theme) => ({
chip: {
margin: `${theme.spacing(.5)} ${theme.spacing(.25)}`,
},
chipFocused: {
backgroundColor: emphasize(
theme.palette.type === "light" ? theme.palette.grey[300] : theme.palette.grey[700],
0.08,
),
},
divider: {
height: theme.spacing(2),
},
input: {
display: "flex",
padding: 0,
},
noOptionsMessage: {
padding: `${theme.spacing(1)} ${theme.spacing(2)}`,
},
paper: {
left: 0,
marginTop: theme.spacing(1),
position: "absolute",
right: 0,
zIndex: 2,
},
placeholder: {
fontSize: 16,
left: 2,
position: "absolute",
},
root: {
flexGrow: 1,
height: 250,
},
singleValue: {
fontSize: 16,
},
valueContainer: {
alignItems: "center",
display: "flex",
flex: 1,
flexWrap: "wrap",
overflow: "hidden",
},
}));
function NoOptionsMessage(props: React.ComponentProps<any>) {
return (
<Typography
color="textSecondary"
className={props.selectProps.classes.noOptionsMessage}
{...props.innerProps}
>
{props.children}
</Typography>
);
}
function inputComponent({inputRef, ...props}: React.ComponentProps<any>) {
return <div ref={inputRef} {...props} />;
}
function Control(props: React.ComponentProps<any>) {
const inputProps = {
inputComponent,
inputProps: {
children: props.children,
className: props.selectProps.classes.input,
inputRef: props.innerRef,
...props.innerProps,
},
};
return (
<TextField
fullWidth
InputProps={inputProps}
{...props.selectProps.textFieldProps}
/>
);
}
function Option(props: React.ComponentProps<any>) {
const itemStyle = {
fontWeight: props.isSelected ? 500 : 400,
};
return (
<MenuItem
buttonRef={props.innerRef}
selected={props.isFocused}
component="div"
style={itemStyle}
{...props.innerProps}
>
{props.children}
</MenuItem>
);
}
function Placeholder(props: React.ComponentProps<any>) {
return (
<Typography
color="textSecondary"
className={props.selectProps.classes.placeholder}
{...props.innerProps}
>
{props.children}
</Typography>
);
}
function SingleValue(props: React.ComponentProps<any>) {
return (
<Typography className={props.selectProps.classes.singleValue} {...props.innerProps}>
{props.children}
</Typography>
);
}
function ValueContainer(props: React.ComponentProps<any>) {
return <div className={props.selectProps.classes.valueContainer}>{props.children}</div>;
}
function MultiValue(props: React.ComponentProps<any>) {
const className = classNames(props.selectProps.classes.chip, {
[props.selectProps.classes.chipFocused]: props.isFocused,
});
return (
<Chip
tabIndex={-1}
label={props.children}
className={className}
onDelete={props.removeProps.onClick}
deleteIcon={<CancelIcon {...props.removeProps} />}
/>
);
}
function Menu(props: React.ComponentProps<any>) {
return (
<Paper square className={props.selectProps.classes.paper} {...props.innerProps}>
{props.children}
</Paper>
);
}
const components = {
Control,
Menu,
MultiValue,
NoOptionsMessage,
Option,
Placeholder,
SingleValue,
ValueContainer,
};
export function MUICreatable<OptionsType>(props: SelectableProps<OptionsType> & CreatableProps<OptionsType>) {
const classes = useStyles();
const theme: Theme = useTheme();
const selectStyles = {
input: (base: React.CSSProperties) => ({
...base,
"& input": {
font: "inherit",
},
"color": theme.palette.text.primary,
}),
};
return (
// @ts-ignore
<Creatable
classes={classes}
styles={selectStyles}
components={components}
{...props}
/>
);
}