2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
interface ChoiceProviderProps {
|
|
|
|
children: ((
|
|
|
|
props: {
|
|
|
|
choices: Array<{
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
}>;
|
|
|
|
loading: boolean;
|
|
|
|
fetchChoices(value: string);
|
|
|
|
}
|
|
|
|
) => React.ReactElement<any>);
|
|
|
|
choices: Array<{
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
}>;
|
|
|
|
}
|
|
|
|
interface ChoiceProviderState {
|
|
|
|
choices: Array<{
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
}>;
|
|
|
|
loading: boolean;
|
|
|
|
timeout: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ChoiceProvider extends React.Component<
|
|
|
|
ChoiceProviderProps,
|
|
|
|
ChoiceProviderState
|
|
|
|
> {
|
|
|
|
state = { choices: [], loading: false, timeout: null };
|
|
|
|
|
|
|
|
handleChange = inputValue => {
|
|
|
|
if (this.state.loading) {
|
|
|
|
clearTimeout(this.state.timeout);
|
|
|
|
}
|
|
|
|
const timeout = setTimeout(() => this.fetchChoices(inputValue), 500);
|
|
|
|
this.setState({
|
|
|
|
loading: true,
|
|
|
|
timeout
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchChoices = inputValue => {
|
|
|
|
let count = 0;
|
|
|
|
this.setState({
|
|
|
|
choices: this.props.choices.filter(suggestion => {
|
|
|
|
const keep =
|
|
|
|
(!inputValue ||
|
|
|
|
suggestion.label.toLowerCase().indexOf(inputValue.toLowerCase()) !==
|
|
|
|
-1) &&
|
|
|
|
count < 5;
|
|
|
|
|
|
|
|
if (keep) {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keep;
|
|
|
|
}),
|
|
|
|
loading: false,
|
|
|
|
timeout: null
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return this.props.children({
|
|
|
|
choices: this.state.choices,
|
|
|
|
fetchChoices: this.handleChange,
|
|
|
|
loading: this.state.loading
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|