use-row-selection.ts


Overview

The use-row-selection.ts file provides React hooks to manage and interact with row selection states, primarily designed for use in data table components. It utilizes React's state and memoization hooks to enable efficient selection handling, including selection tracking, clearing, and retrieving selected item identifiers. This file is especially useful when integrating with @tanstack/react-table, a popular React table library, and leverages lodash for utility functions.


Exports

useRowSelection

Purpose

useRowSelection is a custom React hook that manages the state of selected rows in a table or list. It provides methods to clear the selection, access the current selection state, and retrieve the count of selected rows.

Function Signature

function useRowSelection(): {
  rowSelection: RowSelectionState;
  setRowSelection: React.Dispatch<React.SetStateAction<RowSelectionState>>;
  rowSelectionIsEmpty: boolean;
  clearRowSelection: () => void;
  selectedCount: number;
}

Parameters

Returns

An object containing:

Usage Example

import { useRowSelection } from './use-row-selection';

function MyTableComponent() {
  const {
    rowSelection,
    setRowSelection,
    rowSelectionIsEmpty,
    clearRowSelection,
    selectedCount,
  } = useRowSelection();

  // Use these to manage selection state in your table UI
}

Implementation Details


UseRowSelectionType

This is a TypeScript utility type representing the return type of the useRowSelection hook.

export type UseRowSelectionType = ReturnType<typeof useRowSelection>;

This can be used for typing variables or props that consume the row selection state and methods.


useSelectedIds

Purpose

useSelectedIds is a generic React hook to retrieve the identifiers (id properties) of selected items from a given list, based on the row selection state.

Function Signature

function useSelectedIds<T extends Array<{ id: string }>>(
  rowSelection: RowSelectionState,
  list: T,
): { selectedIds: string[] }

Parameters

Returns

Usage Example

import { useSelectedIds } from './use-row-selection';

const data = [
  { id: 'a1', name: 'Item A' },
  { id: 'b2', name: 'Item B' },
  { id: 'c3', name: 'Item C' },
];

const { selectedIds } = useSelectedIds(rowSelection, data);
console.log(selectedIds); // e.g. ['a1', 'c3'] if those rows are selected

Implementation Details


Important Implementation Notes


Interaction with Other Parts of the System


Mermaid Diagram

classDiagram
    class useRowSelection {
        +rowSelection: RowSelectionState
        +setRowSelection: Dispatch<SetStateAction<RowSelectionState>>
        +rowSelectionIsEmpty: boolean
        +clearRowSelection(): void
        +selectedCount: number
    }

    class useSelectedIds~T~ {
        +selectedIds: string[]
    }

Summary

The use-row-selection.ts file encapsulates logic for handling row selection in React table components, offering a clean and performant API for managing selection state and extracting selected item IDs. It is well-suited for integration with @tanstack/react-table and follows best practices with React hooks and TypeScript typings. This file simplifies selection management, enabling developers to focus on building responsive and intuitive UI components.