Repository navigation
Expand file tree
/
Copy pathAsync.js
More file actions
178 lines (170 loc) · 5.37 KB
/
Copy pathAsync.js
File metadata and controls
178 lines (170 loc) · 5.37 KB
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
// @flow
import React, { Component, type ComponentType, type ElementRef } from 'react';
import Select, { type Props as SelectProps } from './Select';
import { handleInputChange } from './utils';
import manageState from './stateManager';
import type { OptionsType, InputActionMeta } from './types';
export type AsyncProps = {
/* The default set of options to show before the user starts searching. When
set to `true`, the results for loadOptions('') will be autoloaded. */
defaultOptions: OptionsType | boolean,
/* Function that returns a promise, which is the set of options to be used
once the promise resolves. */
loadOptions: (string, (OptionsType) => void) => Promise<*> | void,
/* If cacheOptions is truthy, then the loaded data will be cached. The cache
will remain until `cacheOptions` changes value. */
cacheOptions: any,
};
export type Props = SelectProps & AsyncProps;
export const defaultProps = {
cacheOptions: false,
defaultOptions: false,
};
type State = {
defaultOptions?: OptionsType,
inputValue: string,
isLoading: boolean,
loadedInputValue?: string,
loadedOptions: OptionsType,
passEmptyOptions: boolean,
};
export const makeAsyncSelect = (SelectComponent: ComponentType<*>) =>
class Async extends Component<Props, State> {
static defaultProps = defaultProps;
select: ElementRef<*>;
lastRequest: {};
mounted: boolean = false;
optionsCache: { [string]: OptionsType } = {};
constructor(props: Props) {
super();
this.state = {
defaultOptions: Array.isArray(props.defaultOptions)
? props.defaultOptions
: undefined,
inputValue: '',
isLoading: props.defaultOptions === true ? true : false,
loadedOptions: [],
passEmptyOptions: false,
};
}
componentDidMount() {
this.mounted = true;
const { defaultOptions } = this.props;
if (defaultOptions === true) {
this.loadOptions('', options => {
if (!this.mounted) return;
const isLoading = !!this.lastRequest;
this.setState({ defaultOptions: options || [], isLoading });
});
}
}
componentWillReceiveProps(nextProps: Props) {
// if the cacheOptions prop changes, clear the cache
if (nextProps.cacheOptions !== this.props.cacheOptions) {
this.optionsCache = {};
}
if (nextProps.defaultOptions !== this.props.defaultOptions) {
this.setState({
defaultOptions: Array.isArray(nextProps.defaultOptions)
? nextProps.defaultOptions
: undefined,
});
}
}
componentWillUnmount() {
this.mounted = false;
}
focus() {
this.select.focus();
}
blur() {
this.select.blur();
}
loadOptions(inputValue: string, callback: (?Array<*>) => void) {
const { loadOptions } = this.props;
if (!loadOptions) return callback();
const loader = loadOptions(inputValue, callback);
if (loader && typeof loader.then === 'function') {
loader.then(callback, () => callback());
}
}
handleInputChange = (newValue: string, actionMeta: InputActionMeta) => {
const { cacheOptions, onInputChange } = this.props;
// TODO
const inputValue = handleInputChange(newValue, actionMeta, onInputChange);
if (!inputValue) {
delete this.lastRequest;
this.setState({
inputValue: '',
loadedInputValue: '',
loadedOptions: [],
isLoading: false,
passEmptyOptions: false,
});
return;
}
if (cacheOptions && this.optionsCache[inputValue]) {
this.setState({
inputValue,
loadedInputValue: inputValue,
loadedOptions: this.optionsCache[inputValue],
isLoading: false,
passEmptyOptions: false,
});
} else {
const request = (this.lastRequest = {});
this.setState(
{
inputValue,
isLoading: true,
passEmptyOptions: !this.state.loadedInputValue,
},
() => {
this.loadOptions(inputValue, options => {
if (!this.mounted) return;
if (options) {
this.optionsCache[inputValue] = options;
}
if (request !== this.lastRequest) return;
delete this.lastRequest;
this.setState({
isLoading: false,
loadedInputValue: inputValue,
loadedOptions: options || [],
passEmptyOptions: false,
});
});
}
);
}
return inputValue;
};
render() {
const { loadOptions, ...props } = this.props;
const {
defaultOptions,
inputValue,
isLoading,
loadedInputValue,
loadedOptions,
passEmptyOptions,
} = this.state;
const options = passEmptyOptions
? []
: inputValue && loadedInputValue ? loadedOptions : defaultOptions || [];
return (
// $FlowFixMe
<SelectComponent
{...props}
filterOption={this.props.filterOption || null}
ref={ref => {
this.select = ref;
}}
options={options}
isLoading={isLoading}
onInputChange={this.handleInputChange}
/>
);
}
};
export default makeAsyncSelect(manageState(Select));