Skip to content

Instantly share code, notes, and snippets.

@oscaroceguera
Created September 1, 2019 07:47
Show Gist options
  • Save oscaroceguera/bdf5512e13b399e9b2042dad7078c791 to your computer and use it in GitHub Desktop.
Save oscaroceguera/bdf5512e13b399e9b2042dad7078c791 to your computer and use it in GitHub Desktop.
funciones para autocomplete
// instalar => npm install --save keycode
const getFoodUuid = (data, item) => data.find(i => i.label === item).uuid
// AUTOCOMPLETE
handleInputChange = event => {
this.setState({ inputValue: event.target.value })
}
// AUTOCOMPLETE
handleChange = item => {
let { selectedItem, selectedFood } = this.state
if (selectedItem.indexOf(item) === -1) {
selectedItem = [...selectedItem, item]
selectedFood = [...selectedFood, getFoodUuid(this.state.foodCatalog, item)]
}
this.setState({
inputValue: '',
selectedItem,
selectedFood
})
}
// AUTOCOMPLETE
handleKeyDown = event => {
const { inputValue, selectedItem } = this.state
if (selectedItem.length && !inputValue.length && keycode(event) === 'backspace') {
this.setState({
selectedItem: selectedItem.slice(0, selectedItem.length - 1),
})
}
}
// AUTOCOMPLETE
handleDelete = item => () => {
this.setState(state => {
const selectedItem = [...state.selectedItem]
const selectedFood = [...state.selectedFood]
selectedItem.splice(selectedItem.indexOf(item), 1)
selectedFood.splice(selectedFood.indexOf(getFoodUuid(this.state.foodCatalog, item)), 1)
return {
selectedItem,
selectedFood
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment