Added input validation

the alert level widget now has visual and logical input validation
This commit is contained in:
2025-08-11 12:59:38 -07:00
parent 5df556896f
commit 8116745921
3 changed files with 30 additions and 13 deletions
@@ -13,7 +13,7 @@ export default function AddWidgetMenu({appendWidget}) {
<Menu.Content> <Menu.Content>
<Menu.Item <Menu.Item
value="AlertLevel" value="AlertLevel"
onClick={() => appendWidget(AlertLevel, 3, 6, 3, 6, 3, 6)} // width and height are passed through here for each widget onClick={() => appendWidget(AlertLevel, 4, 7, 4, 7, 4, 7)} // width and height are passed through here for each widget
> >
Alert Level Alert Level
</Menu.Item> </Menu.Item>
@@ -1,13 +1,18 @@
import Widget from "./Widget"; import Widget from "./Widget";
import { Stack, Box, Field, Input, Button, Text } from "@chakra-ui/react"; import { Stack, Box, Field, Input, Button, Text, Flex } from "@chakra-ui/react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { useState } from "react"; import { useState } from "react";
import { LuAsterisk } from "react-icons/lu";
export default function AlertLevel({id, isEditable, isDelete, DeleteWidget}) { export default function AlertLevel({id, isEditable, isDelete, DeleteWidget}) {
const { register, handleSubmit } = useForm(); const { register, handleSubmit, reset, formState: {errors} } = useForm();
const onSubmit = (data) => { const onSubmit = (data) => {
console.log(data); console.log("data: ", data);
if (data.volcanoname === '') {
return null;
}
setIsSubmit(true); setIsSubmit(true);
reset();
} }
const [isSubmit, setIsSubmit] = useState(false); const [isSubmit, setIsSubmit] = useState(false);
@@ -16,6 +21,7 @@ export default function AlertLevel({id, isEditable, isDelete, DeleteWidget}) {
return ( return (
<Widget title="Alert Level" {...widgetProps}> <Widget title="Alert Level" {...widgetProps}>
{!isSubmit ? ( {!isSubmit ? (
// DISPLAYING INPUT FIElDS AND LABELS
<Box <Box
as="form" as="form"
onSubmit={handleSubmit(onSubmit)} onSubmit={handleSubmit(onSubmit)}
@@ -26,23 +32,34 @@ export default function AlertLevel({id, isEditable, isDelete, DeleteWidget}) {
pr={3} pr={3}
display="flex" display="flex"
flexDir="column" flexDir="column"
gap="3" gap="10"
> >
<Field.Root orientation="horizontal" > <Field.Root orientation="horizontal">
<Stack direction="column" gap="3" width="100%"> <Stack direction="column" gap="3" width="100%">
<Stack direction="row" display="flex" flexShrink={1} spacing={4} align="center" width="100%"> <Stack direction="row" width="100%">
<Field.Label whiteSpace="nowrap" fontWeight="bold">Country Name</Field.Label> <Field.Label whiteSpace="nowrap" fontWeight="bold">Country Name</Field.Label>
{/* place select drop down menu here instead of asking for input */}
<Input {...register("countryname")} placeholder="Enter country name"/> <Input {...register("countryname")} placeholder="Enter country name"/>
</Stack> </Stack>
<Stack direction="row" display="flex" spacing={4} align="center" width="100%"> <Flex position="relative" dir="column" gap="0.5" width="100%">
<Field.Label display="flex" whiteSpace="nowrap" fontWeight="bold">Volcano Name</Field.Label> <Stack direction="row" width="100%">
<Input {...register("countryname")} placeholder="Enter volcano name"/> <Field.Label display="flex" whiteSpace="nowrap" fontWeight="bold">
Volcano Name <LuAsterisk color="tomato" />
</Field.Label>
<Input
border={errors.volcanoname ? "2px solid" : undefined}
borderColor={errors.volcanoname ? "tomato" : undefined}
{...register("volcanoname", { required: 'This field is required.' })}
placeholder="Enter volcano name"/>
</Stack> </Stack>
{errors.volcanoname && <Box cursor="default" userSelect="none" position="absolute" top="40px" left="8.5rem" style={{ color: 'tomato' }}>{errors.volcanoname.message}</Box>}
</Flex>
</Stack> </Stack>
</Field.Root> </Field.Root>
<Button type="submit" size="sm" onClick={() => setIsSubmit(true)}>Submit</Button> <Button type="submit" size="sm">Submit</Button>
</Box> </Box>
):( ):(
// DISPLAYING ALERT LEVEL DATA
<Box p={1} mt={1} ml="auto" mr="auto" width="200px"> <Box p={1} mt={1} ml="auto" mr="auto" width="200px">
<Box <Box
bg="bg" bg="bg"
@@ -30,7 +30,7 @@ export default function MyDashboard({layout, onLayoutChange, isEditable, isDelet
useCSSTransforms={false} // remove the sliding animation of the widgets useCSSTransforms={false} // remove the sliding animation of the widgets
className="layout" className="layout"
layout={layout} layout={layout}
cols={16} // Total columns in grid cols={15} // Total columns in grid
rowHeight={30} // Height of a single row in px rowHeight={30} // Height of a single row in px
isBounded={true} // Total width of the grid isBounded={true} // Total width of the grid
margin={[15, 5]} // [horizontal, vertical] gap margin={[15, 5]} // [horizontal, vertical] gap