2
2
useMutableCallback ,
3
3
useResizeObserver ,
4
4
} from '@rocket.chat/fuselage-hooks' ;
5
- import React , { useEffect , useRef , useMemo } from 'react' ;
5
+ import React , { useEffect , useRef , useMemo , useState } from 'react' ;
6
6
7
7
import { Box , PositionAnimated , AnimatedVisibility } from '../Box' ;
8
8
import Chip from '../Chip' ;
@@ -14,7 +14,6 @@ import { useCursor, Options } from '../Options';
14
14
const Addon = ( props ) => < Box rcx-autocomplete__addon { ...props } /> ;
15
15
16
16
const SelectedOptions = React . memo ( ( props ) => < Chip { ...props } /> ) ;
17
-
18
17
export function AutoComplete ( {
19
18
value,
20
19
filter,
@@ -34,9 +33,14 @@ export function AutoComplete({
34
33
35
34
const ref = useRef ( ) ;
36
35
36
+ const [ selected , setSelected ] = useState ( ( ) =>
37
+ options . find ( ( option ) => getValue ( option ) === value )
38
+ ) ;
39
+
37
40
const selectByKeyboard = useMutableCallback ( ( [ value ] ) => {
38
- setFilter ( '' ) ;
41
+ setSelected ( options . find ( ( option ) => getValue ( option ) === value ) ) ;
39
42
onChange ( value ) ;
43
+ setFilter ( '' ) ;
40
44
} ) ;
41
45
42
46
const memoizedOptions = useMemo (
@@ -53,6 +57,7 @@ export function AutoComplete({
53
57
] = useCursor ( value , memoizedOptions , selectByKeyboard ) ;
54
58
55
59
const onSelect = useMutableCallback ( ( [ value ] ) => {
60
+ setSelected ( options . find ( ( option ) => getValue ( option ) === value ) ) ;
56
61
onChange ( value ) ;
57
62
setFilter ( '' ) ;
58
63
hide ( ) ;
@@ -88,21 +93,20 @@ export function AutoComplete({
88
93
onBlur = { hide }
89
94
onFocus = { show }
90
95
onKeyDown = { handleKeyDown }
91
- placeholder = { placeholder }
96
+ placeholder = {
97
+ ( optionsAreVisible !== AnimatedVisibility . HIDDEN || ! value ) &&
98
+ placeholder
99
+ }
92
100
order = { 1 }
93
101
rcx-input-box--undecorated
94
102
value = { filter }
95
103
/>
96
- { value && optionsAreVisible === AnimatedVisibility . HIDDEN && (
104
+ { selected && optionsAreVisible === AnimatedVisibility . HIDDEN && (
97
105
< RenderSelected
98
106
role = 'option'
99
107
value = { value }
100
- label = { getLabel (
101
- options . find ( ( option ) => getValue ( option ) === value )
102
- ) }
103
- children = { getLabel (
104
- options . find ( ( option ) => getValue ( option ) === value )
105
- ) }
108
+ label = { getLabel ( selected ) }
109
+ children = { getLabel ( selected ) }
106
110
/>
107
111
) }
108
112
</ Margins >
0 commit comments