import React, { useState, useEffect } from "react"; import { makeStyles } from "@material-ui/core/styles"; import { onComponentRender, onEventTrigger, onListItems } from "../Canvas/Canvas"; import { groupBy } from "lodash"; import { Grid } from "@material-ui/core"; import SourceBox from "../Canvas/SourceBox"; import SearchItems from "../Search"; import { group } from "console"; import Items from "../Items"; export interface drawerProps { name: string; // assuming name and title are same /** The list of items to show */ items: any[]; /** The component wrapper to use instead of the default for each item */ itemContainer?: React.ReactType; /** The style to use for each item */ itemContainerStyle?: object; /** The location of the drawer */ location: string; /** The title to show for the drawer */ title: string; /** Whether or not to show the title */ showTitle: boolean; /** Whether or not search is enabled */ searchIsEnabled: boolean; /** The component to show (instead of the default) for searching */ searchComponent?: React.ReactType; /** Whether or not drag and dropping is disabled */ disableDragAndDrop: boolean; /** Whether or not the close drawer button is showing */ closeButtonIsShowing: boolean; /** The component to use (instead of the default) for the close button */ closeButton?: React.ReactType; /** The style to use for the item drawer */ style?: object; /** The common name shared between the drag and drop source and the drag and drop target */ dndName: string; } /** * Component to render drawer */ const Drawer = (props: any) => { /** Call to onComponentRender hook when drawer is rendered*/ onComponentRender("ItemDrawer"); const styles: any = useStyles(); const [openDrawer, setOpenDrawer] = useState(false); const [arrayCat, setArrayCat] = useState([]); /** * On click of button opens and closes the drawer */ const onPressButton = () => { console.log("onPressButton", openDrawer); setOpenDrawer(!openDrawer); }; /** * On click of button closes the drawer */ const onClose = () => { onEventTrigger("ItemDrawerClose"); setOpenDrawer(false); }; /** * This function is used to group an array based on key sent in params * @param xs - array list * @param key - based on which the array will be grouped */ const groupBy = (xs: any, key: string) => { if (xs.length > 0) { return xs.reduce(function (rv: any, x: any) { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {}); } else { return []; } }; /** * This hook is used to update the arrayCat state on change in items prop */ useEffect(() => { const itemData = groupBy(props.items, "group"); setArrayCat(itemData); }, [props.items]); /** * Function is called on item search * @param event */ const onSearch = (event: any) => { onEventTrigger("ItemDrawerSearch"); let searchText = event.target.value.trim(); const arraySearchedResults = props.items.filter(function (item: any) { return item.name.toLowerCase().includes(searchText.toLowerCase()); }); setArrayCat(groupBy(arraySearchedResults, "group")); }; /** Reset serach in case any filter is added */ const resetSearch = () => { setArrayCat(groupBy(props.items, "group")); }; return ( <> {openDrawer && (