11import React , { useContext , useEffect , useState } from "react" ;
22import { FieldContext } from "../contexts/field-context" ;
3+ import { GroupContext } from "../contexts/group-context" ;
4+ import { useForceUpdate } from "../force-update" ;
35
46export interface FieldValues {
57 defaultValue ?: string ;
@@ -11,29 +13,72 @@ export interface TextFieldProps extends FieldValues {
1113 name : string ;
1214}
1315
14- const useFieldValues = ( values : FieldValues ) => {
15- console . info ( "useFieldValues" ) ;
16- const [ defaultValue , setDefaultValue ] = useState ( values . defaultValue || "" ) ;
17- let [ initialValue , setInitialValue ] = useState ( values . initialValue ) ;
18- if ( initialValue == null ) {
19- initialValue = defaultValue ;
16+ export const SEPARATOR = "." ;
17+
18+ function useFieldId ( props : Pick < TextFieldProps , "name" > ) : string {
19+ const { groupId } = useContext ( GroupContext ) ;
20+ if ( groupId == null ) {
21+ return props . name ;
2022 }
23+ return `${ groupId } ${ SEPARATOR } ${ props . name } ` ;
24+ }
2125
22- const [ currentValue , setCurrentValue ] = useState ( initialValue ) ;
26+ function useRegisterField ( fieldId : string , props : TextFieldProps ) {
27+ useEffect ( ( ) => {
28+ const { store } = useContext ( GroupContext ) ;
2329
24- return { currentValue, setCurrentValue } ;
25- } ;
30+ let { defaultValue, initialValue } = props ;
31+ if ( defaultValue == null ) {
32+ defaultValue = "" ;
33+ }
34+
35+ if ( initialValue == null ) {
36+ initialValue = defaultValue ;
37+ }
38+
39+ store . registerField ( fieldId , props . name , defaultValue , initialValue ) ;
40+ } , [ ] ) ;
41+ }
2642
2743export const TextField = ( props : TextFieldProps ) => {
28- const { currentValue, setCurrentValue } = useFieldValues ( {
29- defaultValue : props . defaultValue ,
30- initialValue : props . initialValue ,
31- currentValue : props . currentValue ,
32- } ) ;
44+ const { store } = useContext ( GroupContext ) ;
45+ const [ currentValue , setCurrentValue ] = useState ( "" ) ;
46+
47+ const fieldId = useFieldId ( props ) ;
48+
49+ useEffect ( ( ) => {
50+ let { defaultValue, initialValue } = props ;
51+
52+ if ( defaultValue == null ) {
53+ defaultValue = "" ;
54+ }
55+
56+ if ( initialValue == null ) {
57+ initialValue = defaultValue ;
58+ }
59+
60+ store . registerField ( fieldId , props . name , defaultValue , initialValue ) ;
61+
62+ const updateValue = ( ) => {
63+ const field = store . getField ( fieldId ) ;
64+ if ( field == null ) {
65+ return ;
66+ }
67+ setCurrentValue ( field . currentValue ) ;
68+ } ;
69+ updateValue ( ) ;
70+
71+ store . addListener ( updateValue ) ;
72+
73+ return ( ) => {
74+ store . removeListener ( updateValue ) ;
75+ store . unregisterField ( fieldId ) ;
76+ } ;
77+ } , [ ] ) ;
3378
3479 const onChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
35- // store.setCurrentValue( event.target.value) ;
36- setCurrentValue ( event . target . value ) ;
80+ const value = event . target . value ;
81+ store . updateValue ( fieldId , value ) ;
3782 } ;
3883
3984 const onFocus = ( event : React . FocusEvent < HTMLInputElement > ) => {
0 commit comments