1
- import React , { useState , useContext , useRef , useEffect } from 'react' ;
1
+ import React , { useState , useRef , useEffect } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import { FormContext , handleInputChange , normalizeOptions } from './form-helpers' ;
3
+ import { handleInputChange , normalizeOptions } from './helpers /form-helpers' ;
4
4
import { Dropdown } from '../mixed/Dropdown' ;
5
5
import { useOpenState } from '../utils/useOpenState' ;
6
+ import { useFormControl } from './helpers/useFormControl' ;
6
7
7
8
export function FormAutocomplete ( {
8
9
onSearch,
@@ -18,11 +19,11 @@ export function FormAutocomplete({
18
19
const [ searchValue , setSearchValue ] = useState ( '' ) ;
19
20
const { isOpen, open, close } = useOpenState ( ) ;
20
21
const [ ignoreBlur , setIgnoreBlur ] = useState ( false ) ;
21
- const formState = useContext ( FormContext ) ;
22
+ const { getValue , setValue , register } = useFormControl ( name ) ;
22
23
const inputRef = useRef ( null ) ;
23
24
24
25
useEffect ( ( ) => {
25
- formState . register ( name , inputRef . current ) ;
26
+ register ( inputRef . current ) ;
26
27
} , [ ] ) ;
27
28
28
29
return (
@@ -38,8 +39,8 @@ export function FormAutocomplete({
38
39
onSearch ( value ) ;
39
40
open ( ) ;
40
41
41
- if ( formState . getValue ( name ) ) {
42
- formState . update ( name , null ) ;
42
+ if ( getValue ( ) ) {
43
+ setValue ( null ) ;
43
44
}
44
45
} ,
45
46
} ) }
@@ -67,7 +68,7 @@ export function FormAutocomplete({
67
68
isOpen = { isOpen ( ) }
68
69
items = { normalizeOptions ( options , FormData ) . filter ( filter ( searchValue ) ) }
69
70
onSelect = { ( { value, label } ) => {
70
- formState . update ( name , value ) ;
71
+ setValue ( value ) ;
71
72
setSearchValue ( label ) ;
72
73
close ( ) ;
73
74
} }
0 commit comments