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

react-custom-list

v1.0.1

Published

React Component for a dynamic list of custom items.

Downloads

12

Readme

react-custom-list

React Component for a dynamic list of custom items.

Installation

Through npm:

npm install react-custom-list --save

Peer dependencies

You should be using React 0.14.x in your project.

Usage

Simply import it and use it. For example:

import React from 'react';
import ReactDOM from 'react-dom';
import ReactList from '@luisincrespo/react-list';

class DummyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.resetItems = this.resetItems.bind(this);
    this.clearItems = this.clearItems.bind(this);
    this.pushItem = this.pushItem.bind(this);
    this.unshiftItem = this.unshiftItem.bind(this);
    this.addItemBelowFirst = this.addItemBelowFirst.bind(this);
    this.addItemAboveFirst = this.addItemAboveFirst.bind(this);
    this.editFirstItem = this.editFirstItem.bind(this);
    this.setFirstItem = this.setFirstItem.bind(this);
    this.removeFirstItem = this.removeFirstItem.bind(this);
  }

  resetItems(event) {
    event.preventDefault();

    this.reactList.setItems([
      { name: 'Item 1' },
      { name: 'Item 2' },
      { name: 'Item 3' }
    ]);
  }

  clearItems(event) {
    event.preventDefault();

    this.reactList.clearItems();
  }

  pushItem(event) {
    event.preventDefault();

    this.reactList.pushItem({
      name: 'Item X'
    });
  }

  unshiftItem(event) {
    event.preventDefault();

    this.reactList.unshiftItem({
      name: 'Item X'
    });
  }

  addItemBelowFirst(event) {
    event.preventDefault();

    const firstKey = this.reactList.getKeys()[0];

    this.reactList.addItemBelow(firstKey, { name: 'Item X' });
  }

  addItemAboveFirst(event) {
    event.preventDefault();

    const firstKey = this.reactList.getKeys()[0];

    this.reactList.addItemAbove(firstKey, { name: 'Item X' });
  }

  editFirstItem(event) {
    event.preventDefault();

    const firstKey = this.reactList.getKeys()[0];

    this.reactList.editItem(firstKey, { dummyProp: 'Dummy prop' });
  }

  setFirstItem(event) {
    event.preventDefault();

    const firstKey = this.reactList.getKeys()[0];

    this.reactList.setItem(firstKey, { dummyProp: 'Dummy prop' });
  }

  removeFirstItem(event) {
    event.preventDefault();

    const firstKey = this.reactList.getKeys()[0];

    this.reactList.removeItem(firstKey);
  }

  render() {
    return (
      <div>
        <ReactList
          initialItems={[
            { name: 'Item 1' },
            { name: 'Item 2' },
            { name: 'Item 3' }
          ]}
          showItemSearch
          itemSearchPredicate={
            (item, query) =>
            item.name.toLowerCase().startsWith(query.toLowerCase())
          }
          onItemSearch={
            (query, allItems, filteredItems) => {
              console.log(query);
              console.log(allItems);
              console.log(filteredItems);
            }
          }
          onItemsSet={
            (oldItems, newItems) => {
              console.log(oldItems);
              console.log(newItems);
            }
          }
          onItemsClear={(items) => console.log(items)}
          onItemPush={(item) => console.log(item)}
          onItemUnshift={(item) => console.log(item)}
          onItemAddBelow={
            (key, existingItem, newItem) => {
              console.log(key);
              console.log(existingItem);
              console.log(newItem);
            }
          }
          onItemAddAbove={
            (key, existingItem, newItem) => {
              console.log(key);
              console.log(existingItem);
              console.log(newItem);
            }
          }
          onItemRemove={
            (key, item) => {
              console.log(key);
              console.log(item);
            }
          }
          onItemEdit={
            (key, oldItem, editedItem) => {
              console.log(key);
              console.log(oldItem);
              console.log(editedItem);
            }
          }
          onItemSet={
            (key, oldItem, newItem) => {
              console.log(key);
              console.log(oldItem);
              console.log(newItem);
            }
          }
          ref={(ref) => { this.reactList = ref; }}
        />
        <button onClick={this.resetItems}>Reset Items</button>
        <button onClick={this.clearItems}>Clear Items</button>
        <button onClick={this.pushItem}>Push Item</button>
        <button onClick={this.unshiftItem}>Unshift Item</button>
        <button onClick={this.addItemBelowFirst}>
          Add Item Below 1st
        </button>
        <button onClick={this.addItemAboveFirst}>
          Add Item Above 1st
        </button>
        <button onClick={this.editFirstItem}>Edit 1st Item</button>
        <button onClick={this.setFirstItem}>Set 1st Item</button>
        <button onClick={this.removeFirstItem}>
          Remove 1st Item
        </button>
      </div>
    );
  }
}

ReactDOM.render(<DummyComponent/>, document.getElementById('main'));

Watch a live demo of the previous code snippet here http://luisincrespo.github.io/react-list.

API

getSize() => number

Returns the amount of items in the list.

getKeys() => array<number>

Returns the key for every item in the list, in the same order as they appear.

getKey(predicate: (item: object) => bool) => number

Returns the key for the first item that fulfills the given predicate. If no item meets the condition, undefined is returned.

getItems() => array<object>

Returns every item in the list, in the same order as they appear.

getItem(key: number) => object

Returns the item corresponding to the given key. If the key doesn't exist, undefined is returned.

getFirstItem() => object

Returns the first item of the list if it exists, undefined otherwise.

getLastItem() => object

Returns the last item of the list if it exists, undefined otherwise.

setItems(newItems: array<object>) => void

Replaces the whole list with the given newItems.

clearItems() => void

Removes all items from the list.

pushItem(item: object) => void

Adds the given item to the end of the list.

unshiftItem(item: object) => void

Adds the given item to the beginning of the list.

addItemBelow(key: number, newItem: object) => void

Adds the given newItem directly below the item that corresponds to the given key. If the key doesn't exist, the newItem will be added at the beginning of the list.

addItemAbove(key: number, newItem: object) => void

Adds the given newItem directly above the item that corresponds to the given key. If the key doesn't exist, the newItem will be added at the beginning of the list.

removeItem(key: number) => void

Removes the item corresponding to the given key if it exists.

editItem(key: number, newItem: object) => void

Edits the item corresponding to the given key if it exists, merging it with the content of the newItem.

setItem(key: number, item: object) => void

Replaces the item corresponding to the given key if it exists, with the specified item.

Props

initialItems: array<object> (OPTIONAL)

Specifies initial items for the list.

showItemSearch: bool (DEFAULTS to false)

Specifies whether or not to show the search widget.

itemSearchContent: React Component (DEFAULTS to DefaultItemSearchContent)

Specifies a component to be used to render the search widget. Defaults to:

class DefaultItemSearchContent extends React.Component {
  constructor(props) {
    super(props);
    this._onSearch = this._onSearch.bind(this);
  }

  _preventDefault(event) {
    event.preventDefault();
  }

  _onSearch(event) {
    event.preventDefault();

    this.props.onQueryChange(this.query.value);
  }

  render() {
    return (
      <form onSubmit={this._preventDefault}>
        <label>Search: </label>
        <input
          type="search"
          onChange={this._onSearch}
          ref={(ref) => { this.query = ref; }}
        />
      </form>
    );
  }
}

DefaultItemSearchContent.propTypes = {
  onQueryChange: React.PropTypes.func.isRequired
};

The following props will be available for the component:

  • onQueryChange(query: string) => void: Must be called to actually filter the list. The itemSearchPredicate prop (defined below) will be used to filter the list with the given query.

itemSearchPredicate(item: object, query: string) => bool (REQUIRED if showItemSearch is set to true)

Specifies a predicate to be used when filtering the list.

onItemSearch(query: string, allItems: array<object>, filteredItems: array<object>) => void (OPTIONAL)

Specifies a callback to be fired whenever the list is filtered with the given query, receiving allItems and filteredItems as parameters.

itemContent: React Component (DEFAULTS to DefaultItemContent)

Specifies a component to be used to render each item in the list. Defaults to:

class DefaultItemContent extends React.Component {
  constructor(props) {
    super(props);
    this._onRemove = this._onRemove.bind(this);
  }

  _onRemove(event) {
    event.preventDefault();

    this.props.onRemove();
  }

  render() {
    return (
      <li>
        {JSON.stringify(this.props.item.toJS())}
        <div>
          <a
            href="#"
            onClick={this._onRemove}
          >
            Remove
          </a>
        </div>
      </li>
    );
  }
}

DefaultItemContent.propTypes = {
  item: React.PropTypes.object.isRequired,
  onRemove: React.PropTypes.func.isRequired,
  onEdit: React.PropTypes.func.isRequired
};

The following props will be available for the component:

  • onRemove() => void: Can be called to remove the item from the list.
  • onEdit(newItem: object) => void: Can be called to edit the item with the content specified in the newItem.

onItemsSet(oldItems: array<object>, newItems: array<object>) => void (OPTIONAL)

Specifies a callback to be fired whenever the list is replaced, receiving the oldItems and newItems as parameters.

onItemsClear(items: array<object>) => void (OPTIONAL)

Specifies a callback to be fired whenever the list is cleared, receiving the cleared items as a parameter.

onItemPush(item: object) => void (OPTIONAL)

Specifies a callback to be fired whenever an item is pushed to the list.

onItemUnshift(item: object) => void (OPTIONAL)

Specifies a callback to be fired whenever an item is unshifted to the list.

onItemAddBelow(key: number, existingItem: object, newItem: object) => void (OPTIONAL)

Specifies a callback to be fired whenever a newItem is added below another one (existingItem corresponding to the given key) in the list.

onItemAddAbove(key: number, existingItem: object, newItem: object) => void (OPTIONAL)

Specifies a callback to be fired whenever a newItem is added above another one (existingItem corresponding to the given key) in the list.

onItemRemove(key: number, item: object) => void (OPTIONAL)

Specifies a callback to be fired whenever an item is removed from the list, receiving also its key as a parameter.

onItemEdit(key: number, oldItem: object, editedItem: object) => void (OPTIONAL)

Specifies a callback to be fired whenever an item (oldItem corresponding to the given key) is edited, receiving also the new content (editedItem) as a parameter.

onItemSet(key: number, oldItem: object, newItem: object) => void (OPTIONAL)

Specifies a callback to be fired whenever an item (oldItem corresponding to the given key) is set (replaced), receiving also the newItem as a parameter.

itemsEmptyContent: React Component (DEFAULTS to DefaultItemsEmptyContent)

Specifies a component to be used to render the widget to show when the list is empty. Defaults to:

class DefaultItemsEmptyContent extends React.Component {
  render() {
    return (
      <span>No items.</span>
    );
  }
}