@pikas-ui/table

This package contains a table component that can be used to display data in a tabular format.

This library is based on TanStack Table.

Change Log

Requirements

To use this package, you must install the @pikas-ui/styles package.

Installation

You can install this package using npm, yarn, or pnpm.

npm install @pikas-ui/table
yarn add @pikas-ui/table
pnpm add @pikas-ui/table

Usage

Table

The table component is used to display data in a tabular format.

Default

Name
First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows

Light

First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="default"
      emptyMessage="No data found"
      columns={[
        {
          header: 'First Name',
          accessorKey: 'firstName',
          id: 'firstName',
          cell: info => info.getValue(),
        },
        {
          header: 'Last Name',
          accessorKey: 'lastName',
          id: 'lastName',
          cell: info => info.getValue(),
        },
        {
          header: 'Age',
          accessorKey: 'age',
          id: 'age',
        },
        {
          header: 'Visits',
          accessorKey: 'visits',
          id: 'age',
        },
        {
          header: 'Status',
          accessorKey: 'status',
          id: 'status',
        },
        {
          header: 'Progress',
          accessorKey: 'progress',
          id: 'progress',
          cell: ({ getValue }) =>
            `${Math.round(getValue<number>() * 100) / 100}%`,
        },
      ]}
    />
  );
};

Props

PropDescriptionTypeDefault
dataThe data of the tableT[]-
columnsThe columns of the tableColumnDef<T>[]-
variantThe variant of the tableTableVariant"default"
emptyMessageThe message to show when the table is emptyReactNode-
hasTfootIf the table has a tfootbooleanfalse
columnOrderThe column order of the tableTableColumnOrder-
columnPinningThe column pinning of the tableTableColumnPinning-
columnSizingThe column sizing of the tableTableColumnSizing-
columnVisibilityThe column visibility of the tableTableVisibility-
expandingThe expanding of the tableTableExpanding-
filtersThe filters of the tableTableFilters-
groupingThe grouping of the tableTableGrouping-
paginationThe pagination of the tableTablePaginationProps-
rowSelectionThe row selection of the tableTableRowSelection-
sortingThe sorting of the tableTableSorting-
cssThe css of the tableTableCSS<T>-
paddingThe padding of the tableTablePadding-
hoverEffectIf the table has a hover effectbooleanfalse
debugIf the table is in debug modebooleanfalse
fullWidthIf the table is full widthbooleanfalse

Features


Column Order

Allows to activate the moving of the columns.

More information about the column order can be found in the TanStack Table documentation.

Column order is not enabled if you have a nested column.

First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows
[
  "firstName",
  "lastName",
  "age",
  "visits",
  "status",
  "progress"
]
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      columnOrder={{
        enabled: true,
        defaultState: ['firstName', 'lastName', 'age', 'visits', 'status', 'progress']
        onColumnOrderChange: (state) => {
          console.log(state);
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the column order is enabledbooleanfalse
stateThe state of the column orderTableColumnOrderState-
defaultStateThe default state of the column orderTableColumnOrderState-
onColumnOrderChangeThe function to call when the column order changesOnChangeFn<TableColumnOrderState>-

Column Sizing

Enables changing the width of the columns.

More information about the column sizing can be found in the TanStack Table documentation.

First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows
{
  "firstName": 200,
  "lastName": 200,
  "age": 100,
  "visits": 100,
  "status": 100,
  "progress": 100
}
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      columnSizing={{
        enabled: true,
        resizeMode: 'onChange',
        defaultState: {
          firstName: 200,
          lastName: 200,
          age: 100,
          visits: 100,
          status: 100,
          progress: 100,
        }
        onColumnSizeChange: (state) => {
          console.log(state);
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the resizing is enabledbooleanfalse
modeThe mode of the resizingTableResizeMode-
stateThe state of the resizingTableColumnSizingState-
defaultStateThe default state of the resizingTableColumnSizingState-
onResizeThe function to call when the resizing changesOnChangeFn<TableColumnSizingState>-

Column Pinning

Enables changing the width of the columns.

More information about the column pinning can be found in the TanStack Table documentation.

First Name
Last Name
No data found
Age
Visits
Status
No data found
Progress
No data found
0 rows
{
  "left": [
    "firstName",
    "lastName"
  ],
  "right": [
    "progress"
  ]
}
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      columnPinning={{
        enabled: true,
        defaultState: {
          left: ['firstName', 'lastName'],
          right: ['progress'],
        }
        onColumnPinningChange: (state) => {
          console.log(state);
        },
        isSplit: true,
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the column pinning is enabledbooleanfalse
stateThe state of the column pinningTableColumnPinningState-
defaultStateThe default state of the column pinningTableColumnPinningState-
onColumnPinningChangeThe function to call when the column pinning changesOnChangeFn<TableColumnPinningState>-
isSplitIf the column pinning is splitbooleanfalse

Column Visibility

Allows you to activate the display or not of columns.

More information about the column visibility can be found in the TanStack Table documentation.

First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows
{
  "firstName": true,
  "lastName": true,
  "age": true,
  "visits": true,
  "status": true,
  "progress": true
}
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      columnVisibility={{
        enabled: true,
        state: {
          firstName: true,
          lastName: true,
          age: true,
          visits: true,
          status: true,
          progress: true,
        },
        onVisibilityChange: (state) => {
          console.log(state);
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the column visibility is enabledbooleanfalse
stateThe state of the column visibilityTableVisibilityState-
onVisibilityChangeThe function to call when the column visibility changesOnChangeFn<TableVisibilityState>-

Expanding

Allows you to activate the data grouping.

More information about the expanding can be found in the TanStack Table documentation.

First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows
{}
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={[
        {
          id: 1,
          firstName: 'John',
          lastName: 'Doe',
          age: 32,
          visits: 320,
          status: 'complicated',
          progress: 66,
          subRows: [
            {
              id: 11,
              firstName: 'John',
              lastName: 'Doe',
              age: 32,
              visits: 320,
              status: 'complicated',
              progress: 66,
            },
            ...
          ],
        },
        ...
      ]}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      expanding={{
        enabled: true,
        defaultState: {
          1: true,
          2: true,
        }
        onExpandedChange: (state) => {
          console.log(state);
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the expanding is enabledbooleanfalse
stateThe state of the expandingTableExpandedState-
defaultStateThe default state of the expandingTableExpandedState-
onExpandedChangeThe function to call when the expanding changesOnChangeFn<TableExpandedState>-

Filters

Allows you to activate the data filtering.

More information about the expanding can be found in the TanStack Table documentation.

import { Table, rankItem, addMeta } from '@pikas-ui/table';

const fuzzyFilter: FilterFn<Person> = (row, columnId, value, addMeta) => {
  // Rank the item
  const itemRank = rankItem(row.getValue(columnId), value);

  // Store the itemRank info
  addMeta({
    itemRank,
  });

  // Return if the item should be filtered in/out
  return itemRank.passed;
};

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[
        {
          header: 'First Name',
          accessorKey: 'firstName',
          id: 'firstName',
          filterFn: fuzzyFilter,
        },
        {
          header: 'Last Name',
          accessorKey: 'lastName',
          id: 'lastName',
        },
        {
          header: 'Age',
          accessorKey: 'age',
          id: 'age',
        },
        {
          header: 'Visits',
          accessorKey: 'visits',
          id: 'visits',
        },
        {
          header: 'Status',
          accessorKey: 'status',
          id: 'status',
        },
        {
          header: 'Progress',
          accessorKey: 'progress',
          id: 'progress',
          cell: ({ getValue }) =>
            `${Math.round(getValue<number>() * 100) / 100}%`,
        },
      ]}
      filters={{
        enabled: true,
        globalFilter: 'John'',
        globalFilterFn: fuzzyFilter,
        columnFilters: [
          {
            id: 'lastName',
            value: 'Doe',
          }
        ],
        filterFns: {
          fuzzy: fuzzyFilter,
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the filters are enabledbooleanfalse
columnFiltersThe state of the column filtersTableColumnFiltersState-
onColumnFiltersChangeThe function to call when the column filters changeOnChangeFn<TableColumnFiltersState>-
globalFilterThe global filterstring-
onGlobalFilterChangeThe function to call when the global filter changeOnChangeFn<string>-
globalFilterFnThe global filter functionFilterFn<T>-
filterFnsThe filter functionsRecord<string, FilterFn<T>>-

Grouping

Allows you to activate the data grouping.

More information about the grouping can be found in the TanStack Table documentation.

First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows
[]
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[
        {
          header: 'First Name',
          accessorKey: 'firstName',
          id: 'firstName',
        },
        {
          header: 'Last Name',
          accessorKey: 'lastName',
          id: 'lastName',
        },
        {
          header: 'Age',
          accessorKey: 'age',
          id: 'age',
          aggregatedCell: ({ getValue }) =>
            Math.round(getValue<number>() * 100) / 100,
          aggregationFn: 'median',
        },
        {
          header: 'Visits',
          accessorKey: 'visits',
          id: 'visits',
          aggregationFn: 'sum',
        },
        {
          header: 'Status',
          accessorKey: 'status',
          id: 'status',
        },
        {
          header: 'Progress',
          accessorKey: 'progress',
          id: 'progress',
          cell: ({ getValue }) =>
            `${Math.round(getValue<number>() * 100) / 100}%`,
          aggregationFn: 'mean',
          aggregatedCell: ({ getValue }) =>
            `${Math.round(getValue<number>() * 100) / 100}%`,
        },
      ]}
      grouping={{
        enabled: true,
        defaultState: ['firstName', 'lastName']
        onGroupingChange: (state) => {
          console.log(state);
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the grouping is enabledbooleanfalse
stateThe state of the groupingTableGroupingState-
defaultStateThe default state of the groupingTableGroupingState-
onGroupingChangeThe function to call when the grouping changesOnChangeFn<TableGroupingState>-

Pagination

Allows you to activate the data pagination.

More information about the pagination can be found in the TanStack Table documentation.

import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      pagination={{
        enabled: true,
        pageSizes: [10, 20, 30, 40, 50],
        defaultPageIndex: 2,
        defaultPageSize: 0,
        onPaginationChange: ({ pageSize, pageIndex }) => {
          console.log({ pageSize, pageIndex });
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the pagination is enabledbooleanfalse
pageSizeThe size of the pagenumber-
pageSizesThe sizes of the pagenumber[]-
pageIndexThe index of the pagenumber-
defaultPageSizeThe default size of the pagenumber-
defaultPageIndexThe default index of the pagenumber-
onPaginationChangeThe function to call when the pagination changesOnChangeFn<TablePaginationState>-

Row Selection

Enable data selection by adding checkboxes.

More information about the row selection can be found in the TanStack Table documentation.

import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      rowSelection={{
        enabled: true,
        defaultState: {
          '1': true,
          '2': true,
        },
        onRowSelectionChange: (state) => {
          console.log(state);
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the row selection is enabledbooleanfalse
enableSubRowSelectionIf the sub row selection is enabledbooleanfalse
stateThe state of the row selectionTableRowSelectionState-
defaultStateThe default state of the row selectionTableRowSelectionState-
onRowSelectionChangeThe function to call when the row selection changesOnChangeFn<TableRowSelectionState>-

Sorting

Allows you to activate the data sorting.

More information about the sorting can be found in the TanStack Table documentation.

First Name
Last Name
Age
Visits
Status
Progress
No data found
0 rows
[]
import { Table } from '@pikas-ui/table';

const Example: React.FC = () => {
  return (
    <Table
      data={data}
      variant="light"
      emptyMessage="No data found"
      columns={[ ... ]}
      sorting={{
        enabled: true,
        defaultState: [
          {
            id: 'firstName',
            desc: false,
          },
        ],
        onSortingChange: (state) => {
          console.log(state);
        },
      }}
    />
  );
};

Parameters

PropDescriptionTypeDefault
enabledIf the sorting is enabledbooleanfalse
stateThe state of the sortingTableSortingState-
defaultStateThe default state of the sortingTableSortingState-
onSortingChangeThe function to call when the sorting changesOnChangeFn<TableSortingState>-

Contributing

This documentation can be edited on GitHub here