V1.0

Base Components

Data Table

Data table is a 2-dimensional data structure where each row is an item, and each column is a data point about the item.

Installation

Create a table.tsx file and paste the following code into it.

/components/ui/table.tsx
// AlignUI Table v0.0.0
 
import * as React from 'react';
 
import * as Divider from '@/components/ui/divider';
import { cn, cnExt } from '@/utils/cn';
 
const Table = React.forwardRef<
  HTMLTableElement,
  React.TableHTMLAttributes<HTMLTableElement>
>(({ className, ...rest }, forwardedRef) => {
  return (
    <div className={cnExt('w-full overflow-x-auto', className)}>
      <table ref={forwardedRef} className='w-full' {...rest} />
    </div>
  );
});
Table.displayName = 'Table';
 
const TableHeader = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ ...rest }, forwardedRef) => {
  return <thead ref={forwardedRef} {...rest} />;
});
TableHeader.displayName = 'TableHeader';
 
const TableHead = React.forwardRef<
  HTMLTableCellElement,
  React.ThHTMLAttributes<HTMLTableCellElement>
>(({ className, ...rest }, forwardedRef) => {
  return (
    <th
      ref={forwardedRef}
      className={cnExt(
        'bg-bg-weak-50 px-3 py-2 text-left text-paragraph-sm text-text-sub-600 first:rounded-l-lg last:rounded-r-lg',
        className,
      )}
      {...rest}
    />
  );
});
TableHead.displayName = 'TableHead';
 
const TableBody = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement> & {
    spacing?: number;
  }
>(({ spacing = 8, ...rest }, forwardedRef) => {
  return (
    <>
      {/* to have space between thead and tbody */}
      <tbody
        aria-hidden='true'
        className='table-row'
        style={{
          height: spacing,
        }}
      />
 
      <tbody ref={forwardedRef} {...rest} />
    </>
  );
});
TableBody.displayName = 'TableBody';
 
const TableRow = React.forwardRef<
  HTMLTableRowElement,
  React.HTMLAttributes<HTMLTableRowElement>
>(({ className, ...rest }, forwardedRef) => {
  return (
    <tr ref={forwardedRef} className={cn('group/row', className)} {...rest} />
  );
});
TableRow.displayName = 'TableRow';
 
function TableRowDivider({
  className,
  dividerClassName,
  ...rest
}: React.ComponentPropsWithoutRef<typeof Divider.Root> & {
  dividerClassName?: string;
}) {
  return (
    <tr aria-hidden='true' className={className}>
      <td colSpan={999} className='py-1'>
        <Divider.Root
          variant='line-spacing'
          className={dividerClassName}
          {...rest}
        />
      </td>
    </tr>
  );
}
TableRowDivider.displayName = 'TableRowDivider';
 
const TableCell = React.forwardRef<
  HTMLTableCellElement,
  React.TdHTMLAttributes<HTMLTableCellElement>
>(({ className, ...rest }, forwardedRef) => {
  return (
    <td
      ref={forwardedRef}
      className={cnExt(
        'h-16 px-3 transition duration-200 ease-out first:rounded-l-xl last:rounded-r-xl group-hover/row:bg-bg-weak-50',
        className,
      )}
      {...rest}
    />
  );
});
TableCell.displayName = 'TableCell';
 
const TableCaption = React.forwardRef<
  HTMLTableCaptionElement,
  React.HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...rest }, forwardedRef) => (
  <caption
    ref={forwardedRef}
    className={cnExt('mt-4 text-paragraph-sm text-text-sub-600', className)}
    {...rest}
  />
));
TableCaption.displayName = 'TableCaption';
 
export {
  Table as Root,
  TableHeader as Header,
  TableBody as Body,
  TableHead as Head,
  TableRow as Row,
  TableRowDivider as RowDivider,
  TableCell as Cell,
  TableCaption as Caption,
};

Update the import paths to match your project setup.

Examples

We will give you examples on how to create complex Data Tables with @tanstack/react-table using our Table primitives. Let's start by installing the package:

terminal
npm install @tanstack/react-table

Row Selection

Member Name
Title
Projects
Member Documents
Status
Marketing ManagerSince Aug, 2021
Monday.comCampaign Strategy Brainstorming
PDF
brown-james.pdf2.4 MB
Active
Sophia Williams[email protected]
HR AssistantSince Aug, 2021
NotionEmployee Engagement Survey
PDF
williams-sophia.pdf2.4 MB
Active
Arthur Taylor[email protected]
Entrepreneur / CEOSince May, 2022
SpotifyVision and Goal Setting Session
PDF
taylor-arthur.pdf2.4 MB
Absent
Front-end DeveloperSince Sep, 2022
FormcarryUser Feedback Analysis
PDF
wright-emma.pdf1.9 MB
Active
Matthew Johnson[email protected]
Data Software EngineerSince Feb, 2022
LoomData Analysis Methodology
PDF
johnson-matthew.pdf2.9 MB
Active
Fashion DesignerSince Mar, 2022
TidalDesign Trends and Inspirations
PDF
perez-laura.pdf2.5 MB
Absent
Operations ManagerSince July, 2021
DropboxProcess Optimization Brainstorming
PDF
chen-wei.pdf2.6 MB
Active

API Reference

These examples are based on the Tanstack Table package. Refer to their documentation for the API reference.

© 2024 AlignUI Design System. All rights reserved.