1
- import React , { useCallback , useState } from "react" ;
1
+ import React , { useCallback } from "react" ;
2
2
import PT from "prop-types" ;
3
3
import cn from "classnames" ;
4
- import _ from "lodash" ;
5
4
import AsyncSelect from "react-select/async" ;
6
5
import { getMemberSuggestions } from "services/teams" ;
7
- // import { getOptionByValue } from "utils/misc";
8
6
import styles from "./styles.module.scss" ;
9
7
10
8
const selectComponents = {
11
9
DropdownIndicator : ( ) => null ,
12
10
IndicatorSeparator : ( ) => null ,
13
11
} ;
14
12
13
+ const loadingMessage = ( ) => "Loading..." ;
14
+
15
+ const noOptionsMessage = ( ) => "No suggestions" ;
16
+
15
17
/**
16
18
* Displays search input field.
17
19
*
@@ -25,20 +27,31 @@ const selectComponents = {
25
27
* @param {string } props.value input value
26
28
* @returns {JSX.Element }
27
29
*/
28
- const SearchAutocomplete = ( {
30
+ const SearchHandleField = ( {
29
31
className,
30
32
id,
33
+ name,
31
34
size = "medium" ,
32
35
onChange,
33
36
placeholder,
34
37
value,
35
38
} ) => {
36
- // const option = getOptionByValue(options, value);
37
- const [ savedInput , setSavedInput ] = useState ( "" ) ;
38
-
39
39
const onValueChange = useCallback (
40
- ( option ) => {
41
- onChange ( option . value ) ;
40
+ ( option , { action } ) => {
41
+ if ( action === "clear" ) {
42
+ onChange ( "" ) ;
43
+ } else {
44
+ onChange ( option . value ) ;
45
+ }
46
+ } ,
47
+ [ onChange ]
48
+ ) ;
49
+
50
+ const onInputChange = useCallback (
51
+ ( value , { action } ) => {
52
+ if ( action === "input-change" ) {
53
+ onChange ( value ) ;
54
+ }
42
55
} ,
43
56
[ onChange ]
44
57
) ;
@@ -51,52 +64,52 @@ const SearchAutocomplete = ({
51
64
classNamePrefix = "custom"
52
65
components = { selectComponents }
53
66
id = { id }
67
+ name = { name }
68
+ isClearable = { true }
54
69
isSearchable = { true }
55
70
// menuIsOpen={true} // for debugging
56
- // onChange={onOptionChange}
57
- // onMenuOpen={onMenuOpen}
58
- // onMenuClose={onMenuClose}
59
- value = { { value, label : value } }
60
- onInputChange = { setSavedInput }
61
- onFocus = { ( ) => {
62
- setSavedInput ( "" ) ;
63
- onChange ( savedInput ) ;
64
- } }
65
- placeholder = { placeholder }
71
+ value = { null }
72
+ inputValue = { value }
66
73
onChange = { onValueChange }
67
- noOptionsMessage = { ( ) => "No options" }
68
- loadingMessage = { ( ) => "Loading..." }
74
+ onInputChange = { onInputChange }
75
+ openMenuOnClick = { false }
76
+ placeholder = { placeholder }
77
+ noOptionsMessage = { noOptionsMessage }
78
+ loadingMessage = { loadingMessage }
69
79
loadOptions = { loadSuggestions }
70
- blurInputOnSelect
80
+ cacheOptions
71
81
/>
72
82
</ div >
73
83
) ;
74
84
} ;
75
85
76
- const loadSuggestions = ( inputVal ) => {
77
- return getMemberSuggestions ( inputVal )
78
- . then ( ( res ) => {
79
- const users = _ . get ( res , "data.result.content" , [ ] ) ;
80
- return users . map ( ( user ) => ( {
81
- label : user . handle ,
82
- value : user . handle ,
83
- } ) ) ;
84
- } )
85
- . catch ( ( ) => {
86
- console . warn ( "could not get suggestions" ) ;
87
- return [ ] ;
88
- } ) ;
86
+ const loadSuggestions = async ( inputVal ) => {
87
+ let options = [ ] ;
88
+ if ( inputVal . length < 3 ) {
89
+ return options ;
90
+ }
91
+ try {
92
+ const res = await getMemberSuggestions ( inputVal ) ;
93
+ const users = res . data . result . content ;
94
+ for ( let i = 0 , len = users . length ; i < len ; i ++ ) {
95
+ let value = users [ i ] . handle ;
96
+ options . push ( { value, label : value } ) ;
97
+ }
98
+ } catch ( error ) {
99
+ console . error ( error ) ;
100
+ console . warn ( "could not get suggestions" ) ;
101
+ }
102
+ return options ;
89
103
} ;
90
104
91
- SearchAutocomplete . propTypes = {
105
+ SearchHandleField . propTypes = {
92
106
className : PT . string ,
93
107
id : PT . string . isRequired ,
94
108
size : PT . oneOf ( [ "medium" , "small" ] ) ,
95
109
name : PT . string . isRequired ,
96
110
onChange : PT . func . isRequired ,
97
- options : PT . array ,
98
111
placeholder : PT . string ,
99
112
value : PT . oneOfType ( [ PT . number , PT . string ] ) ,
100
113
} ;
101
114
102
- export default SearchAutocomplete ;
115
+ export default SearchHandleField ;
0 commit comments