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 = () => {
6060 return ( ) => store . removeListener ( listener ) ;
6161 } , [ ] ) ;
6262
63- // const [groupId] = useState(undefined);
64- const [ groupId ] = useState ( "person" ) ;
63+ const [ groupId ] = useState ( undefined ) ;
64+ // const [groupId] = useState("person");
6565
6666 const groupContext : GroupContextObject = {
6767 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" ;
21import { produce } from "immer" ;
2+ import { TinyEmitter , Callback } from "../helpers/emitter" ;
33import { FieldValues } from "../contracts/field-contracts" ;
4-
5- export const SEPARATOR = "." ;
4+ import { SEPARATOR } from "./constants" ;
65
76interface Status {
87 focused : boolean ;
@@ -264,7 +263,20 @@ export class GroupStoreMutable extends TinyEmitter<Callback> {
264263 continue ;
265264 }
266265
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 ;
268280 }
269281 }
270282 return result ;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { TextField } from "../components/text-field";
33import { ResetButton } from "../components/reset" ;
44import { Permanent } from "../components/permanent" ;
55import { GroupContext } from "../contexts/group-context" ;
6+ import { Group } from "../components/group" ;
67
78export const Test1 = ( ) => {
89 const [ show , setShow ] = useState ( true ) ;
@@ -37,14 +38,19 @@ export const Test1 = () => {
3738 < >
3839 < div >
3940 < 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+
4854 < div >
4955 < ResetButton > Reset</ ResetButton >
5056 </ div >
You can’t perform that action at this time.
0 commit comments