diff --git a/package.json b/package.json index 9518bc4..c918f4e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@weng-lab/psychscreen-ui-components", "description": "Typescript and Material UI based components used for psychSCREEN", "author": "SCREEN Team @ UMass Chan Medical School", - "version": "2.0.7", + "version": "2.0.8", "license": "MIT", "type": "module", "typings": "dist/index.d.ts", diff --git a/src/components/DataTable/DataTable.stories.tsx b/src/components/DataTable/DataTable.stories.tsx index 3acd1cd..cebc726 100644 --- a/src/components/DataTable/DataTable.stories.tsx +++ b/src/components/DataTable/DataTable.stories.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import { Meta, StoryObj } from '@storybook/react'; import { DataTable, DataTableColumn, DataTableProps } from '../..'; import { @@ -7,15 +7,13 @@ import { Checkbox, FormControlLabel, FormGroup, + IconButton, Menu, MenuItem, Stack, Typography, } from '@mui/material'; - -/** - * @todo These stories should really be cleaned up, especially dense and headerRender - */ +import { Close } from '@mui/icons-material'; const meta = { title: 'DataTable', @@ -38,21 +36,51 @@ type Row = { export default meta; type Story = StoryObj>; +const CountComponent = (props: {info?: Row}) => { + const [count, setCount] = useState(0) + return {setCount(count + 1); if (props?.info?.index) console.log(props.info.index)}}>Click to increase count: {count} +} -const FCCOLUMNS: DataTableColumn[] = [ +const FunctionalRenderCols: DataTableColumn[] = [ { header: 'Functional Column', value: (row) => row.index, - FunctionalRender: () => { - const [index, setIndex] = useState(0); + render: (row) => { + const [count, setCount] = useState(0); + return ( <> - Index: {index} + Row {row.index}: {count}
- + ); - }, + } + }, + { + header: 'Functional Column', + value: (row) => row.index, + render: (row) => { + const [count, setCount] = useState(0); + + return ( + <> + Row {row.index}: {count} +
+ + + ); + } + }, + { + header: 'Functional Column', + value: (row) => row.index, + render: (row) => + }, + { + header: 'Functional Column', + value: (row) => row.index, + render: (row) => }, ]; @@ -86,6 +114,7 @@ const COLUMNS2: DataTableColumn[] = [ { header: 'Index', value: (row) => row.index, + HeaderRender: () => Index, tooltip: 'This is the index column', }, { @@ -223,14 +252,14 @@ export const HoverInfo: Story = { } } -export const FunctionalComponentColumn: Story = { +export const FunctionalRender: Story = { args: { rows: ROWS, - columns: FCCOLUMNS, + columns: FunctionalRenderCols, tableTitle: 'Table Title', searchable: true, showMoreColumns: true, - noOfDefaultColumns: 5, + itemsPerPage: 5, } } @@ -448,7 +477,106 @@ export const LotsOfCols: Story = { } } -const headeRenderCOLUMNS = (setX: React.Dispatch>) => { +const HeaderComponent = (props: {setX: React.Dispatch>}) => { + const [distanceChecked, setDistanceChecked] = useState(true); + const [CTCF_ChIAPETChecked, setCTCF_ChIAPETChecked] = useState(false); + const [RNAPII_ChIAPETChecked, setRNAPII_ChIAPETChecked] = + useState(false); + const [anchorEl, setAnchorEl] = useState(() => { + console.log('initialization performed'); + return null; + }); + + const open = Boolean(anchorEl); + + const handleClose = () => { + console.log('menu closed, state of checkboxes pushed to main state'); + setAnchorEl(null); + }; + + const handleClick = (event: React.MouseEvent) => { + console.log('New Anchor Set'); + setAnchorEl(event.currentTarget); + }; + + const handleCheckboxChange = ( + event: React.ChangeEvent, + value: 0 | 1 | 2 + ) => { + switch (value) { + case 0: + setDistanceChecked(event.target.checked); + props.setX(event.target.checked); + break; + case 1: + setCTCF_ChIAPETChecked(event.target.checked); + break; + case 2: + setRNAPII_ChIAPETChecked(event.target.checked); + break; + } + }; + + return ( + + + Linked Genes + + + + + handleCheckboxChange(event, 0)} + /> + } + label={`Distance`} + /> + + + handleCheckboxChange(event, 1)} + /> + } + label={`CTCF-ChIAPET`} + /> + + + handleCheckboxChange(event, 2)} + /> + } + label={`RNAPII-ChIAPET`} + /> + + + + + ); +} + +const headeRenderCOLUMNS: (setX: React.Dispatch>) => DataTableColumn[] = (setX) => { return [ { header: 'Index', @@ -475,104 +603,7 @@ const headeRenderCOLUMNS = (setX: React.Dispatch row.description, unsortable: true, tooltip: 'This is the Description column. It describes the row.', - HeaderRender: () => { - const [distanceChecked, setDistanceChecked] = useState(true); - const [CTCF_ChIAPETChecked, setCTCF_ChIAPETChecked] = useState(false); - const [RNAPII_ChIAPETChecked, setRNAPII_ChIAPETChecked] = - useState(false); - const [anchorEl, setAnchorEl] = useState(() => { - console.log('initialization performed'); - return null; - }); - - const open = Boolean(anchorEl); - - const handleClose = () => { - console.log('menu closed, state of checkboxes pushed to main state'); - setAnchorEl(null); - }; - - const handleClick = (event: React.MouseEvent) => { - console.log('New Anchor Set'); - setAnchorEl(event.currentTarget); - }; - - const handleCheckboxChange = ( - event: React.ChangeEvent, - value: 0 | 1 | 2 - ) => { - switch (value) { - case 0: - setDistanceChecked(event.target.checked); - setX(event.target.checked); - break; - case 1: - setCTCF_ChIAPETChecked(event.target.checked); - break; - case 2: - setRNAPII_ChIAPETChecked(event.target.checked); - break; - } - }; - - return ( - - - Linked Genes - - - - - handleCheckboxChange(event, 0)} - /> - } - label={`Distance`} - /> - - - handleCheckboxChange(event, 1)} - /> - } - label={`CTCF-ChIAPET`} - /> - - - handleCheckboxChange(event, 2)} - /> - } - label={`RNAPII-ChIAPET`} - /> - - - - - ); - }, + HeaderRender: () => , }, ]; }; @@ -580,7 +611,6 @@ const headeRenderCOLUMNS = (setX: React.Dispatch { const [x, setX] = React.useState(null); useEffect(() => console.log(x)); + + const colFunc = useCallback((setter: React.Dispatch>) => { + return headeRenderCOLUMNS(setter) + }, []) + + console.log("rendering") + return ( ) } +} + +export const TitleRender: Story = { + args: { + rows: ROWS, + columns: COLUMNS, + searchable: true + }, + render: (args) => { + return ( + + Normal String Title + + Custom Typography Title + Small title} + /> + Component title with onClick + {"Click the button -->"} window.alert("Clicked!")}> } + /> + Function Component title using hooks + } + /> + + + ) + } } \ No newline at end of file diff --git a/src/components/DataTable/datatable.tsx b/src/components/DataTable/datatable.tsx index 400f612..ba4af21 100644 --- a/src/components/DataTable/datatable.tsx +++ b/src/components/DataTable/datatable.tsx @@ -101,9 +101,10 @@ const boxStyle = { p: 4, }; -const DataTable = ( +const DataTable = ( props: DataTableProps ) => { + const [page, setPage] = useState(props.page || 0); const [rowsPerPage, setRowsPerPage] = useState(() => { if (Array.isArray(props.itemsPerPage)) { return props.itemsPerPage[0] } @@ -291,8 +292,6 @@ const DataTable = ( } }, [containerRef, arrowLeftRef, arrowRightRef]); - console.log(document.getElementById('row0')?.offsetHeight) - return ( (( borderTopRightRadius: 4, }} > - - {props.tableTitle} - {props.titleHoverInfo && ( - + {(typeof props.tableTitle === "string" ? + - - - )} - + {props.tableTitle} + + : + props.tableTitle) + } + {props.titleHoverInfo && ( + + + + )} + {props.showMoreColumns && props.columns.length > (props.noOfDefaultColumns || 5) && (