This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathWelcomeMessageFeature.tsx
109 lines (104 loc) · 3.23 KB
/
WelcomeMessageFeature.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
import { SimpleGrid } from '@chakra-ui/layout';
import { TextAreaForm } from '@/components/forms/TextAreaForm';
import { UseFormRender, WelcomeMessageFeature } from '@/config/types';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { ColorPickerForm, SmallColorPickerForm } from '@/components/forms/ColorPicker';
import { DatePickerForm } from '@/components/forms/DatePicker';
import { FilePickerForm } from '@/components/forms/FilePicker';
import { SwitchFieldForm } from '@/components/forms/SwitchField';
import { ChannelSelectForm } from '@/components/forms/ChannelSelect';
const schema = z.object({
message: z.string().min(20),
channel: z.string(),
color: z.string().optional(),
date: z.date().optional(),
file: z.custom<File[]>().optional(),
danger: z.boolean(),
});
type Input = z.infer<typeof schema>;
export const useWelcomeMessageFeature: UseFormRender<WelcomeMessageFeature> = (data, onSubmit) => {
const { register, reset, handleSubmit, formState, control } = useForm<Input>({
resolver: zodResolver(schema),
shouldUnregister: false,
defaultValues: {
channel: data.channel,
message: data.message ?? '',
color: undefined,
date: undefined,
file: [],
danger: false,
},
});
return {
component: (
<SimpleGrid columns={{ base: 1, lg: 2 }} gap={3}>
<ChannelSelectForm
control={{
label: 'Channel',
description: 'Where to send the welcome message',
}}
controller={{ control, name: 'channel' }}
/>
<TextAreaForm
control={{
label: 'Message',
description: 'The message to send',
error: formState.errors.message?.message,
}}
placeholder="Type some text here..."
{...register('message')}
/>
<SmallColorPickerForm
control={{
label: 'Color',
description: 'The color of message',
}}
supportAlpha
controller={{ control, name: 'color' }}
/>
<FilePickerForm
control={{
label: 'File',
description: 'The file to upload',
}}
options={{ accept: { 'image/*': [] }, multiple: false }}
controller={{ control, name: 'file' }}
/>
<ColorPickerForm
control={{
label: 'Color',
description: 'The color of message',
}}
controller={{ control, name: 'color' }}
/>
<DatePickerForm
control={{
label: 'Date',
description: 'The date of today',
}}
controller={{ control, name: 'date' }}
/>
<SwitchFieldForm
control={{ label: 'Turn on', description: 'Enable something' }}
controller={{
control,
name: 'danger',
}}
/>
</SimpleGrid>
),
onSubmit: handleSubmit(async (e) => {
const data = await onSubmit(
JSON.stringify({
message: e.message,
channel: e.channel,
})
);
reset(data);
}),
canSave: formState.isDirty,
reset: () => reset(control._defaultValues),
};
};