← Back to UI Prototype

UI Prototype · Data Table

Data Table

A simple data table component.

Users

1Emily Chen28Software Engineer
2Ryan Thompson32Marketing Manager
3Sophia Patel25Data Analyst
4Michael Lee41CEO
5Olivia Brown29Graphic Designer

Page 1 of 8

Houses

1123 Maple StSpringfieldIL1990
2456 Oak StLincolnNE1985
3789 Pine StMadisonWI2000
4101 Cedar StColumbusOH1978
5102 Elm StDoverDE1995

Page 1 of 10

data-table

Read-only
"use client";

import DataTable, { Columns, SortDirection } from './DataTable';
import users from './data/users';
import houses from './data/houses';

type User = (typeof users)[number];

const userColumns: Columns<User> = [
  {
    label: 'ID',
    key: 'id',
    renderCell: (user) => user.id,
    comparator: (a, b, direction) =>
      direction === 'asc' ? a.id - b.id : b.id - a.id,
    filter: {
      type: 'range',
      getValue: (user) => user.id,
    },
  },
  {
    label: 'Name',
    key: 'name',
    renderCell: (user) => user.name,
    comparator: (a, b, direction) =>
      direction === 'asc'
        ? a.name.localeCompare(b.name)
        : b.name.localeCompare(a.name),
    filter: {
      type: 'text',
      getValue: (user) => user.name,
    },
  },
  {
    label: 'Age',
    key: 'age',
    renderCell: (user) => user.age,
    comparator: (a, b, direction: SortDirection) =>
      direction === 'asc' ? a.age - b.age : b.age - a.age,
    filter: {
      type: 'range',
      getValue: (user) => user.age,
    },
  },
  {
    label: 'Occupation',
    key: 'occupation',
    renderCell: (user) => user.occupation,
    comparator: (a, b, direction) =>
      direction === 'asc'
        ? a.occupation.localeCompare(b.occupation)
        : b.occupation.localeCompare(a.occupation),
    filter: {
      type: 'text',
      getValue: (user) => user.occupation,
    },
  },
];

type House = (typeof houses)[number];

const houseColumns: Columns<House> = [
  {
    label: 'ID',
    key: 'id',
    renderCell: (house) => house.id,
    comparator: (a, b, direction) =>
      direction === 'asc' ? a.id - b.id : b.id - a.id,
    filter: {
      type: 'range',
      getValue: (house) => house.id,
    },
  },
  {
    label: 'Street',
    key: 'street',
    renderCell: (house) => house.street,
    comparator: (a, b, direction) =>
      direction === 'asc'
        ? a.street.localeCompare(b.street)
        : b.street.localeCompare(a.street),
    filter: {
      type: 'text',
      getValue: (house) => house.street,
    },
  },
  {
    label: 'City',
    key: 'city',
    renderCell: (house) => house.city,
    comparator: (a, b, direction) =>
      direction === 'asc'
        ? a.city.localeCompare(b.city)
        : b.city.localeCompare(a.city),
    filter: {
      type: 'text',
      getValue: (house) => house.city,
    },
  },
  {
    label: 'State',
    key: 'state',
    renderCell: (house) => house.state,
    comparator: (a, b, direction) =>
      direction === 'asc'
        ? a.state.localeCompare(b.state)
        : b.state.localeCompare(a.state),
    filter: {
      type: 'text',
      getValue: (house) => house.state,
    },
  },
  {
    label: 'Built Year',
    key: 'built_year',
    renderCell: (house) => house.built_year,
    comparator: (a, b, direction) =>
      direction === 'asc'
        ? a.built_year - b.built_year
        : b.built_year - a.built_year,
    filter: {
      type: 'range',
      getValue: (house) => house.built_year,
    },
  },
];

export default function App() {
  return (
    <main>
      <h2>Users</h2>
      <DataTable data={users} columns={userColumns} />

      <br />

      <h2>Houses</h2>
      <DataTable data={houses} columns={houseColumns} />
    </main>
  );
}