Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-stately/selection

Package Overview
Dependencies
Maintainers
2
Versions
845
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-stately/selection

Spectrum UI components in React

  • 3.0.0-nightly.891
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is @react-stately/selection?

@react-stately/selection is a library that provides state management for selection in React applications. It is part of the React Spectrum collection of libraries and is designed to handle selection logic for lists, tables, and other components that require selection functionality.

What are @react-stately/selection's main functionalities?

Single Selection

This feature allows you to manage single selection state in a list. The code sample demonstrates how to use the `useSingleSelectListState` hook to manage the selection state of a list of items.

import { useSingleSelectListState } from '@react-stately/selection';

function SingleSelectList(props) {
  let state = useSingleSelectListState(props);

  return (
    <ul>
      {props.items.map((item) => (
        <li key={item.key} onClick={() => state.setSelectedKey(item.key)}>
          {item.name} {state.selectedKey === item.key && '(Selected)'}
        </li>
      ))}
    </ul>
  );
}

Multiple Selection

This feature allows you to manage multiple selection states in a list. The code sample demonstrates how to use the `useMultipleSelectionState` hook to manage the selection state of a list of items, allowing multiple items to be selected.

import { useMultipleSelectionState } from '@react-stately/selection';

function MultipleSelectList(props) {
  let state = useMultipleSelectionState(props);

  return (
    <ul>
      {props.items.map((item) => (
        <li key={item.key} onClick={() => state.toggleSelection(item.key)}>
          {item.name} {state.selectedKeys.has(item.key) && '(Selected)'}
        </li>
      ))}
    </ul>
  );
}

Selection Management

This feature provides a selection manager to handle complex selection logic. The code sample demonstrates how to use the `useSelectionManager` hook to manage selection actions like selecting all items, clearing selection, and toggling selection for individual items.

import { useSelectionManager } from '@react-stately/selection';

function SelectionManagerExample(props) {
  let manager = useSelectionManager(props.selectionState);

  return (
    <div>
      <button onClick={() => manager.selectAll()}>Select All</button>
      <button onClick={() => manager.clearSelection()}>Clear Selection</button>
      <ul>
        {props.items.map((item) => (
          <li key={item.key} onClick={() => manager.toggleSelection(item.key)}>
            {item.name} {manager.isSelected(item.key) && '(Selected)'}
          </li>
        ))}
      </ul>
    </div>
  );
}

Other packages similar to @react-stately/selection

FAQs

Package last updated on 09 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc