Skip to content

Commit

Permalink
feat: Table improvements (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm authored Aug 1, 2024
1 parent da12ac7 commit 33dab53
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
27 changes: 24 additions & 3 deletions src/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import styled, { useTheme } from 'styled-components'
import { isEmpty, isNil } from 'lodash-es'

import usePrevious from '../hooks/usePrevious'
import { InfoOutlineIcon, Tooltip } from '../index'

import Button from './Button'
import CaretUpIcon from './icons/CaretUpIcon'
Expand All @@ -49,6 +50,7 @@ export type TableProps = DivProps & {
hideHeader?: boolean
padCells?: boolean
rowBg?: 'base' | 'raised' | 'stripes'
highlightedRowId?: string
getRowCanExpand?: any
renderExpanded?: any
loose?: boolean
Expand Down Expand Up @@ -144,6 +146,7 @@ const Tbody = styled(TbodyUnstyled)(({ theme }) => ({
}))

const Tr = styled.tr<{
$highlighted?: boolean
$selected?: boolean
$selectable?: boolean
$clickable?: boolean
Expand All @@ -155,9 +158,12 @@ const Tr = styled.tr<{
$raised: raised = false,
$selectable: selectable = false,
$selected: selected = false,
$highlighted: highlighted = false,
}) => ({
display: 'contents',
backgroundColor: selected
backgroundColor: highlighted
? theme.colors['fill-two']
: selected
? theme.colors['fill-zero-hover']
: raised || (selectable && !selected)
? theme.colors['fill-zero-selected']
Expand All @@ -179,12 +185,14 @@ const Tr = styled.tr<{

const Th = styled.th<{
$stickyColumn: boolean
$highlight?: boolean
$cursor?: CSSProperties['cursor']
$hideHeader?: boolean
}>(
({
theme,
$stickyColumn: stickyColumn,
$highlight: highlight,
$cursor: cursor,
$hideHeader: hideHeader,
}) => ({
Expand All @@ -196,7 +204,9 @@ const Th = styled.th<{
alignItems: 'center',
display: hideHeader ? 'none' : 'flex',
position: 'relative',
backgroundColor: theme.colors['fill-one'],
backgroundColor: highlight
? theme.colors['fill-two']
: theme.colors['fill-one'],
zIndex: 4,
borderBottom: theme.borders.default,
color: theme.colors.text,
Expand Down Expand Up @@ -248,6 +258,7 @@ const Td = styled.td<{
$loose?: boolean
$padCells?: boolean
$stickyColumn: boolean
$highlight?: boolean
$truncateColumn: boolean
$center?: boolean
}>(
Expand All @@ -257,6 +268,7 @@ const Td = styled.td<{
$loose: loose,
$padCells: padCells,
$stickyColumn: stickyColumn,
$highlight: highlight,
$truncateColumn: truncateColumn = false,
$center: center,
}) => ({
Expand All @@ -268,7 +280,7 @@ const Td = styled.td<{
height: 'auto',
minHeight: 52,

backgroundColor: 'inherit',
backgroundColor: highlight ? theme.colors['fill-two'] : 'inherit',
borderTop: firstRow ? '' : theme.borders.default,
color: theme.colors['text-light'],

Expand Down Expand Up @@ -528,6 +540,7 @@ function TableRef(
lockColumnsOnScroll,
reactVirtualOptions,
reactTableOptions,
highlightedRowId,
onRowClick,
emptyStateProps,
hasNextPage,
Expand Down Expand Up @@ -703,6 +716,7 @@ function TableRef(
key={header.id}
$hideHeader={hideHeader}
$stickyColumn={stickyColumn}
$highlight={header.column.columnDef?.meta?.highlight}
{...(header.column.getCanSort()
? {
$cursor:
Expand All @@ -725,6 +739,11 @@ function TableRef(
header.getContext()
)}
</div>
{header.column.columnDef.meta?.tooltip && (
<Tooltip label={header.column.columnDef.meta.tooltip}>
<InfoOutlineIcon />
</Tooltip>
)}
<SortIndicator
direction={header.column.getIsSorted()}
/>
Expand Down Expand Up @@ -764,6 +783,7 @@ function TableRef(
key={key}
onClick={(e) => onRowClick?.(e, row)}
$raised={raised}
$highlighted={row.id === highlightedRowId}
$selectable={row?.getCanSelect() ?? false}
$selected={row?.getIsSelected() ?? false}
$clickable={!!onRowClick}
Expand Down Expand Up @@ -795,6 +815,7 @@ function TableRef(
$padCells={padCells}
$loose={loose}
$stickyColumn={stickyColumn}
$highlight={cell.column?.columnDef?.meta?.highlight}
$truncateColumn={
cell.column?.columnDef?.meta?.truncate
}
Expand Down
41 changes: 31 additions & 10 deletions src/stories/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import {
AppIcon,
ArrowRightLeftIcon,
CollapseIcon,
IconFrame,
InfoIcon,
LogsIcon,
Table,
Tooltip,
Expand Down Expand Up @@ -145,20 +143,15 @@ const columns = [
columnHelper.accessor((row) => row.description, {
id: 'description',
enableGlobalFilter: true,
meta: { tooltip: 'Tooltip message' },
cell: (info: any) => <span>{info.getValue()}</span>,
header: () => (
<Flex
gap="xsmall"
alignItems="center"
justifyContent="space-between"
>
Description{' '}
<IconFrame
clickable
textValue="Book"
icon={<InfoIcon />}
size="small"
/>
Description
</Flex>
),
}),
Expand Down Expand Up @@ -365,14 +358,42 @@ const extremeLengthData = Array(200)
.map((item, i) => ({ ...item, id: `id-${i}` }))

export const Default = Template.bind({})

Default.args = {
width: '900px',
height: '400px',
data: repeatedData,
columns,
}

export const Highlighted = Template.bind({})
Highlighted.args = {
width: '900px',
height: '400px',
data: repeatedData,
columns: (() => {
const c = [...columns]

c.splice(
2,
0,
columnHelper.accessor((row) => row.id, {
id: 'h',
cell: ({ getValue }) => getValue(),
header: 'Highlight',
meta: {
highlight: true,
truncate: true,
gridTemplate: 'minmax(150px, 1fr)',
},
})
)

return c
})(),
reactTableOptions: { getRowId: (_: any, index: any) => index },
highlightedRowId: 1,
}

export const VirtualizedRows = Template.bind({})
VirtualizedRows.args = {
virtualizeRows: true,
Expand Down
2 changes: 2 additions & 0 deletions src/types/react-table.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ declare module '@tanstack/table-core' {
truncate?: boolean
gridTemplate?: string
center?: boolean
tooltip?: string
highlight?: boolean
}
}

0 comments on commit 33dab53

Please sign in to comment.