-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Closes #355
- Loading branch information
1 parent
13b6b37
commit 8326375
Showing
6 changed files
with
124 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import cn from 'classnames' | ||
import { createUseStyles } from 'react-jss' | ||
import Drawer from './Drawer' | ||
import isEmpty from 'lodash/isEmpty' | ||
import { TableDrawerProps } from './index' | ||
import { useModal } from 'components/Modal' | ||
import { useWindowSize } from 'components/Table/useWindowSize' | ||
import { DataId, Table } from '../Table' | ||
import React, { useState } from 'react' | ||
import { styleguide, themes, ThemeType } from '../assets/styles' | ||
|
||
const { borderRadius, spacing } = styleguide | ||
const { dark, light } = ThemeType | ||
|
||
const useStyles = createUseStyles({ | ||
container: { | ||
borderRadius, | ||
display: 'flex', | ||
height: '100%', | ||
overflowX: 'auto', | ||
width: '100%' | ||
}, | ||
drawer: { | ||
flex: 1, | ||
minWidth: 300, | ||
overflow: 'auto', | ||
wordBreak: 'break-word' | ||
}, | ||
drawerOpen: { | ||
borderLeft: `1px solid ${themes[light].border}` | ||
}, | ||
table: { | ||
padding: `${spacing.m}px ${spacing.l}px` | ||
}, | ||
tableContainer: { | ||
flex: 2.5, | ||
overflow: 'auto', | ||
padding: spacing.m, | ||
position: 'relative' | ||
}, | ||
// eslint-disable-next-line sort-keys | ||
'@global': { | ||
[`.${dark}`]: { | ||
'& $drawerOpen': { | ||
borderLeft: `1px solid ${themes[dark].border}` | ||
} | ||
} | ||
} | ||
}) | ||
|
||
export const TableDrawer = <DataType extends DataId>({ | ||
containerId, | ||
drawerContainerClasses = [], | ||
renderDrawerCmp, | ||
tableContainerClasses = [], | ||
...rest | ||
}: TableDrawerProps<DataType>) => { | ||
const [rowData, setRowData] = useState({} as DataType) | ||
const resetRowData = () => setRowData({} as DataType) | ||
const { isMobile } = useWindowSize() | ||
const { setModalConfig } = useModal() | ||
|
||
const isRowEmpty = isEmpty(rowData) | ||
|
||
const classes = useStyles() | ||
const drawerClasses = cn( | ||
{ | ||
[classes.drawer]: true, | ||
[classes.drawerOpen]: !isRowEmpty | ||
}, | ||
drawerContainerClasses | ||
) | ||
|
||
const onRowClick = (clickedRowData: DataType) => { | ||
isMobile && | ||
setModalConfig({ | ||
content: renderDrawerCmp(rowData.id, clickedRowData) | ||
}) | ||
|
||
return rowData.id === clickedRowData.id | ||
? resetRowData() | ||
: setRowData(clickedRowData) | ||
} | ||
|
||
return ( | ||
<div className={classes.container} id={containerId}> | ||
<div | ||
className={cn( | ||
{ [classes.tableContainer]: true }, | ||
tableContainerClasses | ||
)} | ||
> | ||
<Table<DataType> | ||
activeRowKey={rowData.id} | ||
onRowClick={onRowClick} | ||
{...rest} | ||
/> | ||
</div> | ||
{!isMobile && !isRowEmpty && ( | ||
<Drawer classes={[drawerClasses]} onClose={resetRowData}> | ||
{renderDrawerCmp(rowData.id, rowData)} | ||
</Drawer> | ||
)} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,21 @@ | ||
import cn from 'classnames' | ||
import { createUseStyles } from 'react-jss' | ||
import Drawer from './Drawer' | ||
import isEmpty from 'lodash/isEmpty' | ||
import { DataId, Table, TableProps } from '../Table' | ||
import React, { Key, ReactNode, useState } from 'react' | ||
import { styleguide, themes, ThemeType } from '../assets/styles' | ||
|
||
const { borderRadius, spacing } = styleguide | ||
const { dark, light } = ThemeType | ||
|
||
const useStyles = createUseStyles({ | ||
container: { | ||
borderRadius, | ||
display: 'flex', | ||
height: '100%', | ||
overflowX: 'auto', | ||
width: '100%' | ||
}, | ||
drawer: { | ||
flex: 1, | ||
minWidth: 300, | ||
overflow: 'auto', | ||
wordBreak: 'break-word' | ||
}, | ||
drawerOpen: { | ||
borderLeft: `1px solid ${themes[light].border}` | ||
}, | ||
table: { | ||
padding: `${spacing.m}px ${spacing.l}px` | ||
}, | ||
tableContainer: { | ||
flex: 2.5, | ||
overflow: 'auto', | ||
padding: spacing.m, | ||
position: 'relative' | ||
}, | ||
// eslint-disable-next-line sort-keys | ||
'@global': { | ||
[`.${dark}`]: { | ||
'& $drawerOpen': { | ||
borderLeft: `1px solid ${themes[dark].border}` | ||
} | ||
} | ||
} | ||
}) | ||
import { ModalProvider } from 'components/Modal' | ||
import { TableDrawer as TableDrawerCmp } from './TableDrawer' | ||
import { DataId, TableProps } from '../Table' | ||
import React, { Key, ReactNode } from 'react' | ||
|
||
export interface TableDrawerProps<DataType> | ||
extends Omit<TableProps<DataType>, 'activeRowKey' | 'onRowClick'> { | ||
containerId?: string | ||
drawerContainerClasses?: string[] | ||
renderDrawerCmp: (id: Key, rowData: DataType) => ReactNode | ||
tableContainerClasses?: string[] | ||
} | ||
|
||
export const TableDrawer = <DataType extends DataId>({ | ||
drawerContainerClasses = [], | ||
renderDrawerCmp, | ||
tableContainerClasses = [], | ||
containerId = 'table-drawer-wrapper', | ||
...rest | ||
}: TableDrawerProps<DataType>) => { | ||
const [rowData, setRowData] = useState({} as DataType) | ||
const resetRowData = () => setRowData({} as DataType) | ||
|
||
const isRowEmpty = isEmpty(rowData) | ||
|
||
const classes = useStyles() | ||
const drawerClasses = cn( | ||
{ | ||
[classes.drawer]: true, | ||
[classes.drawerOpen]: !isRowEmpty | ||
}, | ||
drawerContainerClasses | ||
) | ||
|
||
const onRowClick = (clickedRowData: DataType) => | ||
rowData.id === clickedRowData.id | ||
? resetRowData() | ||
: setRowData(clickedRowData) | ||
|
||
return ( | ||
<div className={classes.container}> | ||
<div | ||
className={cn( | ||
{ [classes.tableContainer]: true }, | ||
tableContainerClasses | ||
)} | ||
> | ||
<Table<DataType> | ||
activeRowKey={rowData.id} | ||
onRowClick={onRowClick} | ||
{...rest} | ||
/> | ||
</div> | ||
{!isRowEmpty && ( | ||
<Drawer classes={[drawerClasses]} onClose={resetRowData}> | ||
{renderDrawerCmp(rowData.id, rowData)} | ||
</Drawer> | ||
)} | ||
</div> | ||
) | ||
} | ||
}: TableDrawerProps<DataType>) => ( | ||
<ModalProvider popupContainerSelector={containerId}> | ||
<TableDrawerCmp containerId={containerId} {...rest} /> | ||
</ModalProvider> | ||
) |