npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-multiselect

v1.3.5

Published

React hook for managing a the selection state of a set of items. Does not require full knowledge of the set of items.

Downloads

2,051

Readme

use-multiselect

React hook for managing the selection state of a set of items. Does not require the full list of items, so can therefore be used for lazy loaded lists.

NPM JavaScript Style Guide

Install

npm install --save use-multiselect

Usage

useMultiSelect manages the set of items that are currently selected. We try to be a little clever by only keeping track of the items that are different from the default selection state. The way this works is we keep track of whether the default state of items is selected or not selected, and then keep a list of the exceptions to the default state. This means it can be used to manage a large list of objects that aren't necessarily fully known. You could then, for instance, use 'select all' on a lazy loaded list where not all items have been downloaded. You also can choose to use useMultiselect as a standalone hook, or as a hook attached to a Context provider. Using the context provider allows you to better separate the multi select responsiblity among components. Standalone usage is shown below:

import * as React from "react";

import { useMultiSelect } from "use-multiselect";

const Example = () => {
  const { isSelected, setSelected } = useMultiSelect();
  return (
    <div>
      {items.map((item) => {
        return (
          <div>
            <label key={item}>
              {item}
              <input
                type="checkbox"
                item={item}
                checked={isSelected(item)}
                onChange={(ev) => setSelected(item, ev.target.checked)}
              />
            </label>
          </div>
        );
      })}
    </div>
  );
};

Provider Based

import * as React from "react";

import {
  useMultiSelectWithProvider,
  MultiSelectContextProvider,
} from "use-multiselect";
const UpperComponent = () => {
  <MultiSelectContextProvider>
    <div>
      Other stuff in here
      <Example />
    </div>
  </MultiSelectContextProvider>;
};
const Example = () => {
  const { isSelected, setSelected } = useMultiSelectWithProvider();
  return (
    <div>
      {items.map((item) => {
        return (
          <div>
            <label key={item}>
              {item}
              <input
                type="checkbox"
                item={item}
                checked={isSelected(item)}
                onChange={(ev) => setSelected(item, ev.target.checked)}
              />
            </label>
          </div>
        );
      })}
    </div>
  );
};

useMultiSelect (isMultiSelectActive: boolean, allSelected: boolean, exceptions: Array) => {...Returned functions}

useMultiSelect takes an initial state object defining which items are currently selected. allSelected defines whether the default state is selected or not, and exceptions is a list of keys that are set to the opposite of the default state. This is the same structure as returned by getSelectionState()

useMultiSelectWithProvider () => {...Returned functions}

useMultiSelectWithProvider has no parameters as it gets its values from the included Context Provider component. It returns the same functions as the useMultiSelect hook

MultiSelectContextProvider (isMultiSelectActive: boolean, allSelected: boolean, exceptions: Array)

This component provides the context used by useMultiSelectWithProvider. It should be placed as close to the consuming hooks as possible. It accepts initial values to be used by the consuming hooks. note that these are just initialization values and they are not monitored. Changing them will not affect the current state of the multiSelect

Returned functions/values

isMultiSelectActive: boolean

A convenience state to manage whether or not mutliSelect is currently active.

function setMultiSelectActive:(value: boolean) => void

Sets the current value of isMultiSelectActive. This doesn't affect the selection state at all. i.e. you can turn multiselect on and off, but the selected status of all items will remain the same

function setSelected(key: string, value: boolean) => void

Takes a key and value and sets the item to the given value.

function toggleSelected(key: string) => void

Takes a key and toggles the current selected value of that key

function selectAll() => void

Marks all keys as selected.

function deSelectAll() => void

Marks all keys as not selected.

function isSelected(key: string) => boolean

Returns true or false based on whether the given item iscurrently selected

function getAllSelectedKeys(keys: Array) => Array

Filters the given array of keys, returning just the selected ones.

function getSelectedCount(totalCount: number) => number

Returns the number of items currently selected. It requires the totalCount as a parameter so it can calculate the difference between the total number, and the selected amount, when for instance the select all is pressed and then a single item is deselected.

function getSelectionState() => {allSelected: boolean, exceptions: Array}

Returns the internal selection state used by useMultiSelect. This can be used, for instance, to support multiSelect with lazy loaded items. The returned object has enough information to recreate teh selection set elsewhere for processing of commands.

stateHash: string

This is a hash value defined by the internal state of the selection set(note, that this does not include the isMultiSelectActive field). This hash value can be used as a quick way to determine if the state of selection has changed. This can be particularly valuable for hook dependencies.

License

MIT © jschloer


This hook is created using create-react-hook.