Skip to content

Commit e2a79a0

Browse files
Group component added.
1 parent 4491f3e commit e2a79a0

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

src/app.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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,

src/components/group-field.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
};

src/stores/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const SEPARATOR = ".";

src/stores/group-store.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { TinyEmitter, Callback } from "../helpers/emitter";
21
import { produce } from "immer";
2+
import { TinyEmitter, Callback } from "../helpers/emitter";
33
import { FieldValues } from "../contracts/field-contracts";
4-
5-
export const SEPARATOR = ".";
4+
import { SEPARATOR } from "./constants";
65

76
interface 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;

src/tests/test1.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TextField } from "../components/text-field";
33
import { ResetButton } from "../components/reset";
44
import { Permanent } from "../components/permanent";
55
import { GroupContext } from "../contexts/group-context";
6+
import { Group } from "../components/group-field";
67

78
export 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>

0 commit comments

Comments
 (0)