All files / components/select-box/auto-complete index.js

0% Statements 0/84
0% Branches 0/41
0% Functions 0/17
0% Lines 0/22
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                                                                                                                                                                                                     
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Select from 'react-select'
import Highlighter from 'react-highlight-words'
import { Arrow as ArrowIcon } from 'icons'
import InputContainer from 'utils/input-container'
import AutoCompleteStyled from '../select-box.styled'
 
const arrowRenderer = () => (
  <div>
    <ArrowIcon />
  </div>
)
 
class AutoComplete extends Component {
  renderOption = option => {
    const { labelKey } = this.props
 
    return (
      <div className='option-item'>
        <Highlighter
          searchWords={[this._inputValue]}
          textToHighlight={option[labelKey]}
          highlightClassName='text-hightlight'
        />
      </div>
    )
  }
 
  componentDidMount () {
    console.log(this.widget)
  }
 
  loadOptions = (params) => {
    const { loadData, options } = this.props
 
    return loadData ? loadData(params) : Promise.resolve({ options: options || [] })
  }
 
  render () {
    const {
      placeholder,
      name,
      onChange,
      value,
      labelKey,
      valueKey,
      multi,
      loadData,
      className,
      clearable,
      cache,
      ...rest
    } = this.props
 
    return (
      <InputContainer {...rest} >
        <AutoCompleteStyled className={className} >
          <Select.Async
            ref={(widget) => this.widget = widget}
            onInputChange={inputValue => { this._inputValue = inputValue }}
            noResultsText='Nenhum resultado encontrado'
            loadingPlaceholder='Buscando...'
            placeholder={placeholder}
            onChange={onChange}
            value={value}
            autosize={false}
            autoload
            openOnClick
            arrowRenderer={arrowRenderer}
            optionRenderer={this.renderOption}
            valueKey={valueKey}
            labelKey={labelKey}
            multi={multi}
            loadOptions={this.loadOptions}
            clearable={clearable}
            cache={cache}
          />
        </AutoCompleteStyled>
      </InputContainer>
    )
  }
}
 
AutoComplete.propTypes = {
  className: PropTypes.string,
  width: PropTypes.string,
  placeholder: PropTypes.string,
  label: PropTypes.string,
  name: PropTypes.string,
  type: PropTypes.oneOf(['primary', 'success', 'error', 'alert']),
  message: PropTypes.string,
  onChange: PropTypes.func,
  value: PropTypes.oneOfType([
    PropTypes.object,
    PropTypes.array
  ]),
  cache: PropTypes.bool,
  labelKey: PropTypes.string,
  valueKey: PropTypes.string,
  multi: PropTypes.bool,
  clearable: PropTypes.bool,
  loadData: PropTypes.func
}
 
export { AutoComplete }