Skip to content

Commit

Permalink
fix: improve action column
Browse files Browse the repository at this point in the history
  • Loading branch information
keellyp committed Jul 26, 2024
1 parent a520a8d commit 2613da3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
69 changes: 48 additions & 21 deletions src/components/designSystem/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { MouseEvent, ReactNode, useRef } from 'react'
import styled from 'styled-components'

import { Button, ButtonProps, Popper, Skeleton, Typography } from '~/components/designSystem'
import { Button, IconName, Popper, Skeleton, Tooltip, Typography } from '~/components/designSystem'
import { GenericPlaceholder, GenericPlaceholderProps } from '~/components/GenericPlaceholder'
import { useInternationalization } from '~/hooks/core/useInternationalization'
import { useListKeysNavigation } from '~/hooks/ui/useListKeyNavigation'
Expand Down Expand Up @@ -45,6 +45,15 @@ type DataItem = {
id: string
}

export type ActionItem<T> = {
title: string | ReactNode
onAction: (item: T) => void
startIcon?: IconName
disabled?: boolean
tooltip?: string
tooltipListener?: boolean
}

interface TableProps<T> {
name: string
data: T[]
Expand All @@ -57,11 +66,7 @@ interface TableProps<T> {
errorState?: Partial<GenericPlaceholderProps>
}
onRowAction?: (item: T) => void
actionColumn?: Array<{
title: string | ReactNode
startIcon?: ButtonProps['startIcon']
onAction: (item: T) => void
}>
actionColumn?: (item: T) => Array<ActionItem<T> | null>
/**
* 'sm' = 4px ; 'md' = 16px ; 'lg' = 48px
* @default 'lg'
Expand Down Expand Up @@ -254,21 +259,43 @@ export const Table = <T extends DataItem>({
>
{({ closePopper }) => (
<MenuPopper data-id={`${TABLE_ID}-popper`}>
{actionColumn.map((action, j) => (
<Button
fullWidth
key={`${TABLE_ID}-action-${i}-${j}`}
startIcon={action.startIcon}
variant="quaternary"
align="left"
onClick={async () => {
await action.onAction(item)
closePopper()
}}
>
{action.title}
</Button>
))}
{actionColumn(item)
.filter((action) => !!action)
.map((action, j) => {
if (!action) {
return
}

const button = (
<Button
fullWidth
startIcon={action.startIcon}
variant="quaternary"
align="left"
disabled={action.disabled}
onClick={async () => {
await action.onAction(item)
closePopper()
}}
>
{action.title}
</Button>
)

if (action.tooltip) {
return (
<Tooltip
key={`${TABLE_ID}-action-${i}-${j}`}
title={action.tooltip}
disableHoverListener={action.tooltipListener}
>
{button}
</Tooltip>
)
}

return button
})}
</MenuPopper>
)}
</Popper>
Expand Down
18 changes: 10 additions & 8 deletions src/pages/__devOnly/DesignSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,16 @@ const DesignSystem = () => {
},
]}
onRowAction={(item) => alert(`You clicked on ${item.id}`)}
actionColumn={[
{
title: 'Edit',
startIcon: 'pen',
onAction: (item) => {
alert(`You edited ${item.id}`)
},
},
actionColumn={(currentItem) => [
currentItem.amount > 1000
? {
title: 'Edit',
startIcon: 'pen',
onAction: (item) => {
alert(`You edited ${item.id}`)
},
}
: null,
{
title: 'Delete',
startIcon: 'trash',
Expand Down

0 comments on commit 2613da3

Please sign in to comment.