:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import type { Meta, StoryObj } from '@storybook/react'
import { useState } from 'react'
import { IconTrash } from '@posthog/icons'
import { useDelayedOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { fn } from 'storybook/test'
import { IconLink } from '../icons'
import { LemonButton } from '../LemonButton'
import { More } from '../LemonButton/More'
import { LemonDivider } from '../LemonDivider'
import { LemonTable, LemonTableProps } from './LemonTable'
import { LemonTableLink } from './LemonTableLink'
import type { LemonTableColumn, LemonTableColumns } from './types'
type Story = StoryObj<LemonTableProps<any>>
const meta: Meta<LemonTableProps<any>> = {
title: 'Lemon UI/Lemon Table',
component: LemonTable as any,
tags: ['autodocs'],
args: { onRow: fn(() => ({})) },
}
export default meta
interface MockPerson {
name: string
occupation: string
}
interface MockFunnelSeries {
name: string
stepResults: [[number, number], [number, number]]
}
const MANY_PEOPLE: MockPerson[] = [
{ name: 'Werner C.', occupation: 'Engineer' },
{ name: 'Ursula Z.', occupation: 'Retired' },
{ name: 'Ludwig A.', occupation: 'Painter' },
{ name: 'Arnold S.', occupation: 'Body-builder' },
{ name: 'Franz B.', occupation: 'Teacher' },
{ name: 'Marie K.', occupation: 'Scientist' },
{ name: 'Hans G.', occupation: 'Architect' },
{ name: 'Greta T.', occupation: 'Activist' },
{ name: 'Otto V.', occupation: 'Musician' },
{ name: 'Helga P.', occupation: 'Doctor' },
{ name: 'Klaus M.', occupation: 'Chef' },
{ name: 'Ingrid S.', occupation: 'Writer' },
]
const WIDE_COLUMNS: LemonTableColumn<MockPerson, keyof MockPerson | undefined>[] = [
{
title: 'Name',
dataIndex: 'name',
width: 150,
sorter: (a, b) => a.name.split(' ')[1].localeCompare(b.name.split(' ')[1]),
},
{
title: 'Occupation',
dataIndex: 'occupation',
width: 150,
tooltip: 'What they are primarily working on.',
sorter: (a, b) => a.occupation.localeCompare(b.occupation),
},
{
title: 'Age',
key: 'age',
width: 120,
render: (_, person) => `${person.name.length * 12} years`,
},
{
title: 'Zodiac sign',
key: 'zodiac',
width: 120,
render: () => 'Gemini',
},
{
title: 'Favorite color',
key: 'color',
width: 120,
render: (_, person) => (person.occupation === 'Engineer' ? 'Blue' : 'Red'),
},
{
title: 'Hometown',
key: 'hometown',
width: 120,
render: (_, person) => (person.occupation === 'Engineer' ? 'Berlin' : 'Munich'),
},
{
title: 'Years of experience',
key: 'experience',
width: 150,
render: (_, person) => `${person.name.length + 5} years`,
},
]
const renderGrouped = (props: LemonTableProps<MockFunnelSeries>): JSX.Element => {
return (
<LemonTable
{...props}
columns={[
{
children: [
{
title: 'Breakdown',
dataIndex: 'name',
},
],
},
{
title: '1. Pageview',
children: [
{
title: 'Completed',
render: (_, record) => record.stepResults[0][0],
},
{
title: 'Dropped off',
render: (_, record) => record.stepResults[0][1],
},
],
},
{
title: '2. Signup',
children: [
{
title: 'Completed',
render: (_, record) => record.stepResults[1][0],
},
{
title: 'Dropped off',
render: (_, record) => record.stepResults[1][1],
},
],
},
]}
dataSource={
[
{
name: 'United States',
stepResults: [
[4325, 0],
[4324, 1],
],
},
{
name: 'France',
stepResults: [
[53, 0],
[12, 41],
],
},
{
name: 'Germany',
stepResults: [
[92, 0],
[1, 91],
],
},
] as MockFunnelSeries[]
}
/>
)
}
const renderBasic = (props: LemonTableProps<MockPerson>): JSX.Element => {
return (
<LemonTable
{...props}
columns={[
{
title: 'Name',
dataIndex: 'name',
sorter: (a, b) => a.name.split(' ')[1].localeCompare(b.name.split(' ')[1]),
},
{
title: 'Occupation',
dataIndex: 'occupation',
tooltip: 'What they are primarily working on.',
sorter: (a, b) => a.occupation.localeCompare(b.occupation),
},
]}
dataSource={
[
{
name: 'Werner C.',
occupation: 'Engineer',
},
{
name: 'Ursula Z.',
occupation: 'Retired',
},
{
name: 'Ludwig A.',
occupation: 'Painter',
},
{
name: 'Arnold S.',
occupation: 'Body-builder',
},
{
name: 'Franz B.',
occupation: 'Teacher',
},
] as MockPerson[]
}
/>
)
}
const renderEmpty = (props: LemonTableProps<Record<string, any>>): JSX.Element => {
return (
<LemonTable
{...props}
columns={[
{ title: 'Name', dataIndex: 'name' },
{ title: 'Occupation', dataIndex: 'occupation' },
]}
dataSource={[]}
/>
)
}
export const Basic: Story = { render: renderBasic as any, args: {} }
export const Grouped: Story = { render: renderGrouped as any, args: {} }
export const Empty: Story = { render: renderEmpty as any, args: {} }
export const PaginatedAutomatically: Story = {
render: renderBasic as any,
args: { nouns: ['person', 'people'], pagination: { pageSize: 3 } },
}
export const WithExpandableRows: Story = {
render: renderBasic as any,
args: {
expandable: {
rowExpandable: (record) => record.occupation !== 'Retired',
expandedRowRender: function RenderCow() {
return <img src="https://c.tenor.com/WAFH6TX2VIYAAAAC/polish-cow.gif" alt="Dancing cow" />
},
},
},
}
export const WithFixedLayoutExpandableRows: Story = {
...WithExpandableRows,
args: {
tableLayout: 'fixed',
expandable: {
expandedRowRender: (record) => <span>{record.occupation}</span>,
},
},
parameters: { testOptions: { viewport: { width: 600, height: 400 } } },
}
export const Small: Story = { render: renderBasic as any, args: { size: 'small' } }
export const Embedded: Story = { render: renderBasic as any, args: { embedded: true } }
export const Stealth: Story = { render: renderBasic as any, args: { stealth: true } }
export const Loading: Story = {
render: renderBasic as any,
args: { loading: true },
parameters: {
testOptions: {
waitForLoadersToDisappear: false,
waitForSelector: '.LemonTableLoader',
},
},
}
export const EmptyLoading: Story = {
render: renderEmpty as any,
args: { loading: true },
parameters: {
testOptions: {
waitForLoadersToDisappear: false,
waitForSelector: '.LemonTableLoader',
},
},
}
export const EmptyLoadingWithManySkeletonRows: Story = {
render: renderEmpty as any,
args: { loading: true, loadingSkeletonRows: 10 },
parameters: {
testOptions: {
waitForLoadersToDisappear: false,
waitForSelector: '.LemonTableLoader',
},
},
}
export const WithoutHeader: Story = { render: renderBasic as any, args: { showHeader: false } }
export const WithoutUppercasingInHeader: Story = { render: renderBasic as any, args: { uppercaseHeader: false } }
export const WithFooter: Story = {
render: renderBasic as any,
args: {
footer: (
<>
<div className="flex items-center m-2">
<LemonButton center fullWidth>
Load more rows
</LemonButton>
</div>
</>
),
},
}
export const WithColorCodedRows: Story = {
render: renderBasic as any,
args: {
rowRibbonColor: ({ occupation }) =>
occupation === 'Engineer'
? 'var(--success)'
: occupation === 'Retired'
? 'var(--warning)'
: occupation === 'Body-builder'
? 'var(--danger)'
: null,
},
}
export const WithHighlightedRows: Story = {
render: renderBasic as any,
args: {
rowStatus: ({ occupation }) => (['Retired', 'Body-builder'].includes(occupation) ? 'highlighted' : null),
},
}
export const WithMandatorySorting: Story = {
render: renderBasic as any,
args: { defaultSorting: { columnKey: 'name', order: 1 }, noSortingCancellation: true },
}
export const WithStickyFirstColumn: Story = {
render: () => {
useDelayedOnMountEffect(() => {
const scrollableInner = document.querySelector(
'#story--lemon-ui-lemon-table--with-sticky-first-column .scrollable__inner'
)
if (scrollableInner) {
scrollableInner.scrollLeft = 20
}
})
return (
<LemonTable
className="max-w-100"
firstColumnSticky
columns={WIDE_COLUMNS.slice(0, 5)}
dataSource={MANY_PEOPLE.slice(0, 5)}
/>
)
},
}
export const WithLink: Story = {
render: () => {
return (
<LemonTable
columns={[
{
title: 'Name',
dataIndex: 'name',
sorter: (a, b) => a.name.split(' ')[1].localeCompare(b.name.split(' ')[1]),
render: (_, item) => (
<LemonTableLink
title={item.name}
to="/test"
description={`${item.name} is a ${item.occupation.toLowerCase()} who is ${
item.name.length * 12
} years old.`}
/>
),
},
...WIDE_COLUMNS.slice(1, 5),
]}
dataSource={MANY_PEOPLE.slice(0, 5)}
/>
)
},
}
export const WithCellActions: Story = {
render: () => {
return (
<LemonTable
columns={[
{
title: 'Name',
dataIndex: 'name',
cellActions: (value) => (
<>
<LemonButton
fullWidth
size="small"
icon={<IconLink />}
onClick={() => alert(`Viewing profile for ${value}`)}
>
View profile
</LemonButton>
<LemonButton fullWidth size="small" onClick={() => alert(`Copying ${value}`)}>
Copy name
</LemonButton>
</>
),
},
{
title: 'Occupation',
dataIndex: 'occupation',
cellActions: (value, record) => (
<>
<LemonButton fullWidth size="small" onClick={() => alert(`Filtering to ${value}`)}>
Filter to {value}
</LemonButton>
<LemonDivider />
<LemonButton
fullWidth
size="small"
status="danger"
icon={<IconTrash />}
onClick={() => alert(`Removing ${record.name}`)}
>
Remove person
</LemonButton>
</>
),
},
{
title: 'Age',
key: 'age',
render: (_, person) => `${person.name.length * 12} years`,
},
]}
dataSource={MANY_PEOPLE.slice(0, 5)}
/>
)
},
}
export const WithRowActions: Story = {
render: () => {
return (
<LemonTable
columns={[
{
title: 'Name',
dataIndex: 'name',
},
{
title: 'Occupation',
dataIndex: 'occupation',
},
{
title: 'Age',
key: 'age',
render: (_, person) => `${person.name.length * 12} years`,
},
]}
rowActions={(record) => (
<More
overlay={
<>
<LemonButton
fullWidth
size="small"
icon={<IconLink />}
onClick={() => alert(`Viewing ${record.name}'s profile`)}
>
View profile
</LemonButton>
<LemonButton fullWidth size="small" onClick={() => alert(`Editing ${record.name}`)}>
Edit
</LemonButton>
<LemonDivider />
<LemonButton
fullWidth
size="small"
status="danger"
icon={<IconTrash />}
onClick={() => alert(`Deleting ${record.name}`)}
>
Delete
</LemonButton>
</>
}
/>
)}
dataSource={MANY_PEOPLE.slice(0, 5)}
/>
)
},
}
const RESIZABLE_COLUMNS: LemonTableColumn<MockPerson, keyof MockPerson | undefined>[] = [
{
title: 'Identifier',
key: 'identifier',
width: 300,
render: (_, person) => (
<span className="whitespace-nowrap">{`0192b2c8-4f4c-7c3a-9e6d-${person.name.length}b2c3d4e5f6`}</span>
),
},
...WIDE_COLUMNS.slice(0, 3),
]
function ResizableColumnsTable(): JSX.Element {
const [widths, setWidths] = useState<Record<number, number>>({})
return (
<LemonTable
columns={RESIZABLE_COLUMNS.map((column, index) => ({
...column,
width: widths[index] ?? column.width,
resizable: true,
onResize: (width: number) => setWidths((currentWidths) => ({ ...currentWidths, [index]: width })),
}))}
dataSource={MANY_PEOPLE.slice(0, 5)}
// Size the table to its content once a column is resized, the same as tables that persist
// column widths. Intrinsic sizing is the mode a cropped column shows in.
tableStyle={Object.keys(widths).length > 0 ? { width: 'max-content' } : undefined}
/>
)
}
export const WithResizableColumns: Story = {
render: () => <ResizableColumnsTable />,
}
export const WithHorizontalOverflow: Story = {
render: () => {
return (
<div className="max-w-120">
<LemonTable columns={WIDE_COLUMNS} dataSource={MANY_PEOPLE.slice(0, 5)} />
</div>
)
},
}
export const WithVerticalOverflow: Story = {
render: () => {
return (
<div className="w-fit max-w-full flex flex-col" style={{ height: 240 }}>
<LemonTable columns={WIDE_COLUMNS.slice(0, 2)} dataSource={MANY_PEOPLE} allowContentScroll />
</div>
)
},
}
export const WithHorizontalAndVerticalOverflow: Story = {
render: () => {
return (
<div className="flex flex-col" style={{ maxWidth: 480, height: 320 }}>
<LemonTable columns={WIDE_COLUMNS} dataSource={MANY_PEOPLE} allowContentScroll />
</div>
)
},
}
const BULK_SELECTION_COLUMNS: LemonTableColumns<MockPerson> = [
{ title: 'Name', dataIndex: 'name' },
{ title: 'Occupation', dataIndex: 'occupation' },
]
const BULK_SELECTION_PEOPLE = MANY_PEOPLE.slice(0, 5)
function BulkSelectionTable({ initialSelected }: { initialSelected: string[] }): JSX.Element {
return (
<LemonTable
columns={BULK_SELECTION_COLUMNS}
dataSource={BULK_SELECTION_PEOPLE}
rowKey="name"
bulkSelection={{
getKey: (person: MockPerson): string => person.name,
initialSelectedKeys: initialSelected,
noun: ['person', 'people'],
rowAriaLabel: (person: MockPerson) => `Select ${person.name}`,
headerAriaLabel: 'Select all people on this page',
renderActions: (ctx) => (
<>
<LemonButton
type="secondary"
size="small"
onClick={() => alert(`Editing ${ctx.selectedCount} person(s)`)}
>
Edit selected
</LemonButton>
<LemonButton
type="primary"
status="danger"
size="small"
icon={<IconTrash />}
onClick={() => alert(`Deleting ${ctx.selectedKeys.join(', ')}`)}
>
Delete selected
</LemonButton>
</>
),
}}
/>
)
}
export const WithBulkSelectionNothingSelected: Story = {
render: () => <BulkSelectionTable initialSelected={[]} />,
}
export const WithBulkSelectionTwoSelected: Story = {
render: () => <BulkSelectionTable initialSelected={['Werner C.', 'Ursula Z.']} />,
}