File tree Expand file tree Collapse file tree 6 files changed +56
-14
lines changed Expand file tree Collapse file tree 6 files changed +56
-14
lines changed Original file line number Diff line number Diff line change @@ -60,8 +60,8 @@ const Test = () => {
60
60
return ( ) => store . removeListener ( listener ) ;
61
61
} , [ ] ) ;
62
62
63
- // const [groupId] = useState(undefined);
64
- const [ groupId ] = useState ( "person" ) ;
63
+ const [ groupId ] = useState ( undefined ) ;
64
+ // const [groupId] = useState("person");
65
65
66
66
const groupContext : GroupContextObject = {
67
67
store : store ,
Original file line number Diff line number Diff line change
1
+ import React , { useContext } from "react" ;
2
+ import { SEPARATOR } from "../stores/constants" ;
3
+ import { GroupContext , GroupContextObject } from "../contexts/group-context" ;
4
+
5
+ interface Props {
6
+ name : string ;
7
+ children : React . ReactNode ;
8
+ }
9
+
10
+ export const Group = ( props : Props ) => {
11
+ const groupContext = useContext ( GroupContext ) ;
12
+
13
+ let groupId = props . name ;
14
+ if ( groupContext . groupId != null ) {
15
+ groupId = `${ groupContext . groupId } ${ SEPARATOR } ${ props . name } ` ;
16
+ }
17
+
18
+ const groupObject : GroupContextObject = {
19
+ ...groupContext ,
20
+ groupId : groupId
21
+ } ;
22
+ return < GroupContext . Provider value = { groupObject } > { props . children } </ GroupContext . Provider > ;
23
+ } ;
Original file line number Diff line number Diff line change
1
+ export const SEPARATOR = "." ;
Original file line number Diff line number Diff line change 1
- import { TinyEmitter , Callback } from "../helpers/emitter" ;
2
1
import { produce } from "immer" ;
2
+ import { TinyEmitter , Callback } from "../helpers/emitter" ;
3
3
import { FieldValues } from "../contracts/field-contracts" ;
4
-
5
- export const SEPARATOR = "." ;
4
+ import { SEPARATOR } from "./constants" ;
6
5
7
6
interface Status {
8
7
focused : boolean ;
@@ -264,7 +263,20 @@ export class GroupStoreMutable extends TinyEmitter<Callback> {
264
263
continue ;
265
264
}
266
265
267
- result [ key ] = field . currentValue ;
266
+ const splitKey = key . split ( SEPARATOR ) ;
267
+
268
+ const paths = splitKey . slice ( 0 , splitKey . length - 1 ) ;
269
+
270
+ // tslint:disable-next-line no-any
271
+ let current : any = result ;
272
+ for ( const path of paths ) {
273
+ if ( current [ path ] == null ) {
274
+ current [ path ] = { } ;
275
+ }
276
+ current = current [ path ] ;
277
+ }
278
+
279
+ current [ field . name ] = field . currentValue ;
268
280
}
269
281
}
270
282
return result ;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { TextField } from "../components/text-field";
3
3
import { ResetButton } from "../components/reset" ;
4
4
import { Permanent } from "../components/permanent" ;
5
5
import { GroupContext } from "../contexts/group-context" ;
6
+ import { Group } from "../components/group" ;
6
7
7
8
export const Test1 = ( ) => {
8
9
const [ show , setShow ] = useState ( true ) ;
@@ -37,14 +38,19 @@ export const Test1 = () => {
37
38
< >
38
39
< div >
39
40
< button onClick = { ( ) => setShow ( ! show ) } > { ! show ? "Mount" : "Unmount" } </ button >
40
- < label >
41
- First name
42
- < TextField name = { firstNameId } initialValue = "John" />
43
- </ label >
44
- { lastName }
45
- { /* ---------
46
- {lastNameDuplicate} */ }
47
- { lastNamePermanent }
41
+ < Group name = "person" >
42
+ < label >
43
+ First name
44
+ < TextField name = { firstNameId } initialValue = "John" />
45
+ </ label >
46
+ { lastName }
47
+ { lastNamePermanent }
48
+ </ Group >
49
+ < Group name = "meta" >
50
+ < TextField name = "area" initialValue = "development" />
51
+ < TextField name = "priority" initialValue = "2" />
52
+ </ Group >
53
+
48
54
< div >
49
55
< ResetButton > Reset</ ResetButton >
50
56
</ div >
You can’t perform that action at this time.
0 commit comments