SHARE
TWEET

Untitled

a guest Sep 12th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // instalar => npm install --save keycode
  2.  
  3. const getFoodUuid = (data, item) => data.find(i => i.label === item).uuid
  4.  
  5. // AUTOCOMPLETE
  6.   handleInputChange = event => {
  7.     this.setState({ inputValue: event.target.value })
  8.   }
  9.  
  10.   // AUTOCOMPLETE
  11.   handleChange = item => {
  12.     let { selectedItem, selectedFood } = this.state
  13.  
  14.     if (selectedItem.indexOf(item) === -1) {
  15.       selectedItem = [...selectedItem, item]
  16.       selectedFood = [...selectedFood, getFoodUuid(this.state.foodCatalog, item)]
  17.     }
  18.  
  19.     this.setState({
  20.       inputValue: '',
  21.       selectedItem,
  22.       selectedFood
  23.     })
  24.   }
  25.  
  26.   // AUTOCOMPLETE
  27.   handleKeyDown = event => {
  28.     const { inputValue, selectedItem } = this.state
  29.     if (selectedItem.length && !inputValue.length && keycode(event) === 'backspace') {
  30.       this.setState({
  31.         selectedItem: selectedItem.slice(0, selectedItem.length - 1),
  32.       })
  33.     }
  34.   }
  35.  
  36.   // AUTOCOMPLETE
  37.   handleDelete = item => () => {
  38.     this.setState(state => {
  39.       const selectedItem = [...state.selectedItem]
  40.       const selectedFood = [...state.selectedFood]
  41.  
  42.       selectedItem.splice(selectedItem.indexOf(item), 1)
  43.       selectedFood.splice(selectedFood.indexOf(getFoodUuid(this.state.foodCatalog, item)), 1)
  44.  
  45.       return {
  46.         selectedItem,
  47.         selectedFood
  48.       }
  49.     })
  50.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top