From 064123c013a4461e5183ab18b6b0ab4c334daee9 Mon Sep 17 00:00:00 2001 From: Samuel Rowe Date: Sat, 30 Jul 2022 18:52:30 +0530 Subject: [PATCH] feat(frontend): updated `Records` to be collapsible --- services/frontend/src/components/Records.tsx | 64 ++++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/services/frontend/src/components/Records.tsx b/services/frontend/src/components/Records.tsx index a9b18ce..c431233 100644 --- a/services/frontend/src/components/Records.tsx +++ b/services/frontend/src/components/Records.tsx @@ -1,6 +1,16 @@ import { styled } from "@mui/joy"; -import { Fragment, FunctionComponent, ReactElement, useCallback } from "react"; -import { PlusIcon } from "@heroicons/react/outline"; +import { + Fragment, + FunctionComponent, + ReactElement, + useCallback, + useState +} from "react"; +import { + ChevronDownIcon, + ChevronUpIcon, + PlusIcon +} from "@heroicons/react/outline"; import Record, { IFieldType } from "./Record"; import { useFormikContext } from "formik"; import lodash from "lodash"; @@ -29,6 +39,7 @@ const Group = styled("div", { flex-direction: column; align-items: ${({ empty }) => (empty ? "center" : "flex-end")}; row-gap: ${({ theme }) => theme.spacing(1)}; + width: 100%; `; const GroupTitle = styled("h5")` @@ -62,6 +73,23 @@ const Description = styled("p")` width: 100%; `; +const Top = styled("div")` + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + width: 100%; + + &:hover { + cursor: pointer; + user-select: none; + } +`; + +const ExpandButton = styled(IconButton)` + border-radius: ${({ theme }) => theme.spacing(2)}; +`; + const Records: FunctionComponent = ( props: IRecordsProps ): ReactElement => { @@ -77,12 +105,14 @@ const Records: FunctionComponent = ( renderBorder } = props; + const [open, setOpen] = useState(false); + const formik = useFormikContext(); const items = lodash.get(formik.values, name); - if (!items) { - throw new Error(`"${name}" is falsy.`); - } + const handleToggle = useCallback(() => { + setOpen((open) => !open); + }, []); const handleNew = useCallback(() => { formik.setFieldValue(`${name}[${items.length}]`, newValue); @@ -98,12 +128,22 @@ const Records: FunctionComponent = ( [formik] ); + if (!items) { + throw new Error(`"${name}" is falsy.`); + } + const empty = items && items.length === 0; return ( - {title} - {!empty && ( + + {title} + + {open && } + {!open && } + + + {open && !empty && ( {items.map((_: unknown, index: number) => ( @@ -121,11 +161,13 @@ const Records: FunctionComponent = ( )} - {empty && No items available} + {open && empty && No items available} - - - + {open && ( + + + + )} ); };