Files
tanami-admin-panel/src/Components/FormField.jsx

98 lines
2.9 KiB
React
Raw Normal View History

2024-06-26 17:45:13 +05:30
import { FormControl, FormLabel, Input, Textarea, Select, Checkbox, RadioGroup, Radio, Stack } from '@chakra-ui/react';
import React from 'react';
2024-06-24 12:08:21 +05:30
import { Controller } from 'react-hook-form';
import { TiWarning } from 'react-icons/ti';
const FormField = ({
label,
control,
name,
type = "text",
2024-06-26 17:45:13 +05:30
options = [],
2024-06-24 12:08:21 +05:30
errors,
isRequired,
2024-06-25 19:16:55 +05:30
arabic,
2024-06-24 12:08:21 +05:30
...props
}) => (
2024-06-26 17:45:13 +05:30
<FormControl isInvalid={errors[name]}>
2024-06-25 19:16:55 +05:30
<FormLabel textAlign={arabic ? "right" : "left"} fontSize={"sm"}>{label}</FormLabel>
2024-06-24 12:08:21 +05:30
<Controller
control={control}
name={name}
defaultValue=""
render={({ field }) => {
2024-06-26 17:45:13 +05:30
switch (type) {
case 'textarea':
return (
<Textarea
focusBorderColor="forestGreen.400"
size={"sm"}
{...field}
{...props}
placeholder={label}
textAlign={arabic ? "right" : "left"}
/>
);
case 'select':
return (
<Select
focusBorderColor="forestGreen.300"
size={"sm"}
{...field}
{...props}
placeholder={label}
textAlign={arabic ? "right" : "left"}
>
{options.map((option, index) => (
<option key={index} value={option.value}>{option.label}</option>
))}
</Select>
);
case 'checkbox':
return (
<Checkbox
size={"sm"}
{...field}
{...props}
textAlign={arabic ? "right" : "left"}
>
{label}
</Checkbox>
);
case 'radio':
return (
<RadioGroup {...field} {...props}>
<Stack direction="row">
{options.map((option, index) => (
<Radio key={index} value={option.value}>
{option.label}
</Radio>
))}
</Stack>
</RadioGroup>
);
default:
return (
<Input
focusBorderColor="forestGreen.300"
size={"sm"}
type={type}
{...field}
{...props}
placeholder={label}
textAlign={arabic ? "right" : "left"}
/>
);
}
2024-06-24 12:08:21 +05:30
}}
/>
{errors[name] && (
<span className="text-danger web-text-small fw-bold ps-2 d-flex align-items-center gap-1 mt-1">
<TiWarning className="fw-bold fs-5 " /> {errors[name].message}
</span>
)}
</FormControl>
);
2024-06-26 17:45:13 +05:30
export default FormField;