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

@render-props/items

v0.1.22

Published

A state container which provides an interface for adding and removing items from simple arrays and sets while maintaining immutability on those arrays and sets, allowing for strict-comparison in child components.

Downloads

9

Readme

Items

A state container which provides an interface for adding and removing items from simple arrays and sets while maintaining immutability on those arrays and sets, allowing for strict-comparison in child components.

Installation

yarn add @render-props/items or npm i @render-props/items

Usage

import Items, {ItemSet} from '@render-props/items'

const ArrayItems = props => (
  <Items initialItems={['foo']} minItems={0} maxItems={5}>
    {
      ({items, addItem, deleteItem, setItems, clearItems, includes}) => (
        <div>
          <span>
            <strong>
              Number of items:
            </strong>
            <span>
              {items.length}
            </span>
          </span>

          <form onSubmit={e => {
            e.preventDefault()
            const field = document.getElementById('item1')
            addItem(field.value)
            field.value = ''
          }}>
            <input id='item1' type='text' placeholder='Add item'/>
          </form>

          {items.map(item => (
            <button
              key={item}
              onClick={() => deleteItem(item)}
            >
              Delete '{item}'
            </button>
          ))}

          <button onClick={clearItems}>
            Clear items
          </button>
        </div>
      )
    }
  </Items>
)

const SetItems = props => (
  <ItemSet initialItems={['foo']} minItems={0} maxItems={5} onBoundMax={
    function ({items, addItem, deleteItem}) {
      const arrItems = Array.from(items)
      deleteItem(arrItems.shift())
      addItem(arrItems.pop())
    }
  }>
    {
      ({items, addItem, deleteItem, setItems, clearItems, includes}) => (
        <div>
          <span>
            <strong>
              Number of items:
            </strong>
            <span>
              {items.size}
            </span>
          </span>

          <form onSubmit={e => {
            e.preventDefault()
            const field = document.getElementById('item2')
            addItem(field.value)
            field.value = ''
          }}>
            <input id='item2' type='text' placeholder='Add item'/>
          </form>

          {
            Array.from(items).map(item => (
              <button
                key={item}
                onClick={() => deleteItem(item)}
              >
                Delete '{item}'
              </button>
            ))}

          <button onClick={clearItems}>
            Clear items
          </button>
        </div>
      )
    }
  </ItemSet>
)

Props

  • initialItems {array|set}
    • the initial state of items in the component
  • minItems {integer}
    • the minimum number of items that should be in the state
  • maxItems {integer}
    • the maximum number of items that can be added to the state
  • onBoundMin {function ({items <array|set>, addItem <function>, deleteItem <function>})}
    • called when the minimum bound has been reached. Callback should include one argument for object: {items, addItem, deleteItem} where items is the set of items which triggered the bounding error
  • onBoundMax {function ({items <array|set>, addItem <function>, deleteItem <function>})}
    • called when the maximum bound has been exceeded. Callback should include one argument for object: {items, addItem, deleteItem} where items is the set of items which triggered the bounding error
  • onChange {function (items <array|set>)}
    • called whenever an item is added or removed from the set. Receives one argument for items which reflects the latest state
  • onAdd {function (items <array|set>)}
    • called whenever an item is added to the set. Receives one argument for items which reflects the latest state
  • onDelete {function (items <array|set>)}
    • called whenever an item is removed from the set. Receives one argument for items which reflects the latest state

Render Props

Methods

  • addItem (...items <any>)
    • adds one or several items to the component state
  • deleteItem (...items <any>)
    • deletes one or several items from the component state
  • includes (item <any>)
    • returns true if @item is included in the component state, otherwise false
  • setItems (items <array|set>)
    • sets the component state to his new set of @items
  • clearItems ()
    • clears the component state of its items

State

  • items {<array|set>}
    • the items currently in the state of the component. Is array if Items component is used, and Set if ItemSet