react-base-table

repository·master·Indexed 23 days ago

https://github.com/autodesk/react-base-table

A high-performance React table component designed to display large datasets with high flexibility. Version 2.1.0 features built-in TypeScript declarations, virtualization support, and customizable UI via a components prop. It supports fixed and flex column modes, custom renderers, and SCSS variable overrides for styling. The library is compatible with all modern browsers (Chrome, Firefox, Safari, Edge), though Internet Explorer is no longer supported.

Tokens
10.8K
Snippets
15
Records
56
Agent score
81%

What's inside react-base-table

  1. Configure Column Keys and Row Keys

    master

    Column Keys

    Every Column definition must include a key prop, otherwise the column will be ignored.

    Row Keys

    To ensure data items are unique, BaseTable uses a key to identify rows. The default key used is id. If your data uses a different unique identifier, customize it using the rowKey prop on the BaseTable component.

  2. Configure unique keys for columns and rows

    master

    Column Keys

    Every Column definition must have a key prop, otherwise the column will be ignored.

    Row Keys

    Each item in your data array must be uniquely identifiable. By default, BaseTable uses the id field from your data objects. You can customize this by providing a rowKey prop to the BaseTable component.

  3. Customize cells and rows with custom renderers and props

    master
    For fine-grained control over how data is displayed, BaseTable provides highly flexible props following the pattern xxxRenderer and xxxProps. These allow you to inject custom rendering logic and custom properties into specific parts of the table (e.g., cells, rows, or headers).
  4. Configure column size and flex mode

    master

    Fixed Width

    width is a required prop for Column definitions.

    Flex Mode

    To create flexible columns, set fixed={false} on the Column. You can then set width={0} and flexGrow={1} to allow the column to grow and fill available space.

  5. Configure Column Widths and Flex Mode

    master

    Fixed Width

    By default, width is required for each Column definition.

    Flex Mode

    To enable flexible column widths, set fixed={false} on the table. You can then set a column's width={0} and provide a flexGrow={1} value to allow it to expand and fill available space.

  6. Run the react-base-table website locally

    master
    Start the local development server by running yarn start from the website directory. Once the server is running, visit http://localhost:8000 in your browser. The server supports hot reloading, so changes made to files in the src folder will be reflected synchronously in the browser.
    yarn start
  7. Implement Inline Editing in BaseTable

    master

    Because BaseTable uses virtualization with overflow: hidden on rows and cells, editing content that is larger than the cell area will be clipped if rendered directly inside the cell.

    To implement inline editing correctly, use a Portal (such as Overlay from react-overlays) to render the editing UI outside of the cell's DOM hierarchy. Most custom renderers in BaseTable provide a container prop, which refers to the table itself and should be used as the target container for your portal to ensure the editing UI is not constrained by cell boundaries.

    import { Overlay } from 'react-overlays';
    
    // Inside your custom CellRenderer component:
    const { container, rowIndex, columnIndex } = this.props;
    
    // ...
    {editing && this.targetRef && (
      <Overlay 
        show 
        flip 
        rootClose 
        container={container} 
        target={this.getTargetRef} 
        onHide={this.handleHide}
      >
        {({ props, placement }) => (
          <div {...props} style={{ ...props.style, width: this.targetRef.offsetWidth }}>
            {/* Your Editor Component (e.g., Input, Select) */}
          </div>
        )}
      </Overlay>
    )}
  8. Override BaseTable styles using SCSS

    master

    The simplest way to style BaseTable is to override its default SCSS variables. You can set a $table-prefix to avoid collisions and then import the base styles.

    // override default variables for BaseTable
    $table-prefix: AdvanceTable;
    
    $table-font-size: 13px;
    $table-padding-left: 15px;
    $table-padding-right: 15px;
    $column-padding: 7.5px;
    ...
    $show-frozen-rows-shadow: false;
    $show-frozen-columns-shadow: true;
    
    @import 'react-base-table/dist/esm/_BaseTable.scss';
    
    .#{$table-prefix} {
      // Custom overrides
    }
    // override default variables for BaseTable
    $table-prefix: AdvanceTable;
    
    $table-font-size: 13px;
    $table-padding-left: 15px;
    $table-padding-right: 15px;
    $column-padding: 7.5px;
    ...
    $show-frozen-rows-shadow: false;
    $show-frozen-columns-shadow: true;
    
    @import 'react-base-table/dist/esm/_BaseTable.scss';
    
    .#{$table-prefix} {
      &:not(.#{$table-prefix}--show-left-shadow) {
        .#{$table-prefix}__table-frozen-left {
          box-shadow: none;
        }
      }
    
      &:not(.#{$table-prefix}--show-right-shadow) {
        .#{$table-prefix}__table-frozen-right {
          box-shadow: none;
        }
      }
    
      ...
    }
  9. Set up the react-base-table website development environment

    master

    To set up the local development environment for the react-base-table website, clone the repository, navigate to the website directory, and install dependencies using yarn or npm.

    git clone https://github.com/Autodesk/react-base-table.git
    cd react-base-table
    cd website
    yarn # install dependencies
  10. Implement row selection via a custom recipe

    master

    Since selection is not a built-in feature, you can implement it by creating a wrapper component (e.g., SelectableTable) that manages selectedRowKeys in its internal state.

    To implement selection, follow these steps:

    1. Manage State: Maintain an array of selectedRowKeys in your component state. If the table is uncontrolled, use defaultSelectedRowKeys to initialize it.
    2. Create a Selection Column: Add a special column at the beginning of your columns array. This column should use a custom cellRenderer (like a checkbox) that calls an onChange handler.
    3. Handle Changes: In the onChange handler, update the selectedRowKeys state by adding or removing the row's key based on the interaction.
    4. Visual Feedback: Use the rowClassName prop to apply a CSS class (e.g., .row-selected) to rows whose keys are present in selectedRowKeys.
    5. Cleanup: Use a method like removeRowKeysFromState to purge keys from the internal state when rows are deleted to prevent stale selection data.
    const StyledTable = styled(BaseTable)`
      .row-selected {
        background-color: #e3e3e3;
      }
    `;
    
    class SelectionCell extends React.PureComponent {
      _handleChange = e => {
        const { rowData, rowIndex, column } = this.props;
        const { onChange } = column;
    
        onChange({ selected: e.target.checked, rowData, rowIndex });
      };
    
      render() {
        const { rowData, column } = this.props;
        const { selectedRowKeys, rowKey } = column;
        const checked = selectedRowKeys.includes(rowData[rowKey]);
    
        return <input type="checkbox" checked={checked} onChange={this._handleChange} />;
      }
    }
    
    class SelectableTable extends React.PureComponent {
      // ... implementation details for managing selectedRowKeys and rowClassName ...
    
      render() {
        const { columns, children, selectable, selectionColumnProps, ...rest } = this.props;
        const { selectedRowKeys } = this.state;
    
        let _columns = columns || normalizeColumns(children);
        if (selectable) {
          const selectionColumn = {
            width: 40,
            flexShrink: 0,
            resizable: false,
            frozen: Column.FrozenDirection.LEFT,
            cellRenderer: SelectionCell,
            ...selectionColumnProps,
            key: '__selection__',
            rowKey: this.props.rowKey,
            selectedRowKeys: selectedRowKeys,
            onChange: this._handleSelectChange,
          };
          _columns = [selectionColumn, ..._columns];
        }
    
        return <StyledTable {...rest} columns={_columns} rowClassName={this._rowClassName} />;
      }
    }