forked from JedWatson/react-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreatable.js
167 lines (157 loc) · 5.07 KB
/
Creatable.js
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
// @flow
import React, {
Component,
type Node,
type ComponentType,
type ElementRef,
} from 'react';
import Select, { type Props as SelectProps } from './Select';
import type { OptionType, OptionsType, ValueType, ActionMeta } from './types';
import { cleanValue } from './utils';
import manageState from './stateManager';
export type CreatableProps = {
/* Allow options to be created while the `isLoading` prop is true. Useful to
prevent the "create new ..." option being displayed while async results are
still being loaded. */
allowCreateWhileLoading: boolean,
/* Gets the label for the "create new ..." option in the menu. Is given the
current input value. */
formatCreateLabel: string => Node,
/* Determines whether the "create new ..." option should be displayed based on
the current input value, select value and options array. */
isValidNewOption: (string, ValueType, OptionsType) => boolean,
/* Returns the data for the new option when it is created. Used to display the
value, and is passed to `onChange`. */
getNewOptionData: (string, Node) => OptionType,
/* If provided, this will be called with the input value when a new option is
created, and `onChange` will **not** be called. Use this when you need more
control over what happens when new options are created. */
onCreateOption?: string => void,
/* Sets the position of the createOption element in your options list. Defaults to 'last' */
createOptionPosition: 'first' | 'last',
};
export type Props = SelectProps & CreatableProps;
const compareOption = (inputValue, option) => {
const candidate = inputValue.toLowerCase();
return (
option.value.toLowerCase() === candidate ||
option.label.toLowerCase() === candidate
);
};
const builtins = {
formatCreateLabel: (inputValue: string) => `Create "${inputValue}"`,
isValidNewOption: (
inputValue: string,
selectValue: OptionsType,
selectOptions: OptionsType
) =>
!(
!inputValue ||
selectValue.some(option => compareOption(inputValue, option)) ||
selectOptions.some(option => compareOption(inputValue, option))
),
getNewOptionData: (inputValue: string, optionLabel: string) => ({
label: optionLabel,
value: inputValue,
__isNew__: true,
}),
};
export const defaultProps = {
allowCreateWhileLoading: false,
createOptionPosition: 'last',
...builtins,
};
type State = {
newOption: OptionType | void,
options: OptionsType,
};
export const makeCreatableSelect = (SelectComponent: ComponentType<*>) =>
class Creatable extends Component<Props, State> {
static defaultProps = defaultProps;
select: ElementRef<*>;
constructor(props: Props) {
super(props);
const options = props.options || [];
this.state = {
newOption: undefined,
options: options,
};
}
componentWillReceiveProps(nextProps: Props) {
const {
allowCreateWhileLoading,
createOptionPosition,
formatCreateLabel,
getNewOptionData,
inputValue,
isLoading,
isValidNewOption,
value,
} = nextProps;
const options = nextProps.options || [];
let { newOption } = this.state;
if (isValidNewOption(inputValue, cleanValue(value), options)) {
newOption = getNewOptionData(inputValue, formatCreateLabel(inputValue));
} else {
newOption = undefined;
}
this.setState({
newOption: newOption,
options:
(allowCreateWhileLoading || !isLoading) && newOption
? (createOptionPosition === 'first' ? [newOption, ...options] : [...options, newOption])
: options,
});
}
onChange = (newValue: ValueType, actionMeta: ActionMeta) => {
const {
getNewOptionData,
inputValue,
isMulti,
onChange,
onCreateOption,
value,
} = this.props;
if (actionMeta.action !== 'select-option') {
return onChange(newValue, actionMeta);
}
const { newOption } = this.state;
const valueArray = Array.isArray(newValue) ? newValue : [newValue];
if (valueArray[valueArray.length - 1] === newOption) {
if (onCreateOption) onCreateOption(inputValue);
else {
const newOptionData = getNewOptionData(inputValue, inputValue);
const newActionMeta = { action: 'create-option' };
if (isMulti) {
onChange([...cleanValue(value), newOptionData], newActionMeta);
} else {
onChange(newOptionData, newActionMeta);
}
}
return;
}
onChange(newValue, actionMeta);
};
focus() {
this.select.focus();
}
blur() {
this.select.blur();
}
render() {
const { ...props } = this.props;
const { options } = this.state;
return (
<SelectComponent
{...props}
ref={ref => {
this.select = ref;
}}
options={options}
onChange={this.onChange}
/>
);
}
};
// TODO: do this in package entrypoint
export default manageState(makeCreatableSelect(Select));