transfer-list.tsx


Overview

transfer-list.tsx implements a React component named TransferList that provides a dual-list interface allowing users to transfer items between two lists: a left list and a right list. This UI pattern is commonly known as a "transfer list" or "shuttle list." The component supports item selection, filtering via search inputs, and moving selected items between lists with callback support for state synchronization with parent components.

This component is optimized for client-side rendering ('use client') and uses React hooks and memoization to manage state and performance. It also leverages iconography from the lucide-react library and UI elements from a local design system (e.g., Button, Input).


Exports Summary


Detailed API Documentation

Types

TransferListItemType

Represents the shape of an individual item in the transfer lists.

Property

Type

Description

key

string

Unique identifier for the item.

label

string

Display label for the item.

selected

boolean

Optional flag indicating if the item is selected. Defaults to false.

disabled

boolean

Optional flag indicating if the item is disabled (non-selectable). Defaults to false.


TransferListMoveDirection

Enum representing the direction of item movement between lists.

Value

Description

Left

Items moved from right to left list.

Right

Items moved from left to right list.


TransferListProps

Props accepted by the TransferList component.

Property

Type

Description

items

TransferListItemType[]

The full list of items to be displayed across both lists.

targetKeys

string[] (optional)

Array of keys representing items initially placed in the right list (target).

onChange

(targetKeys: string[], direction: TransferListMoveDirection, moveKeys: string[]) => void (optional)

Callback fired when items are moved between lists. Receives the new right list keys, direction of move, and keys of moved items.

children

(item: TransferListItemType) => ReactNode (optional)

Render prop to render additional content for each item in the right list.


Component: TransferList

A memoized React functional component rendering two searchable lists with transfer buttons in the middle.

Props

See TransferListProps above.

Internal State

Behavior & Implementation Details

Usage Example

import { TransferList, TransferListItemType, TransferListMoveDirection } from './transfer-list';

const items: TransferListItemType[] = [
  { key: '1', label: 'Item One' },
  { key: '2', label: 'Item Two', disabled: true },
  { key: '3', label: 'Item Three' },
];

function MyComponent() {
  const [selectedKeys, setSelectedKeys] = React.useState<string[]>([]);

  const handleChange = (
    targetKeys: string[],
    direction: TransferListMoveDirection,
    moveKeys: string[],
  ) => {
    console.log('Moved items:', moveKeys, 'to', direction);
    setSelectedKeys(targetKeys);
  };

  return (
    <TransferList
      items={items}
      targetKeys={selectedKeys}
      onChange={handleChange}
    >
      {(item) => item.disabled ? <em>(disabled)</em> : null}
    </TransferList>
  );
}

Important Implementation Details and Algorithms


Interactions With Other Parts of the System


Structure Diagram

classDiagram
    class TransferList {
        -leftList: TransferListItemType[]
        -rightList: TransferListItemType[]
        -leftSearch: string
        -rightSearch: string
        +moveToRight()
        +moveToLeft()
        +toggleSelection(list, setList, key)
        +render()
    }

    TransferList ..> TransferListItemType : uses
    TransferListMoveDirection <|-- TransferList : uses enum

Summary

The transfer-list.tsx file provides a reusable, accessible, and customizable React component for managing dual-list item transfers with search and selection capabilities. It cleanly separates concerns between state management, UI rendering, and event handling, making it suitable for integration into larger React applications that require item selection and organization workflows. Its design embraces React best practices with hooks and memoization, augmented by a modern iconography and UI toolkit.