-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormComponents.tsx
More file actions
345 lines (322 loc) · 9.44 KB
/
FormComponents.tsx
File metadata and controls
345 lines (322 loc) · 9.44 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { useStore } from '@tanstack/react-form';
import { useFieldContext, useFormContext } from '../hooks/app-form-context';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import * as ShadcnSelect from '@/components/ui/select';
import { Slider as ShadcnSlider } from '@/components/ui/slider';
import { Switch as ShadcnSwitch } from '@/components/ui/switch';
import { Textarea as ShadcnTextarea } from '@/components/ui/textarea';
import type { CheckedState } from '@radix-ui/react-checkbox';
import { Decimal } from '@sovryn/slayer-shared';
import { Loader2Icon } from 'lucide-react';
import { useEffect, useState, type ReactNode } from 'react';
import type { GetBalanceData } from 'wagmi/query';
import { AmountRenderer } from './ui/amount-renderer';
import { Checkbox } from './ui/checkbox';
import { Field, FieldDescription, FieldError, FieldLabel } from './ui/field';
import { InputGroup, InputGroupAddon, InputGroupInput } from './ui/input-group';
export function SubscribeButton({ label }: { label: string }) {
const form = useFormContext();
return (
<form.Subscribe
selector={(state) => [state.isSubmitting, state.isFormValid]}
>
{([isSubmitting, isFormValid]) => (
<Button
type="submit"
disabled={isSubmitting || !isFormValid}
form={form.formId}
>
<Loader2Icon
className={`mr-2 h-4 w-4 animate-spin ${isSubmitting ? '' : 'hidden'}`}
/>
{label}
</Button>
)}
</form.Subscribe>
);
}
function ErrorMessages({
errors,
}: {
errors: Array<string | { message: string }>;
}) {
return (
<>
{errors.map((error) => (
<FieldError
key={typeof error === 'string' ? error : error.message}
className="text-red-500 mt-1 font-bold"
>
{typeof error === 'string' ? error : error.message}
</FieldError>
))}
</>
);
}
export function TextField({
label,
placeholder,
description,
}: {
label: ReactNode;
placeholder?: string;
description?: string;
}) {
const field = useFieldContext<string>();
const errors = useStore(field.store, (state) => state.meta.errors);
return (
<Field>
<FieldLabel htmlFor={field.name}>{label}</FieldLabel>
<Input
id={field.name}
value={field.state.value}
placeholder={placeholder}
onBlur={field.handleBlur}
onChange={(e) => field.handleChange(e.target.value)}
/>
{description && <FieldDescription>{description}</FieldDescription>}
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
</Field>
);
}
export function TextArea({
label,
rows = 3,
description,
}: {
label: ReactNode;
rows?: number;
description?: string;
}) {
const field = useFieldContext<string>();
const errors = useStore(field.store, (state) => state.meta.errors);
return (
<Field>
<FieldLabel htmlFor={field.name}>{label}</FieldLabel>
<ShadcnTextarea
id={field.name}
value={field.state.value}
onBlur={field.handleBlur}
rows={rows}
onChange={(e) => field.handleChange(e.target.value)}
/>
{description && <FieldDescription>{description}</FieldDescription>}
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
</Field>
);
}
export function Select({
label,
values,
placeholder,
description,
}: {
label: ReactNode;
values: Array<{ label: string; value: string }>;
placeholder?: string;
description?: string;
}) {
const field = useFieldContext<string>();
const errors = useStore(field.store, (state) => state.meta.errors);
return (
<Field>
<FieldLabel htmlFor={field.name}>{label}</FieldLabel>
<ShadcnSelect.Select
name={field.name}
value={field.state.value}
onValueChange={(value) => field.handleChange(value)}
>
<ShadcnSelect.SelectTrigger className="w-full">
<ShadcnSelect.SelectValue placeholder={placeholder} />
</ShadcnSelect.SelectTrigger>
<ShadcnSelect.SelectContent>
<ShadcnSelect.SelectGroup>
<ShadcnSelect.SelectLabel>{label}</ShadcnSelect.SelectLabel>
{values.map((value) => (
<ShadcnSelect.SelectItem key={value.value} value={value.value}>
{value.label}
</ShadcnSelect.SelectItem>
))}
</ShadcnSelect.SelectGroup>
</ShadcnSelect.SelectContent>
</ShadcnSelect.Select>
{description && <FieldDescription>{description}</FieldDescription>}
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
</Field>
);
}
export function Slider({
label,
description,
}: {
label: ReactNode;
description?: string;
}) {
const field = useFieldContext<number>();
const errors = useStore(field.store, (state) => state.meta.errors);
return (
<Field>
<FieldLabel htmlFor={field.name}>{label}</FieldLabel>
<ShadcnSlider
id={field.name}
onBlur={field.handleBlur}
value={[field.state.value]}
onValueChange={(value) => field.handleChange(value[0])}
/>
{description && <FieldDescription>{description}</FieldDescription>}
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
</Field>
);
}
export function Switch({
label,
description,
}: {
label: ReactNode;
description?: string;
}) {
const field = useFieldContext<boolean>();
const errors = useStore(field.store, (state) => state.meta.errors);
return (
<Field>
<div className="flex items-center gap-2">
<ShadcnSwitch
id={field.name}
onBlur={field.handleBlur}
checked={field.state.value}
onCheckedChange={(checked) => field.handleChange(checked)}
/>
<FieldLabel htmlFor={field.name}>{label}</FieldLabel>
</div>
{description && <FieldDescription>{description}</FieldDescription>}
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
</Field>
);
}
export function CheckBox({
label,
description,
}: {
label: ReactNode;
description?: string;
}) {
const field = useFieldContext<CheckedState>();
const errors = useStore(field.store, (state) => state.meta.errors);
return (
<Field>
<div className="flex items-start gap-3">
<Checkbox
id={field.name}
checked={field.state.value}
onCheckedChange={(checked) => field.handleChange(checked)}
onBlur={field.handleBlur}
className="mt-1"
/>
<div className="grid gap-2">
<FieldLabel htmlFor={field.name}>{label}</FieldLabel>
{description && <FieldDescription>{description}</FieldDescription>}
</div>
</div>
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
</Field>
);
}
const tryDecimalValue = (input: string): string => {
try {
if (input) {
const decimalValue = Decimal.from(input);
return decimalValue.toString();
}
return '';
} catch {
return '';
}
};
export function AmountField({
label,
placeholder,
description,
balance,
addonRight,
}: {
label: ReactNode;
placeholder?: string;
description?: string;
balance?: Omit<GetBalanceData, 'formatted'>;
addonRight?: ReactNode;
}) {
const field = useFieldContext<string>();
const errors = useStore(field.store, (state) => state.meta.errors);
const [renderedValue, setRenderedValue] = useState<string>(
tryDecimalValue(field.state.value),
);
const handleChange = (input: string) => {
setRenderedValue(input);
field.setValue(tryDecimalValue(input) as never, {
dontRunListeners: true,
});
};
useEffect(() => {
const unsub = field.store.subscribe(({ prevVal, currentVal }) => {
if (prevVal.value !== currentVal.value) {
setRenderedValue(tryDecimalValue(currentVal.value));
}
});
return unsub;
}, []);
return (
<Field>
<FieldLabel htmlFor={field.name}>
{balance ? (
<>
<div className="w-full flex flex-row gap-4 justify-between items-center">
<span>{label}</span>
<Button
variant="link"
size="sm"
className="p-0"
onClick={() => {
field.setValue(
Decimal.from(balance.value).toString(balance.decimals),
);
}}
>
<span>
(max:
<AmountRenderer
value={Decimal.from(
balance.value,
balance.decimals,
).toString()}
suffix={balance.symbol}
showApproxSign
/>
)
</span>
</Button>
</div>
</>
) : (
<>{label}</>
)}
</FieldLabel>
<InputGroup>
<InputGroupInput
id={field.name}
value={renderedValue}
placeholder={placeholder}
onBlur={field.handleBlur}
onChange={(e) => handleChange(e.target.value)}
type="number"
step="0.00001"
/>
{addonRight && (
<InputGroupAddon align="inline-end">{addonRight}</InputGroupAddon>
)}
</InputGroup>
{description && <FieldDescription>{description}</FieldDescription>}
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
</Field>
);
}