-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* table in progress * in progress * in progress * table complete * fixed header route + reverted changes * added alignment support
- Loading branch information
1 parent
a77f285
commit 17108bc
Showing
9 changed files
with
263 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { FC, useMemo } from 'react'; | ||
import { | ||
Table as ReactTable, | ||
Header, | ||
HeaderRow, | ||
Body, | ||
Row, | ||
HeaderCell, | ||
Cell, | ||
} from '@table-library/react-table-library/table'; | ||
import { useTheme } from '@table-library/react-table-library/theme'; | ||
import styled from 'styled-components'; | ||
import { textVariants } from '../text'; | ||
import { SurfaceColors } from '../theme/Theme.types'; | ||
import { Column, DataSource } from './Table.types'; | ||
|
||
export type TableProps = { | ||
columns: Column[]; | ||
dataSource: DataSource[]; | ||
fixedHeader?: boolean; | ||
backgroundColor?: SurfaceColors; | ||
}; | ||
|
||
const StyledHeaderCell = styled(HeaderCell)<{ headerAlignment?: Column['headerAlignment'] }>` | ||
${({ headerAlignment }) => | ||
headerAlignment | ||
? ` | ||
div { | ||
display: flex; | ||
justify-content: ${headerAlignment}; | ||
} | ||
` | ||
: ''} | ||
`; | ||
|
||
const StyledRowCell = styled(Cell)<{ cellAlignment?: Column['cellAlignment'] }>` | ||
${({ cellAlignment }) => | ||
cellAlignment | ||
? ` | ||
div { | ||
display: flex; | ||
justify-content: ${cellAlignment}; | ||
} | ||
` | ||
: ''} | ||
`; | ||
|
||
const Table: FC<TableProps> = ({ backgroundColor = 'surface-secondary', columns, dataSource, fixedHeader = false }) => { | ||
const columnData = useMemo(() => { | ||
const columnWidths = columns.map((col) => col.width || `${100 / columns.length}%`); | ||
|
||
const leftRightPositionCSS = columns | ||
.map((col, index) => { | ||
if (col?.fixed == 'left') { | ||
return ` | ||
&:nth-of-type(${index + 1}) { | ||
left: ${index === 0 ? '0px' : columnWidths[index]}; | ||
}; | ||
`; | ||
} | ||
if (col?.fixed == 'right') { | ||
return ` | ||
&:nth-of-type(${index + 1}) { | ||
right: ${index + 1 === columns.length ? '0px' : columnWidths[index]}; | ||
}; | ||
`; | ||
} | ||
|
||
return ''; | ||
}) | ||
.join(''); | ||
|
||
return { | ||
columnWidthCSS: columnWidths.join(' '), | ||
leftRightPositionCSS, | ||
}; | ||
}, [columns]); | ||
|
||
const theme = useTheme({ | ||
Table: ` | ||
--data-table-library_grid-template-columns: ${columnData.columnWidthCSS}; | ||
`, | ||
BaseCell: ` | ||
${columnData.leftRightPositionCSS} | ||
`, | ||
Cell: ` | ||
align-items: center; | ||
align-self: stretch; | ||
border-bottom: var(--border-sm) solid var(--components-table-stroke-default); | ||
color: var(--components-table-text-default); | ||
display: flex; | ||
flex: 1 0 0; | ||
font-family: var(--font-family); | ||
font-size: ${textVariants['bs-semibold'].fontSize}; | ||
font-style: ${textVariants['bs-semibold'].fontStyle}; | ||
font-weight: ${textVariants['bs-semibold'].fontWeight}; | ||
line-height: ${textVariants['bs-semibold'].lineHeight}; | ||
gap: var(--spacing-xxs); | ||
height: 56px; | ||
padding: var(--spacing-xxxs); | ||
`, | ||
Row: ` | ||
background: var(--${backgroundColor}); | ||
`, | ||
HeaderRow: ` | ||
background: var(--${backgroundColor}); | ||
`, | ||
HeaderCell: ` | ||
align-items: center; | ||
color: var(--components-table-text-heading); | ||
display: flex; | ||
font-family: var(--font-family); | ||
font-size: ${textVariants['c-bold'].fontSize}; | ||
font-style: ${textVariants['c-bold'].fontStyle}; | ||
font-weight: ${textVariants['c-bold'].fontWeight}; | ||
line-height: ${textVariants['c-bold'].lineHeight}; | ||
padding: var(--spacing-xxs) var(--spacing-xxxs); | ||
gap: 10px; | ||
`, | ||
}); | ||
|
||
return ( | ||
<ReactTable | ||
data={{ | ||
nodes: dataSource, | ||
}} | ||
theme={theme} | ||
layout={{ custom: true, horizontalScroll: true, fixedHeader }} | ||
> | ||
{(tableList: DataSource[]) => ( | ||
<> | ||
<Header> | ||
<HeaderRow> | ||
{columns.map((column, index) => ( | ||
<StyledHeaderCell | ||
headerAlignment={column.headerAlignment} | ||
key={`${column.title}-${index}`} | ||
pinLeft={column?.fixed === 'left'} | ||
pinRight={column?.fixed === 'right'} | ||
> | ||
{column.title} | ||
</StyledHeaderCell> | ||
))} | ||
</HeaderRow> | ||
</Header> | ||
|
||
<Body> | ||
{tableList.map((record) => ( | ||
<Row | ||
key={record.id} | ||
item={record} | ||
> | ||
{columns.map((column) => { | ||
const cellValue = `${record?.[column.dataIndex] || ''}`; | ||
return ( | ||
<StyledRowCell | ||
cellAlignment={column.cellAlignment} | ||
key={`${column.dataIndex}-${record.id}`} | ||
pinLeft={column?.fixed === 'left'} | ||
pinRight={column?.fixed === 'right'} | ||
> | ||
{column.render ? column.render(cellValue, record) : cellValue} | ||
</StyledRowCell> | ||
); | ||
})} | ||
</Row> | ||
))} | ||
</Body> | ||
</> | ||
)} | ||
</ReactTable> | ||
); | ||
}; | ||
|
||
export { Table }; |
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,16 @@ | ||
import { CSSProperties, ReactNode } from 'react'; | ||
|
||
export type Column = { | ||
title: string; | ||
dataIndex: string; | ||
cellAlignment?: CSSProperties['justifyContent']; | ||
headerAlignment?: CSSProperties['justifyContent']; | ||
render?: (text: string, record: any) => ReactNode; | ||
width?: string; | ||
fixed?: 'left' | 'right'; | ||
}; | ||
|
||
export type DataSource = { | ||
id: string; | ||
[key: string]: any; | ||
}; |
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 @@ | ||
export * from './Table'; |
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,14 @@ | ||
import { iconSemantics } from './semantics.icon'; | ||
import { strokeSemantics } from './semantics.stroke'; | ||
import { textSemantics } from './semantics.text'; | ||
|
||
export const tableSemantics = { | ||
'stroke-default': { light: strokeSemantics['tertiary'].light, dark: strokeSemantics['tertiary'].dark }, | ||
|
||
'text-default': { light: textSemantics['primary'].light, dark: textSemantics['primary'].dark }, | ||
'text-heading': { light: textSemantics['tertiary'].light, dark: textSemantics['tertiary'].dark }, | ||
'text-disabled': { light: textSemantics['state-disabled'].light, dark: textSemantics['state-disabled'].dark }, | ||
|
||
'icon-default': { light: iconSemantics['primary'].light, dark: iconSemantics['primary'].dark }, | ||
'icon-disabled': { light: iconSemantics['state-disabled'].light, dark: iconSemantics['state-disabled'].dark }, | ||
}; |
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