-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Form-for-test.tsx
188 lines (173 loc) · 4.93 KB
/
Form-for-test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React from "react";
import { useSearchParams } from "react-router-dom";
import { Field, Input, RefreshButton, Tag } from "shared/components";
import { form } from "shared/form";
import { useUrlState } from "state-in-url/react-router";
export const Form = ({ className }: { className?: string }) => {
const [sp] = useSearchParams();
const { urlState, setState, setUrl, reset } = useUrlState(form, {
replace: sp.get("replace") === "false" ? false : true,
});
React.useEffect(() => {
if (urlState?.tags?.length) {
const dateObj = urlState.tags.find((t) => t?.value?.time);
if (dateObj) {
console.assert(
dateObj?.value?.time instanceof Date,
"Date should be a Date instance!",
);
}
const isoDate = urlState.tags.find((t) => t?.value?.iso);
if (isoDate) {
console.assert(
typeof isoDate?.value?.iso === "string",
"iso should be a string!",
);
}
}
// Just to test ts types
if (urlState?.tags?.length === 10) {
setState(urlState);
setState((st) => st);
setState((st) => ({ ...st, age: 18 }));
setUrl(urlState);
setUrl((st) => ({ ...st, age: 18 }));
setUrl(urlState, { replace: true });
setUrl((st) => ({ ...st, age: 18 }), { replace: true });
reset();
reset({ replace: false, preventScrollReset: true });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onChangeAge = React.useCallback(
(ev: React.ChangeEvent<HTMLInputElement>) => {
const val = +ev.target.value;
setUrl({ age: val ? val : undefined });
},
[setUrl],
);
const onChangeNameUrl = React.useCallback(
(ev: React.ChangeEvent<HTMLInputElement>) => {
setUrl({ name: ev.target.value });
},
[setUrl],
);
const onChangeTerms = React.useCallback(
(ev: React.ChangeEvent<HTMLInputElement>) => {
setUrl({ agree_to_terms: ev.target.checked });
},
[setUrl],
);
const onChangeTags = React.useCallback(
(tag: (typeof tags)[number]) => {
setUrl((curr) => ({
...curr,
tags: curr.tags.find((t) => t.id === tag.id)
? curr.tags.filter((t) => t.id !== tag.id)
: curr.tags.concat(tag),
}));
},
[setUrl],
);
return (
<div className={className}>
<div className="flex-1 border border-grey rounded-md p-4 shadow-md">
<div className="font-semibold mb-4">First client component</div>
<div className="space-y-6">
<Field id="name" text="Name">
<Input
id="name"
value={urlState.name}
onChange={onChangeNameUrl}
className="h-[30px] text-black"
/>
</Field>
<Field id="age" text="Age">
<Input
id="age"
type="number"
value={urlState.age}
onChange={onChangeAge}
className="h-[30px] text-black"
/>
</Field>
<Field
id="agree_to_terms"
text="Agree to terms"
className="select-none flex gap-4 cursor-pointer"
>
<Input
id="agree_to_terms"
type="checkbox"
checked={urlState["agree_to_terms"]}
onChange={onChangeTerms}
className="w-[25px] h-[25px] text-black"
data-testid="agree_to_terms"
/>
</Field>
<Field id="tags" text="Tags">
<div className="flex flex-wrap gap-2">
{tags.map((tag) => (
<Tag
active={!!urlState.tags.find((t) => t.id === tag.id)}
text={tag.value.text}
onClick={() => onChangeTags(tag)}
key={tag.id}
/>
))}
</div>
</Field>
<Button onClick={reset} dataTestId="sync-default">
Reset state
</Button>
<Button
onClick={() => {
setUrl((curr) => ({ ...curr, name: "My Name", age: 55 }));
}}
dataTestId="sync-object"
>
Sync state object
</Button>
<RefreshButton />
</div>
</div>
</div>
);
};
const tags = [
{
id: "1",
value: { text: "React.js", time: new Date("2024-07-17T04:53:17.000Z") },
},
{
id: "2",
value: { text: "Next.js", time: new Date("2024-07-18T04:53:17.000Z") },
},
{
id: "3",
value: {
text: "TailwindCSS",
time: new Date("2024-07-19T04:53:17.000Z"),
iso: "2020-07-19T04:53:17.000Z",
},
},
];
const Button = ({
onClick,
dataTestId,
children,
}: {
onClick: () => void;
dataTestId: string;
children: React.ReactNode;
}) => {
return (
<button
onClick={onClick}
className="border border-black rounded-md px-2 py-1 text-black"
data-testid={dataTestId}
>
{children}
</button>
);
};