-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.tsx
39 lines (32 loc) · 892 Bytes
/
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
import { RenderableProps } from 'preact';
import { InputValueType } from './components';
import { FormConfig } from './useForm';
export type FormValues<C extends FormConfig> = {
[key in keyof C]: InputValueType<C[key]['type']>;
};
export type FormComponentSetup<C extends FormConfig> = {
id?: string;
values: FormValues<C>;
onSubmit: (values: FormValues<C>) => void;
};
interface FormProps<C extends FormConfig> {
setup: FormComponentSetup<C>;
class?: string;
}
const Form = <C extends FormConfig>(
{
setup: { id, onSubmit, values },
class: className,
children,
}: RenderableProps<FormProps<C>>) => {
const handleSubmit = (e: Event) => {
e.preventDefault();
onSubmit(values);
};
return (
<form {...id ? { id } : {}} onSubmit={handleSubmit} class={`${className ? className : ''}`}>
{children}
</form>
);
};
export default Form;