next-app/app/components/form.tsx

70 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2024-06-01 15:47:53 +03:00
'use client';
import { Field, Description, Button, Input } from "@headlessui/react";
import { PaperAirplaneIcon } from "@heroicons/react/24/solid";
2024-06-01 15:47:53 +03:00
import { useState } from "react";
export enum FieldType {
number = 'number',
text = 'text',
radio = 'radio',
checkbox = 'checkbox'
}
2024-06-01 15:47:53 +03:00
export type FormFields = {
type: FieldType,
name: string,
label?: string,
required?: boolean,
default?: string,
autocomplete?: boolean,
placeholder?: string
}
2024-06-01 15:47:53 +03:00
const Form = ({ fields, onSubmit }: { fields: FormFields[], onSubmit: (FormData) => any }) => {
const [formData, setFormData] = useState({});
const formSubmit = (e) => {
e.preventDefault();
setFormData( new FormData( e.target ) );
onSubmit( formData );
2024-06-01 15:47:53 +03:00
}
return (
<div>
<form className="mt-10 align-center items-center justify-center gap-x-6" onSubmit={formSubmit}>
{fields.map( (f) => {
return (
<Field>
{f.label && <Description className="text-sm/6 text-white/50"> {f.label} </Description>}
<Input
name={f.name}
required={f.required ? true : false}
type={f.type}
defaultValue={f.default ? f.default : ''}
autoComplete={f.autocomplete ? "on" : "off"}
placeholder={f.placeholder ? f.placeholder : ''}
className=
'mt-3 block w-full rounded-lg border-none bg-white/5 py-1.5 px-3 text-sm/6 text-white focus:outline-none data-[focus]:outline-2 data-[focus]:-outline-offset-2 data-[focus]:outline-white/25'
/>
</Field>
);
})}
<Button
className="mt-9 inline-flex items-center gap-2 rounded-md bg-white py-1.5 px-5 text-sm/6 font-semibold text-black shadow-inner shadow-white/10 focus:outline-none data-[hover]:bg-gray-200 data-[open]:bg-gray-200 data-[focus]:outline-1 data-[focus]:outline-white"
type='submit'
>
Send
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" className="size-4">
<path d="M3.478 2.404a.75.75 0 0 0-.926.941l2.432 7.905H13.5a.75.75 0 0 1 0 1.5H4.984l-2.432 7.905a.75.75 0 0 0 .926.94 60.519 60.519 0 0 0 18.445-8.986.75.75 0 0 0 0-1.218A60.517 60.517 0 0 0 3.478 2.404Z" />
</svg>
</Button>
</form>
</div>
2024-06-01 15:47:53 +03:00
);
}
export default Form;