When I try to call an API to fill semantic UI's options, it displays a portion of the options at first and in order to display the full list I have to click outside the dropdown (blur it) first then click inside it again.
I have been stuck on this for a while and I really can't think of anything else to try, anyone know why it is behaving this way?
Here is the code:
import React, { Component } from 'react';
import { Dropdown } from 'semantic-ui-react';
import axios from 'axios';
let typingTimer;
class App extends Component {
constructor(props) {
super(props);
this.state = {
creators: [],
creatorsLoading: true,
selectedCreator: null
};
this.searchCreators = this.searchCreators.bind(this);
this.setCreatorsState = this.setCreatorsState.bind(this);
this.changeCreator = this.changeCreator.bind(this);
}
componentDidMount() {
this.searchCreators();
}
setCreatorsState(res) {
this.setState({
creators: res.data.map((user) => {
return { text: `${user.name} (${user.country})`, value: user.id };
}),
creatorsLoading: false
});
}
searchCreators(searchQuery) {
if (searchQuery === '') {
this.setState({ creatorsLoading: false });
return null;
}
this.setState({ creatorsLoading: true });
const args = {
params: {
'search_query: searchQuery.trim();
}
};
axios
.get(url, args)
.then((res) => {
if ('error' in res)
return this.setState({ creatorsLoading: false });
else {
this.setCreatorsState(res.data);
}
})
.catch((err) => this.setState({ creatorsLoading: false }));
}
delayExecute(text) {
//Detect keystroke and only execute after the user has finish typing
clearTimeout(typingTimer);
typingTimer = setTimeout(() => {
return this.searchCreators(text);
}, 700);
return true;
}
changeCreator(value) {
if (value) this.setState({ selectedCreator: value });
else this.setState({ selectedCreator: null });
}
render() {
const {creators, creatorsLoading, selectedCreator} = this.state;
return (
<Dropdown
selectOnBlur={false}
loading={creatorsLoading || false}
clearable
onChange={(_, data) => this.changeUser(data.value)}
onSearchChange={(_, data) => this.delayExecute(data.searchQuery)}
placeholder="Creators"
fluid
selection
search
value={selectedCreator}
options={creators}
/>
);
}
}
export default App;