-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
select.tsx
132 lines (110 loc) · 3.6 KB
/
select.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
// Wrapper for "react-select" component
// API docs: https://react-select.com/
import "./select.scss";
import React, { ReactNode } from "react";
import { computed } from "mobx";
import { observer } from "mobx-react";
import { autobind, cssNames } from "../../utils";
import ReactSelect, { ActionMeta, components, Props as ReactSelectProps, Styles } from "react-select";
import Creatable, { CreatableProps } from "react-select/creatable";
import { themeStore } from "../../theme.store";
const { Menu } = components;
export interface GroupSelectOption<T extends SelectOption = SelectOption> {
label: ReactNode;
options: T[];
}
export interface SelectOption<T = any> {
value: T;
label?: React.ReactNode;
}
export interface SelectProps<T = any> extends ReactSelectProps<T>, CreatableProps<T> {
value?: T;
themeName?: "dark" | "light" | "outlined";
menuClass?: string;
isCreatable?: boolean;
autoConvertOptions?: boolean; // to internal format (i.e. {value: T, label: string}[]), not working with groups
onChange?(option: T, meta?: ActionMeta<any>): void;
}
@observer
export class Select extends React.Component<SelectProps> {
static defaultProps: SelectProps = {
autoConvertOptions: true,
menuPortalTarget: document.body,
menuPlacement: "auto",
};
@computed get theme() {
return this.props.themeName || themeStore.activeTheme.type;
}
private styles: Styles = {
menuPortal: styles => ({
...styles,
zIndex: "auto"
}),
};
protected isValidOption(opt: SelectOption | any) {
return typeof opt === "object" && opt.value !== undefined;
}
@computed get selectedOption() {
const { value, isMulti } = this.props;
if (isMulti) {
return this.options.filter(opt => {
const values = value ? [].concat(value) : [];
return values.includes(opt) || values.includes(opt.value);
});
}
return this.options.find(opt => opt === value || opt.value === value);
}
@computed get options(): SelectOption[] {
const { autoConvertOptions, options } = this.props;
if (autoConvertOptions && Array.isArray(options)) {
return options.map(opt => {
return this.isValidOption(opt) ? opt : { value: opt, label: String(opt) };
});
}
return options as SelectOption[];
}
@autobind()
onChange(value: SelectOption, meta: ActionMeta<any>) {
if (this.props.onChange) {
this.props.onChange(value, meta);
}
}
@autobind()
onKeyDown(evt: React.KeyboardEvent<HTMLElement>) {
if (this.props.onKeyDown) {
this.props.onKeyDown(evt);
}
const escapeKey = evt.nativeEvent.code === "Escape";
if (escapeKey) evt.stopPropagation(); // don't close the <Dialog/>
}
render() {
const {
className, menuClass, isCreatable, autoConvertOptions,
value, options, components = {}, ...props
} = this.props;
const themeClass = `theme-${this.theme}`;
const WrappedMenu = components.Menu ?? Menu;
const selectProps: Partial<SelectProps> = {
...props,
styles: this.styles,
value: autoConvertOptions ? this.selectedOption : value,
options: autoConvertOptions ? this.options : options,
onChange: this.onChange,
onKeyDown: this.onKeyDown,
className: cssNames("Select", themeClass, className),
classNamePrefix: "Select",
components: {
...components,
Menu: props => (
<WrappedMenu
{...props}
className={cssNames(menuClass, themeClass, props.className)}
/>
),
}
};
return isCreatable
? <Creatable {...selectProps}/>
: <ReactSelect {...selectProps}/>;
}
}