1.0.3 • Published 9 months ago

@coligo/react-native-table v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@coligo/react-native-table

A lightweight and customizable React Native package for creating flexible, sortable tables. Perfect for displaying structured data with support for customizable columns, headers, and cell rendering.

Features

  • Sortable Columns: Easily add sorting functionality to any column.
  • Customizable Headers and Cells: Render custom content for headers and cells.
  • Flexible Layout: Control padding, border styles, and column widths.
  • Sticky Headers: Keep headers visible during scrolling.
  • Separator Customization: Customize row separators for enhanced visual structure.

Installation

npm install @coligo/react-native-table

Usage

import React from 'react';
import { Table } from '@coligo/react-native-table';

const MyTable = () => {
  const data = [
    { id: 1, name: 'John Doe', age: 28 },
    { id: 2, name: 'Jane Smith', age: 34 },
    { id: 3, name: 'Sam Wilson', age: 45 },
  ];

  const columns = [
    { label: 'ID', key: 'id', sortable: true },
    { label: 'Name', key: 'name', sortable: true },
    { label: 'Age', key: 'age', sortable: true },
  ];

  return <Table data={data} columns={columns} keyExtractor="id" stickyHeader />;
};

export default MyTable;

API Reference

Table Component

The Table component accepts the following props:

PropTypeDescription
dataT[]Array of data objects to display in the table.
columnsColumn<T>[]Defines the columns of the table, including keys and render functions for custom content.
keyExtractorkeyof TKey used to uniquely identify each row.
cellPaddingCellPaddingOptional padding for cells. See CellPadding below.
sortingIconsSortingIconsOptional icons for sorting states. See SortingIcons below.
borderStyleBorderStyleOptional border styles for the table. See BorderStyle below.
styleStyleProp<ViewStyle>Optional styling for the container view.
stickyHeaderbooleanEnables sticky headers when set to true.
headerBackgroundColorValueOptional background color for the header.
rowHeader(rowData: T) => ReactNodeOptional render function for custom row header content.
rowFooter(rowData: T) => ReactNodeOptional render function for custom row footer content.

Column Type

Each column is defined by the following properties:

PropertyTypeDescription
labelstringColumn header label.
keykeyof TKey in the data object to display.
idstringOptional id that is used as key for duplicate key use.
widthnumberOptional fixed width for the column.
flexnumberOptional flex value to proportionally size the column.
sortablebooleanWhether the column is sortable.
header(label: string) => ReactNodeOptional custom render function for the column header.
render(item: T[keyof T], rowData: T) => ReactNodeOptional custom render function for the cell content.

CellPadding Type

Defines padding for cells within the table.

PropertyTypeDescription
paddingnumberUniform padding for cells.
paddingHorizontalnumberHorizontal padding for cells.
paddingVerticalnumberVertical padding for cells.

SortingIcons Type

Specifies icons for ascending and descending sorting states.

PropertyTypeDescription
ascReactNodeIcon for ascending sort state.
descReactNodeIcon for descending sort state.

BorderStyle Type

Controls visibility and styling of borders within the table.

PropertyTypeDescription
showVerticalbooleanShows vertical cell borders when true.
showHorizontalHeaderbooleanShows horizontal border below the header when true.
showHorizontalBodybooleanShows horizontal borders between rows when true.
borderWidthnumberWidth of the borders.
borderColorstringColor of the borders.

Example with Custom Cell and Header Renderers

import React from 'react';
import { Text } from 'react-native';
import { Table } from '@coligo/react-native-table';

const MyCustomTable = () => {
  const data = [
    { id: 1, name: 'John Doe', age: 28 },
    { id: 2, name: 'Jane Smith', age: 34 },
  ];

  const columns = [
    {
      label: 'Name',
      key: 'name',
      render: (item) => <Text style={{ fontWeight: 'bold' }}>{item}</Text>,
    },
    {
      label: 'Age',
      key: 'age',
      header: (label) => <Text style={{ color: 'blue' }}>{label}</Text>,
      sortable: true,
    },
  ];

  return (
    <Table
      data={data}
      columns={columns}
      keyExtractor="id"
      sortingIcons={{
        asc: <Text>🔼</Text>,
        desc: <Text>🔽</Text>,
      }}
      borderStyle={{
        showVertical: true,
        borderWidth: 1,
        borderColor: '#ccc',
      }}
      cellPadding={{
        paddingHorizontal: 10,
        paddingVertical: 5,
      }}
    />
  );
};

export default MyCustomTable;

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT