findConsumer.js
1.29 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
import React, { Component } from 'react';
import { Async } from 'react-select';
import GetAllConsumers from './getAllConsumers';
import FindLikeConsumers from './findLikeConsumers';
import 'react-select/dist/react-select.css';
export default class FindConsumer extends Component<*, State> {
constructor(props) {
super(props);
this.state = {
selectedOption: '',
todosConsumidores: []
};
this.handleChange = this.handleChange.bind(this);
GetAllConsumers().then((responseAllConsumers) => {
this.setState({ todosConsumidores: responseAllConsumers }); // sets with the result of the query to nodejs
return { allConsumers: responseAllConsumers };
});
}
render() {
const { selectedOption } = this.state;
const value = selectedOption && selectedOption.title;
const getOptions = (input) => {
if (input.length > 0 || input.length === 0) {
return FindLikeConsumers(input, this.state.todosConsumidores).then((response) => {
return { options: response };
});
}
};
return (
<div>
<Async
loadOptions={getOptions}
valueKey={'value'}
labelKey={'label'}
onChange={this.handleChange}
value={value}
/>
</div>
);
}
handleChange = (selectedOption) => {
console.log(selectedOption);
this.setState({ selectedOption: selectedOption });
};
}