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

504 lines
16 KiB
React
Raw Normal View History

2024-07-01 12:33:55 +05:30
import {
FormControl,
FormLabel,
Input,
Textarea,
Select,
Checkbox,
RadioGroup,
Radio,
Stack,
Box,
Heading,
FormHelperText,
Kbd,
Image,
Text,
2024-07-24 19:58:15 +05:30
Table,
Thead,
Tr,
Th,
Tbody,
Td,
InputGroup,
InputRightAddon,
2024-07-01 12:33:55 +05:30
} from "@chakra-ui/react";
import { Controller } from "react-hook-form";
import { TiWarning } from "react-icons/ti";
import { motion } from "framer-motion";
import { AddIcon, CloseIcon } from "@chakra-ui/icons";
2024-06-24 12:08:21 +05:30
2024-07-24 19:58:15 +05:30
const today = new Date().toISOString().split("T")[0];
const formatDate = (dateString) => {
const date = new Date(dateString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are zero-based
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
};
const defaultDate = "8/2/2024";
// Format the default date as YYYY-MM-DD
const formattedDate = formatDate(defaultDate);
2024-06-24 12:08:21 +05:30
const FormField = ({
2024-06-27 11:50:59 +05:30
label,
control,
name,
2024-07-01 12:33:55 +05:30
id,
2024-06-27 11:50:59 +05:30
type = "text",
options = [],
errors,
isRequired,
rules,
arabic,
placeHolder,
helperText,
multiple,
handleImageChange,
2024-07-01 12:33:55 +05:30
selectedImageData,
setSelectedImageData,
removeImage,
2024-07-05 12:07:52 +05:30
imageData,
width,
2024-07-24 19:58:15 +05:30
value,
handleInputChange,
align,
maxLength,
2024-06-27 11:50:59 +05:30
...props
}) => (
2024-07-01 12:33:55 +05:30
<FormControl
2024-07-05 12:07:52 +05:30
w={width ? width : "49%"}
2024-07-01 12:33:55 +05:30
isInvalid={errors[name]}
isRequired={isRequired}
2024-07-09 19:05:08 +05:30
mb={2}
2024-07-01 12:33:55 +05:30
>
2024-07-24 19:58:15 +05:30
<FormLabel textAlign={"left"} fontSize={"xs"} color={"gray.600"}>
2024-07-01 12:33:55 +05:30
{label}
</FormLabel>
2024-06-27 11:50:59 +05:30
<Controller
control={control}
name={name}
2024-07-24 19:58:15 +05:30
defaultValue={value}
2024-06-27 11:50:59 +05:30
rules={rules}
render={({ field }) => {
2024-07-01 12:33:55 +05:30
if (type === "select") {
2024-06-27 11:50:59 +05:30
return (
<Select
2024-07-24 19:58:15 +05:30
bg={"#F5F8F6"}
2024-06-27 11:50:59 +05:30
focusBorderColor="forestGreen.300"
size={"sm"}
{...field}
{...props}
2024-07-01 12:33:55 +05:30
cursor={"pointer"}
2024-07-24 19:58:15 +05:30
fontSize={"sm"}
2024-07-01 12:33:55 +05:30
placeholder={placeHolder ? placeHolder : label}
2024-06-27 11:50:59 +05:30
textAlign={arabic ? "right" : "left"}
2024-07-09 19:05:08 +05:30
_placeholder={{ fontSize: "sm" }}
2024-07-24 19:58:15 +05:30
rounded={"sm"}
2024-06-27 11:50:59 +05:30
>
{options.map((option, index) => (
2024-07-01 12:33:55 +05:30
<option key={index} value={option.value}>
{option.label}
</option>
2024-06-27 11:50:59 +05:30
))}
</Select>
);
2024-07-01 12:33:55 +05:30
} else if (type === "textarea") {
2024-06-27 11:50:59 +05:30
return (
<Textarea
focusBorderColor="forestGreen.400"
size={"sm"}
{...field}
{...props}
2024-07-01 12:33:55 +05:30
placeholder={placeHolder ? placeHolder : label}
2024-06-27 11:50:59 +05:30
textAlign={arabic ? "right" : "left"}
2024-07-09 19:05:08 +05:30
_placeholder={{ fontSize: "sm" }}
2024-07-24 19:58:15 +05:30
rounded={"sm"}
2024-07-05 12:07:52 +05:30
resize={"none"}
2024-07-11 11:46:48 +05:30
rows={2}
2024-07-24 19:58:15 +05:30
bg={"#F5F8F6"}
maxLength={maxLength} // Set the maximum character limit
2024-06-27 11:50:59 +05:30
/>
);
2024-07-01 12:33:55 +05:30
} else if (type === "checkbox") {
2024-06-27 11:50:59 +05:30
return (
<Checkbox
size={"sm"}
{...field}
{...props}
textAlign={arabic ? "right" : "left"}
>
{label}
</Checkbox>
);
2024-07-01 12:33:55 +05:30
} else if (type === "radio") {
2024-06-27 11:50:59 +05:30
return (
2024-07-24 19:58:15 +05:30
<RadioGroup bg={"#F5F8F6"} {...field} {...props}>
2024-06-27 11:50:59 +05:30
<Stack direction="row">
{options.map((option, index) => (
<Radio key={index} value={option.value}>
{option.label}
</Radio>
))}
</Stack>
</RadioGroup>
);
2024-07-01 12:33:55 +05:30
} else if (type === "file") {
2024-06-27 11:50:59 +05:30
return (
2024-07-01 12:33:55 +05:30
<>
<Box
borderColor="gray.300"
borderStyle="dashed"
borderWidth="2px"
rounded="md"
shadow="sm"
role="group"
transition="all 150ms ease-in-out"
_hover={{
shadow: "md",
}}
as={motion.div}
initial="rest"
animate="rest"
whileHover="hover"
height={"105px"}
className="pointer"
>
<Box position="relative" height="100%" width="100%">
<Box
position="absolute"
top="0"
left="0"
2024-06-27 11:50:59 +05:30
height="100%"
width="100%"
display="flex"
2024-07-01 12:33:55 +05:30
flexDirection="column"
2024-06-27 11:50:59 +05:30
>
2024-07-01 12:33:55 +05:30
<Stack
height="100%"
width="100%"
display="flex"
alignItems="center"
justify="center"
2024-06-27 11:50:59 +05:30
>
<span
2024-07-01 12:33:55 +05:30
className="d-flex flex-column align-items-center pointer"
spacing="1"
2024-06-27 11:50:59 +05:30
>
2024-07-01 12:33:55 +05:30
<Heading
fontSize="lg"
color="gray.700"
fontWeight="bold"
cursor={"pointer"}
>
Drop images here
</Heading>
<span
fontWeight="light"
className="web-text-large text-secondary text-center pointer"
>
or click to upload
</span>
2024-06-27 11:50:59 +05:30
</span>
2024-07-01 12:33:55 +05:30
</Stack>
</Box>
<Input
{...field}
{...props}
type="file"
height="100%"
width="100%"
position="absolute"
top="0"
left="0"
opacity="0"
aria-hidden="true"
accept="image/*"
onChange={(e) => {
field.onChange(e);
handleImageChange(e);
}}
onDrop={(e) => {
field.onChange(e);
handleImageChange(e);
}}
/>
2024-06-27 11:50:59 +05:30
</Box>
</Box>
2024-07-01 12:33:55 +05:30
</>
2024-06-27 11:50:59 +05:30
);
2024-07-01 12:33:55 +05:30
} else if (type === "fileNormal") {
2024-06-27 11:50:59 +05:30
return (
<>
<Input
2024-07-05 12:07:52 +05:30
id={id}
2024-06-27 11:50:59 +05:30
{...field}
{...props}
multiple={multiple} // Support for multiple file uploads
accept="image/*"
type="file"
className="web-text-medium form-control rounded-1"
size="sm"
2024-07-24 19:58:15 +05:30
bg={"#F5F8F6"}
2024-06-27 11:50:59 +05:30
onChange={(e) => {
field.onChange(e);
handleImageChange(e);
}}
/>
{multiple && (
<FormHelperText className="web-text-small">
You can select multiple images using{" "}
<span className="text-dark">
2024-07-01 12:33:55 +05:30
<Kbd size={"sm"} className="text-dark">
ctrl
</Kbd>{" "}
+ <Kbd className="text-dark">select</Kbd>
</span>
2024-06-27 11:50:59 +05:30
</FormHelperText>
)}
2024-07-05 12:07:52 +05:30
{selectedImageData &&
(multiple ? (
selectedImageData?.length > 0 && (
<Box
mt={4}
width={"100%"}
display="flex"
flexWrap="wrap"
gap={4}
>
{selectedImageData?.map((imageDataLink, index) => (
<Box key={index} width={"100px"}>
<Image
2024-07-01 12:33:55 +05:30
rounded={"md"}
2024-07-05 12:07:52 +05:30
objectFit={"cover"}
src={imageDataLink}
alt={`profile-${index}`}
width={100}
height={100}
2024-07-01 12:33:55 +05:30
/>
2024-07-05 12:07:52 +05:30
<Box
display={"flex"}
flexDirection={"column"}
position={"relative"}
2024-07-01 12:33:55 +05:30
>
2024-07-05 12:07:52 +05:30
<CloseIcon
onClick={() => removeImage(index)}
bg={"#fff"}
className="link pointer"
p={1}
fontSize={"lg"}
color={"red"}
fontWeight={"500"}
rounded={"md"}
position={"absolute"}
bottom={0}
right={0}
/>
<Text
as={"span"}
fontSize={"sm"}
fontWeight={"500"}
mt={1}
isTruncated={true}
>
{imageData[index]?.name}
</Text>
<Text
as={"span"}
fontSize={"sm"}
fontStyle={"italic"}
>
{(imageData[index]?.size / (1024 * 1024)).toFixed(
2
)}{" "}
mb
</Text>
</Box>
2024-07-01 12:33:55 +05:30
</Box>
2024-07-05 12:07:52 +05:30
))}
<AddIcon
onClick={() => document.getElementById(id).click()}
rounded={"md"}
width={50}
height={50}
mt={26}
p={4}
cursor={"pointer"}
className="link"
/>
</Box>
)
) : (
<Box mt={5} width={"49%"}>
<Image
2024-07-01 12:33:55 +05:30
rounded={"md"}
2024-07-05 12:07:52 +05:30
objectFit={"cover"}
src={selectedImageData}
alt="profile"
width={100}
height={100}
2024-07-01 12:33:55 +05:30
/>
2024-07-05 12:07:52 +05:30
<Box
w={"30%"}
display={"flex"}
flexDirection={"column"}
position={"relative"}
2024-07-01 12:33:55 +05:30
>
2024-07-05 12:07:52 +05:30
<CloseIcon
onClick={() => setSelectedImageData(null)}
className="link pointer"
p={1}
fontSize={"lg"}
color={"red"}
fontWeight={"500"}
rounded={"md"}
position={"absolute"}
top={1}
right={0}
/>
<Text
as={"span"}
fontSize={"xs"}
w={"70%"}
fontWeight={"500"}
mt={1}
isTruncated={true}
>
{imageData?.name}
</Text>
<Text as={"span"} fontSize={"xs"} fontStyle={"italic"}>
{(imageData?.size / (1024 * 1024)).toFixed(2)} mb
</Text>
</Box>
2024-07-01 12:33:55 +05:30
</Box>
2024-07-05 12:07:52 +05:30
))}
2024-06-27 11:50:59 +05:30
</>
);
2024-07-24 19:58:15 +05:30
} else if (type === "table") {
return (
<Table w={"100%"} variant="simple">
2024-07-25 12:26:18 +05:30
<Thead>
<Tr>
{value?.map((item, index) => (
<Th
border={"none"}
p={2}
textTransform={"none"}
key={index}
>
<Box
as="span"
display={"flex"}
alignItems={"center"}
gap={2}
>
<Image
objectFit={"cover"}
opacity={0.9}
rounded={"full"}
w={6}
h={6}
src={
" https://admin.tanami.betadelivery.com/" +
item?.logo
}
/>
{item.country === "United Arab Emirates"
? "UAE"
: item.country}
2024-07-24 19:58:15 +05:30
</Box>
</Th>
))}
</Tr>
</Thead>
<Tbody>
<Tr>
2024-07-25 12:26:18 +05:30
{value?.map((item, index) => (
2024-07-24 19:58:15 +05:30
<Td
2024-07-25 12:26:18 +05:30
p={2}
2024-07-24 19:58:15 +05:30
color={"gray.600"}
style={{
whiteSpace: "nowrap",
textOverflow: "ellipsis",
}}
className="web-text-small"
key={index}
2024-07-25 12:26:18 +05:30
border={"none"}
2024-07-24 19:58:15 +05:30
>
2024-07-25 12:26:18 +05:30
<InputGroup size="sm">
<Input
isRequired={true}
bg={"#F5F8F6"}
focusBorderColor="forestGreen.300"
// border="1px solid #000"
size={"sm"}
fontSize={"sm"}
rounded={"sm"}
type="number"
value={item.value}
textAlign={"right"}
placeholder={"00.00"}
onChange={(e) =>
handleInputChange(index, e.target.value)
}
border={"1px solid #e2e8f0"}
/>
<InputRightAddon
fontSize={"xs"}
fontWeight={600}
color={"forestGreen.500"}
>
{item?.curr}
</InputRightAddon>
</InputGroup>
2024-07-24 19:58:15 +05:30
</Td>
))}
</Tr>
</Tbody>
</Table>
);
2024-06-27 11:50:59 +05:30
} else {
return (
<Input
2024-07-24 19:58:15 +05:30
bg={"#F5F8F6"}
2024-06-27 11:50:59 +05:30
focusBorderColor="forestGreen.300"
size={"sm"}
2024-07-05 12:07:52 +05:30
fontSize={"sm"}
2024-07-24 19:58:15 +05:30
rounded={"sm"}
2024-06-27 11:50:59 +05:30
type={type}
{...field}
{...props}
2024-07-01 12:33:55 +05:30
placeholder={placeHolder ? placeHolder : label}
textAlign={arabic || type === "number" ? "right" : align ? align : "left"}
2024-07-09 19:05:08 +05:30
_placeholder={{ fontSize: "sm" }}
2024-07-08 20:42:55 +05:30
min={type === "date" ? today : undefined}
2024-07-26 16:37:27 +05:30
maxLength={maxLength}
2024-07-26 13:38:48 +05:30
// defaultValue={type === "date" && "2023-07-26" : undefined}
// value={"2023-07-26"}
2024-06-27 11:50:59 +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>
)}
2024-07-01 12:33:55 +05:30
{helperText && (
<FormHelperText className="web-text-small">{helperText}</FormHelperText>
)}
{type === "file" && (
2024-06-27 11:50:59 +05:30
<FormHelperText className="web-text-small">
Maximum limit of image is 10MB.
</FormHelperText>
)}
</FormControl>
);
2024-06-24 12:08:21 +05:30
2024-06-26 17:45:13 +05:30
export default FormField;