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
134import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { Input, Select } from '~/components/inputs';
import {
Form as HookFormProvider,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '.';
import { useSubmit } from 'react-router-dom';
const PropertySchema = z.object({
propertyName: z.string().min(1, 'Please enter a Property name.'),
propertyUrl: z.string().url('Please enter a valid URL.'),
propertyDiscovery: z.enum(['single', 'sitemap', 'discovery_process']),
});
type PropertyFormInputs = z.infer<typeof PropertySchema>;
interface PropertyFormProps {
actionUrl: string;
defaultValues: PropertyFormInputs;
formId: 'add-property-form' | 'edit-property-form';
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
}
const PropertyForm: React.FC<PropertyFormProps> = ({
actionUrl,
defaultValues,
formId,
onChange,
}) => {
const submit = useSubmit();
const form = useForm<PropertyFormInputs>({
resolver: zodResolver(PropertySchema),
defaultValues,
});
return (
<HookFormProvider {...form}>
<form
method="post"
action={actionUrl}
onSubmit={(event) => {
const target = event.currentTarget;
form.handleSubmit(() => {
submit(target, { method: 'post' })
})(event)
}}
id={formId}
className="flex flex-col gap-4 md:flex-row"
>
<FormField
control={form.control}
name="propertyName"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="propertyName">Property Name</FormLabel>
<FormControl>
<Input
type="text"
id="propertyName"
className="h-12 bg-white"
{...field}
onChange={(event) => {
field.onChange(event);
onChange && onChange(event);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="propertyUrl"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="propertyUrl">URL</FormLabel>
<FormControl>
<Input
type="text"
id="propertyUrl"
className="h-12 bg-white"
{...field}
onChange={(event) => {
field.onChange(event);
onChange && onChange(event);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="propertyDiscovery"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="propertyDiscovery">Property Discovery</FormLabel>
<FormControl>
<select
id="propertyDiscovery"
className="h-12 bg-white border-[1px] rounded-lg px-2"
{...field}
onChange={(event) => {
field.onChange(event);
onChange && onChange(event);
}}>
{[
{ label: 'Single', value: 'single' },
{ label: 'Sitemap', value: 'sitemap' },
].map((obj, index) => <option key={index} value={obj.value}>{obj.label}</option>)}
</select>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</HookFormProvider>
);
};
export default PropertyForm;