1- import React from "react"
1+ import React , { useRef } from "react"
22import Flex from "@/components/templates/flex"
33import { TextMicro } from "@/components/typography"
4+ import Drop from "@/components/drops/drop"
45import { Input , LabelText } from "./styled"
6+ import { useEffect } from "react"
7+ import { useState } from "react"
58
69const Error = ( { error } ) => {
710 const errorMessage = error === true ? "invalid" : error
@@ -13,6 +16,18 @@ const Error = ({ error }) => {
1316 )
1417}
1518
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+
1631export const TextInput = ( {
1732 error,
1833 disabled,
@@ -32,12 +47,23 @@ export const TextInput = ({
3247 containerStyles,
3348 inputContainerStyles,
3449 hideErrorMessage,
50+ autocompleteProps,
3551 ...props
3652} ) => {
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+
3763 return (
3864 < Flex gap = { 0.5 } column className = { className } { ...containerStyles } as = "label" >
3965 { typeof label === "string" ? < LabelText size = { size } > { label } </ LabelText > : label }
40- < Flex position = "relative" { ...inputContainerStyles } >
66+ < Flex ref = { inputContainerRef } position = "relative" { ...inputContainerStyles } >
4167 { iconLeft && (
4268 < Flex position = "absolute" left = { 1 } top = { 0 } bottom = { 0 } alignItems = "center" >
4369 { iconLeft }
@@ -71,6 +97,22 @@ export const TextInput = ({
7197 </ Flex >
7298 { typeof hint === "string" ? < TextMicro color = "textLite" > { hint } </ TextMicro > : ! ! hint && hint }
7399 { ! 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+ ) }
74116 </ Flex >
75117 )
76118}
0 commit comments