All files / Select Async.js

19.05% Statements 4/21
0% Branches 0/13
0% Functions 0/6
19.05% Lines 4/21
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 731x 1x                                                                                                                     1x             1x          
import React from 'react'
import PropTypes from 'prop-types'
 
class SelectAsync extends React.PureComponent {
  constructor(props) {
    super(props)
 
    this.state = {
      isLoading: false,
      options: props.options
    }
  }
 
  loadOptions = (term) => {
    const { loadOptions } = this.props
 
    const callback = (error, data) => {
      const options = data && data.options || [];
      if (callback === this._callback) {
				this._callback = null;
				this.setState({
					isLoading: false,
					options
				});
			}
    }
 
    this._callback = callback
 
    const promise = loadOptions(term, callback);
 
    if (promise) {
			promise.then(
				(data) => callback(null, data),
				(error) => callback(error)
			);
		}
 
		if ( this._callback && !this.state.isLoading ) {
			this.setState({
				isLoading: true
			});
		}
  }
 
  render() {
    return React.cloneElement(this.props.children, Object.assign({}, this.props, {
      options: this.state.options,
      onTyping: this.loadOptions
    }))
    // (
    //
    //   <Select
    //     {...this.props}
    //     options={this.state.options}
    //     onTyping={this.loadOptions} />
    // )
  }
}
 
SelectAsync.propTypes = {
  autoload: PropTypes.bool.isRequired,
  ignoreAccents: PropTypes.bool,
  ignoreCase: PropTypes.bool,
  loadOptions: PropTypes.func.isRequired
}
 
SelectAsync.defaultProps = {
  loadOptions: () => {}
}
 
export default SelectAsync