11import React from 'react'
22import PropTypes from 'prop-types'
3- import { findIndex } from 'lodash'
43import Select from '../Select/Select'
54import './AutocompleteInput.scss'
6- import { loadMemberSuggestions } from '../../api/projectMembers'
7- import { AUTOCOMPLETE_TRIGGER_LENGTH } from '../../config/constants'
85
96/**
107 * Render a searchable dropdown for selecting users that can be invited
@@ -13,78 +10,27 @@ class AutocompleteInput extends React.Component {
1310 constructor ( props ) {
1411 super ( props )
1512
16- this . onChange = this . onChange . bind ( this )
17- this . asyncOptions = this . asyncOptions . bind ( this )
18-
19- this . debounceTimer = null
20- }
21-
22- // cannot use debounce method from lodash, because we have to return Promise
23- loadMemberSuggestionsDebounced ( value ) {
24- return new Promise ( ( resolve ) => {
25- if ( this . debounceTimer ) {
26- clearTimeout ( this . debounceTimer )
27- }
28-
29- this . debounceTimer = setTimeout ( ( ) => {
30- resolve ( loadMemberSuggestions ( value ) )
31- } , 500 )
32- } )
33- }
34-
35- onChange ( inputValue , selectedOptions = [ ] ) {
36- const { onUpdate } = this . props
37-
38- if ( onUpdate ) {
39- onUpdate ( selectedOptions )
40- }
41- }
42-
43- asyncOptions ( input ) {
44- const { allMembers } = this . props
45-
46- const value = typeof input === 'string' ? input : ''
47- const createOption = {
48- handle : value ,
49- // Decide if it's email
50- isEmail : ( / ( .+ ) @ ( .+ ) { 2 , } \. ( .+ ) { 2 , } / ) . test ( value ) ,
51- }
52- if ( value . length >= AUTOCOMPLETE_TRIGGER_LENGTH ) {
53- return this . loadMemberSuggestionsDebounced ( value ) . then ( r => {
54- // Remove current members from suggestions
55- const suggestions = r . filter ( suggestion => (
56- findIndex ( allMembers , ( member ) => member . handle === suggestion . handle ) === - 1 &&
57- // Remove current value from list to add it manually on top
58- suggestion . handle !== value
59- ) )
60- // Only allow creation if it is not already exists in members
61- const shouldIncludeCreateOption = findIndex ( allMembers , ( member ) => member . handle === value ) === - 1
62-
63- return Promise . resolve ( { options : shouldIncludeCreateOption ?[ createOption , ...suggestions ] : suggestions } )
64- } ) . catch ( ( ) => {
65- return Promise . resolve ( { options : [ createOption ] } )
66- } )
67- }
68- return Promise . resolve ( { options : value . length > 0 ? [ createOption ] : [ ] } )
6913 }
7014
7115 render ( ) {
7216 const {
7317 placeholder,
7418 selectedMembers,
75- disabled,
19+ disabled
7620 } = this . props
7721
7822 return (
7923 < div className = "autocomplete-wrapper" >
8024 < Select
81- multi
25+ isMulti
26+ closeMenuOnSelect
27+ showDropdownIndicator = { false }
28+ createOption
8229 placeholder = { placeholder }
8330 value = { selectedMembers }
84- onChange = { this . onChange }
85- asyncOptions = { this . asyncOptions }
86- valueKey = "handle"
87- labelKey = "handle"
31+ onInputChange = { this . props . onInputChange }
32+ onChange = { this . props . onUpdate }
33+ options = { this . props . suggestedMembers }
8834 disabled = { disabled }
8935 />
9036 </ div >
@@ -132,5 +78,4 @@ AutocompleteInput.propTypes = {
13278 allMembers : PropTypes . arrayOf ( PropTypes . object )
13379}
13480
135-
13681export default AutocompleteInput
0 commit comments