0.0.5 • Published 3 months ago

@tcn/ui-table v0.0.5

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
3 months ago

Table

This repo contains classes and components used for implementing UI tables.

Architectural Overview

This table contrasts with our past efforts in how we separate the business of data management from data rendering, and this approach has greatly simplified both the implementation and usage of the table.

All responsibilities relating to display (including localization and formatting) are handled here in the table itself, where we have access to our I18n and theme contexts.

Data handling is confined to the DataSource interface (discussed in more detail below).

We have thus eliminated all of the entanglement between presentation and data handling.

Components

This package provides several components that can be used together or independently:

  • Table: The main table component that renders the data
  • TableColumn: Configures how each column is displayed
  • TableSearch: Runs a global search on the given data source
  • TablePager: Pagination controls for the data source

The components are designed to work independently from each other, but may consume a common DataSource, allowing you to:

  • Use them together for a complete table experience
  • Implement your own header/footer components
  • Customize the layout and styling

In other words, if you pass the Table and TablePager the same DataStore instance, the paging controls in the footer will affect the Table.

Table

The core component that renders the actual table (as a semantic HTML <table> element). It handles:

  • Column configuration
  • Data rendering
  • Sorting
  • Sticky columns
  • Row highlighting

Data Source

The Table components (except the TableColumn) require a DataSource which manages:

  • Data fetching and updates
  • Sorting
  • Filtering
  • Pagination

The DataSource interface and implementations are found in a separate ResourceStore module.

Available Data Sources

  1. StaticDataSource: For working with static data
  2. AIPDataSource: For working with AIP-compliant REST resources

Column Configuration

The Table is configured with TableColumn components that describe:

  • Column position
  • Header content
  • Footer content
  • Sorting behavior
  • Cell rendering
  • Sticky positioning
  • Width

Basic Column Example

const s = useStringTranslation();

<Table dataSource={personSource}>
  <TableColumn
    heading={s("Name")}
    fieldName="name"
  /> 
</Table>

Column Props

PropTypeRequiredDescription
headingReactNodeYesContent to display in the column header
footerReactNodeNoContent to display in the column footer
fieldNamestringNo*Name of the field in the data source
canSortbooleanNoWhether the column can be sorted
render(item: T) => ReactNodeNoCustom render function for cell content
sticky"start" \| "end"NoMakes the column stick to the start or end
widthnumberNoColumn width in pixels

* Required if canSort is true

Custom Cell Rendering

You can customize how cells are rendered using the render prop:

const d = useDateTranslation();
const s = useStringTranslation();

<Table dataSource={personSource}>
  <TableColumn
    heading={s("Birthdate")}
    fieldName="birthdate"
    render={(item) => d(item.birthdate)}
  />
</Table>

Action Columns

Columns that don't represent data (like action buttons):

<Table dataSource={personSource}>
  <TableColumn
    heading={s("Actions")}
    render={(item) => (
      <Button onClick={() => myDomain.edit(item)}>
        {s("Edit")}
      </Button>
    )}
    sticky="end"
  />
</Table>

Styling

The table components use CSS modules for styling. You can:

  • Override default styles using CSS modules
  • Use the provided CSS variables for consistent theming
  • Add custom classes to individual components