-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathForm.tsx
143 lines (127 loc) · 3.63 KB
/
Form.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
'use client';
import React from 'react';
import { Field, Input, RefreshButton, Tag } from 'shared/components';
import { form } from 'shared/form';
import { useUrlState } from 'state-in-url/next';
import { SourceCodeBtn } from './components/SourceCodeBtn';
export const Form = ({
className,
searchParams,
ghLink
}: {
className?: string;
searchParams?: object;
ghLink: string
}) => {
const { urlState, setUrl: setUrlBase } = useUrlState(form, {
searchParams,
});
const replace = React.useRef(false);
const setUrl = React.useCallback((state: Parameters<typeof setUrlBase>[0], opts?: Parameters<typeof setUrlBase>[1]) => {
setUrlBase(state, { replace: replace.current, ...opts });
replace.current = true;
}, [setUrlBase]);
const onChangeAge = React.useCallback(
(ev: React.ChangeEvent<HTMLInputElement>) => {
const val = +ev.target.value;
setUrl({ age: val ? val : undefined });
},
[setUrl],
);
const onChangeName = 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 flex-1 flex-col border border-grey rounded-md p-4 shadow-md hover:shadow-lg">
<div className="font-semibold mb-4">
First client component
</div>
<div className="space-y-6 flex-col">
<Field id="name" text="Name">
<Input
id="name"
value={urlState.name}
onChange={onChangeName}
name="name"
/>
</Field>
<Field id="age" text="Age">
<Input
id="age"
type="number"
value={urlState.age}
onChange={onChangeAge}
name="age"
/>
</Field>
<Field
id="agree_to_terms"
text="Agree to terms"
className="flex flex-row justify-between gap-2 max-w-[150px] min-w-[150px]"
>
<Input
id="agree_to_terms"
type="checkbox"
className="max-w-[24px]"
checked={urlState['agree_to_terms']}
onChange={onChangeTerms}
name="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>
<RefreshButton />
</div>
<SourceCodeBtn
href={ghLink}
className="self-end ml-auto mt-4"
/>
</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') },
},
];