@@ -2,6 +2,7 @@ import React, { useContext, useEffect, useState } from "react";
2
2
import { FieldContext } from "../contexts/field-context" ;
3
3
import { GroupContext } from "../contexts/group-context" ;
4
4
import { useForceUpdate } from "../force-update" ;
5
+ import { GroupStore } from "../stores/group-store" ;
5
6
6
7
export interface FieldValues {
7
8
defaultValue ?: string ;
@@ -13,55 +14,31 @@ export interface TextFieldProps extends FieldValues {
13
14
name : string ;
14
15
}
15
16
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 ;
22
- }
23
- return `${ groupId } ${ SEPARATOR } ${ props . name } ` ;
17
+ interface FieldResult {
18
+ store : GroupStore ;
19
+ fieldId : string ;
20
+ fieldProps : JSX . IntrinsicElements [ "input" ] ;
24
21
}
25
22
26
- function useRegisterField (
27
- fieldId : string ,
28
- props : TextFieldProps ,
29
- updateHandler : ( ) => void
30
- ) : void {
31
- useEffect ( ( ) => {
32
- const { store } = useContext ( GroupContext ) ;
23
+ function useField ( props : TextFieldProps ) : FieldResult {
24
+ const [ currentValue , setCurrentValue ] = useState ( "" ) ;
33
25
34
- let { defaultValue, initialValue } = props ;
35
- if ( defaultValue == null ) {
36
- defaultValue = "" ;
37
- }
26
+ const { store, groupId } = useContext ( GroupContext ) ;
38
27
39
- if ( initialValue == null ) {
40
- initialValue = defaultValue ;
41
- }
28
+ if ( groupId == null ) {
29
+ // TODO: Error message.
30
+ throw new Error ( "groupId is not defined." ) ;
31
+ }
42
32
43
- store . registerField ( fieldId , props . name , defaultValue , initialValue ) ;
33
+ let { defaultValue, initialValue } = props ;
44
34
45
- store . addListener ( updateHandler ) ;
35
+ const fieldId = store . generateFieldId ( props . name , groupId ) ;
46
36
47
- return ( ) => {
48
- // First, remove listener to not get any more updates.
49
- store . removeListener ( updateHandler ) ;
50
- // Then, unregister field.
51
- store . unregisterField ( fieldId ) ;
52
- } ;
53
- } , [ ] ) ;
54
- }
55
-
56
- export const TextField = ( props : TextFieldProps ) => {
57
- const { store } = useContext ( GroupContext ) ;
58
- const [ currentValue , setCurrentValue ] = useState ( "" ) ;
59
-
60
- const fieldId = useFieldId ( props ) ;
37
+ const onChange : React . ChangeEventHandler < HTMLInputElement > = event => {
38
+ store . updateValue ( fieldId , event . target . value ) ;
39
+ } ;
61
40
62
41
useEffect ( ( ) => {
63
- let { defaultValue, initialValue } = props ;
64
-
65
42
if ( defaultValue == null ) {
66
43
defaultValue = "" ;
67
44
}
@@ -70,40 +47,38 @@ export const TextField = (props: TextFieldProps) => {
70
47
initialValue = defaultValue ;
71
48
}
72
49
73
- store . registerField ( fieldId , props . name , defaultValue , initialValue ) ;
50
+ store . registerField ( props . name , groupId , defaultValue , initialValue ) ;
74
51
75
- const updateValue = ( ) => {
52
+ const storeUpdated = ( ) => {
76
53
const field = store . getField ( fieldId ) ;
77
54
if ( field == null ) {
78
55
return ;
79
56
}
80
57
setCurrentValue ( field . currentValue ) ;
81
58
} ;
82
- updateValue ( ) ;
59
+ storeUpdated ( ) ;
83
60
84
- store . addListener ( updateValue ) ;
61
+ store . addListener ( storeUpdated ) ;
85
62
86
63
return ( ) => {
87
- store . removeListener ( updateValue ) ;
64
+ // First, remove listener to not get any more updates.
65
+ store . removeListener ( storeUpdated ) ;
66
+ // Then, unregister field.
88
67
store . unregisterField ( fieldId ) ;
89
68
} ;
90
69
} , [ ] ) ;
91
-
92
- const onChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
93
- const value = event . target . value ;
94
- store . updateValue ( fieldId , value ) ;
70
+ return {
71
+ fieldId : fieldId ,
72
+ store : store ,
73
+ fieldProps : {
74
+ value : currentValue ,
75
+ onChange : onChange
76
+ }
95
77
} ;
78
+ }
96
79
97
- const onFocus = ( event : React . FocusEvent < HTMLInputElement > ) => {
98
- console . debug ( event . target ) ;
99
- } ;
80
+ export const TextField = ( props : TextFieldProps ) => {
81
+ const { fieldId, store, fieldProps } = useField ( props ) ;
100
82
101
- return (
102
- < input
103
- type = "text"
104
- value = { currentValue }
105
- onChange = { onChange }
106
- onFocus = { onFocus }
107
- />
108
- ) ;
83
+ return < input type = "text" { ...fieldProps } /> ;
109
84
} ;
0 commit comments