1
- import React from "react"
1
+ import React , { useRef } from "react"
2
2
import Flex from "@/components/templates/flex"
3
3
import { TextMicro } from "@/components/typography"
4
+ import Drop from "@/components/drops/drop"
4
5
import { Input , LabelText } from "./styled"
6
+ import { useEffect } from "react"
7
+ import { useState } from "react"
5
8
6
9
const Error = ( { error } ) => {
7
10
const errorMessage = error === true ? "invalid" : error
@@ -13,6 +16,18 @@ const Error = ({ error }) => {
13
16
)
14
17
}
15
18
19
+ const Suggestions = ( { suggestions = [ ] } = { } ) => {
20
+ return (
21
+ < ul role = "listbox" >
22
+ { suggestions . map ( ( { value, label } ) => (
23
+ < li key = { value } role = "option" >
24
+ { label }
25
+ </ li >
26
+ ) ) }
27
+ </ ul >
28
+ )
29
+ }
30
+
16
31
export const TextInput = ( {
17
32
error,
18
33
disabled,
@@ -32,12 +47,23 @@ export const TextInput = ({
32
47
containerStyles,
33
48
inputContainerStyles,
34
49
hideErrorMessage,
50
+ autocompleteProps,
35
51
...props
36
52
} ) => {
53
+ const inputContainerRef = useRef ( )
54
+ const [ autocompleteOpen , setAutocompleteOpen ] = useState ( )
55
+ const { suggestions = [ ] } = autocompleteProps || { }
56
+
57
+ useEffect ( ( ) => {
58
+ if ( suggestions . length ) {
59
+ setAutocompleteOpen ( ! ! value . length )
60
+ }
61
+ } , [ suggestions , value , setAutocompleteOpen ] )
62
+
37
63
return (
38
64
< Flex gap = { 0.5 } column className = { className } { ...containerStyles } as = "label" >
39
65
{ typeof label === "string" ? < LabelText size = { size } > { label } </ LabelText > : label }
40
- < Flex position = "relative" { ...inputContainerStyles } >
66
+ < Flex ref = { inputContainerRef } position = "relative" { ...inputContainerStyles } >
41
67
{ iconLeft && (
42
68
< Flex position = "absolute" left = { 1 } top = { 0 } bottom = { 0 } alignItems = "center" >
43
69
{ iconLeft }
@@ -71,6 +97,22 @@ export const TextInput = ({
71
97
</ Flex >
72
98
{ typeof hint === "string" ? < TextMicro color = "textLite" > { hint } </ TextMicro > : ! ! hint && hint }
73
99
{ ! hideErrorMessage ? < Error error = { error } /> : null }
100
+ { autocompleteOpen && inputContainerRef ?. current && (
101
+ < Drop
102
+ width = { 60 }
103
+ target = { inputContainerRef . current }
104
+ align = { { top : "bottom" , left : "left" } }
105
+ animation
106
+ background = "inputBg"
107
+ margin = { [ 1 , 0 , 0 ] }
108
+ round = { 1 }
109
+ close = { ( ) => { } }
110
+ onClickOutside = { ( ) => { } }
111
+ onEsc = { ( ) => { } }
112
+ >
113
+ < Suggestions suggestions = { suggestions } />
114
+ </ Drop >
115
+ ) }
74
116
</ Flex >
75
117
)
76
118
}
0 commit comments