Table

A component for displaying structured data in rows and columns.


A list of your recent invoices.
NameSales ($)RegionStatusWorking Hours (h)
Peter Doe1.000.000Region Aoverperforming100
Jon Doe2.202.000Region Boverperforming110
Peter Doe1.505.000Region Cunderperforming90
Employee 4500000Region Doverperforming92
Employee 5600000Region Eunderperforming95
Employee 6700000Region Foverperforming98
Employee 7800000Region Gunderperforming101
Employee 8900000Region Hoverperforming104
464215h

Automatic Installation

This method is still working on progress, please use manual installation for now.

Manual Installation

Install dependencies

npm install radix-ui

Copy the code

components/table/table.tsx
import * as React from 'react'
import { tableStyles } from './table.css'
 
const TableRoot = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
  ({ className, children, ...props }, forwardedRef) => {
    const styles = tableStyles()
    return (
      <div ref={forwardedRef}>
        {/* make table scrollable on mobile */}
        <div className={styles.root({ className })} {...props}>
          {children}
        </div>
      </div>
    )
  }
)
 
const Table = React.forwardRef<HTMLTableElement, React.TableHTMLAttributes<HTMLTableElement>>(
  ({ className, ...props }, forwardedRef) => {
    const styles = tableStyles()
    return <table ref={forwardedRef} className={styles.table({ className })} {...props} />
  }
)
 
const TableHead = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, forwardedRef) => {
  const styles = tableStyles()
  return <thead ref={forwardedRef} className={styles.head({ className })} {...props} />
})
 
const TableHeaderCell = React.forwardRef<
  HTMLTableCellElement,
  React.ThHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, forwardedRef) => {
  const styles = tableStyles()
  return <th ref={forwardedRef} className={styles.headerCell({ className })} {...props} />
})
 
const TableBody = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, forwardedRef) => {
  const styles = tableStyles()
  return <tbody ref={forwardedRef} className={styles.body({ className })} {...props} />
})
 
const TableRow = React.forwardRef<HTMLTableRowElement, React.HTMLAttributes<HTMLTableRowElement>>(
  ({ className, ...props }, forwardedRef) => {
    const styles = tableStyles()
    return <tr ref={forwardedRef} className={styles.row({ className })} {...props} />
  }
)
 
const TableCell = React.forwardRef<
  HTMLTableCellElement,
  React.TdHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, forwardedRef) => {
  const styles = tableStyles()
  return <td ref={forwardedRef} className={styles.cell({ className })} {...props} />
})
 
const TableFoot = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, forwardedRef) => {
  const styles = tableStyles()
  return <tfoot ref={forwardedRef} className={styles.foot({ className })} {...props} />
})
 
const TableCaption = React.forwardRef<
  HTMLTableCaptionElement,
  React.HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...props }, forwardedRef) => {
  const styles = tableStyles()
  return <caption ref={forwardedRef} className={styles.caption({ className })} {...props} />
})
 
TableRoot.displayName = 'TableRoot'
Table.displayName = 'Table'
TableHead.displayName = 'TableHead'
TableHeaderCell.displayName = 'TableHeaderCell'
TableBody.displayName = 'TableBody'
TableRow.displayName = 'TableRow'
TableCell.displayName = 'TableCell'
TableFoot.displayName = 'TableFoot'
TableCaption.displayName = 'TableCaption'
 
export {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFoot,
  TableHead,
  TableHeaderCell,
  TableRoot,
  TableRow,
}

Usage

Imports

import {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFoot,
  TableHead,
  TableHeaderCell,
  TableRoot,
  TableRow,
} from '#/components/table'

Example

Browse the Storybook for more examples.

Edit on GitHub

Last updated on

On this page