1- import React , { useRef } from "react"
1+ import React , { useRef , useMemo } from "react"
22import Flex from "@/components/templates/flex"
33import { TextMicro } from "@/components/typography"
4- import Drop from "@/components/drops/drop"
54import { Input , LabelText } from "./styled"
6- import { useEffect } from "react"
7- import { useState } from "react"
5+ import Autocomplete from "./autocomplete"
86
97const Error = ( { error } ) => {
108 const errorMessage = error === true ? "invalid" : error
@@ -16,18 +14,6 @@ const Error = ({ error }) => {
1614 )
1715}
1816
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-
3117export const TextInput = ( {
3218 error,
3319 disabled,
@@ -51,14 +37,17 @@ export const TextInput = ({
5137 ...props
5238} ) => {
5339 const inputContainerRef = useRef ( )
54- const [ autocompleteOpen , setAutocompleteOpen ] = useState ( )
55- const { suggestions = [ ] } = autocompleteProps || { }
5640
57- useEffect ( ( ) => {
58- if ( suggestions . length ) {
59- setAutocompleteOpen ( ! ! value . length )
60- }
61- } , [ suggestions , value , setAutocompleteOpen ] )
41+ const autocompleteInputProps = useMemo (
42+ ( ) =>
43+ autocompleteProps
44+ ? {
45+ "aria-autocomplete" : "list" ,
46+ "aria-controls" : "autocomplete-list" ,
47+ }
48+ : { } ,
49+ [ ]
50+ )
6251
6352 return (
6453 < Flex gap = { 0.5 } column className = { className } { ...containerStyles } as = "label" >
@@ -85,6 +74,7 @@ export const TextInput = ({
8574 ref = { inputRef }
8675 error = { error }
8776 hasValue = { ! ! value }
77+ { ...autocompleteInputProps }
8878 { ...props }
8979 />
9080
@@ -97,22 +87,11 @@ export const TextInput = ({
9787 </ Flex >
9888 { typeof hint === "string" ? < TextMicro color = "textLite" > { hint } </ TextMicro > : ! ! hint && hint }
9989 { ! 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- ) }
90+ < Autocomplete
91+ value = { value }
92+ autocompleteProps = { autocompleteProps }
93+ tagretRef = { inputContainerRef }
94+ />
11695 </ Flex >
11796 )
11897}
0 commit comments