3.0.2 • Published 5 months ago

react-nestable v3.0.2

Weekly downloads
3,383
License
ISC
Repository
github
Last release
5 months ago

Table of Contents

Demo

Demo

Installation

npm install -save react-nestable

Usage

import Nestable from 'react-nestable';

// this usually goes once
// to the entry point of the whole app
// (e.g. src/index.js)
import 'react-nestable/dist/styles/index.css';

// every item must have a unique `id`
// in order to distinguish elements
const items = [
  { id: 0, text: 'Andy' },
  {
    id: 1, text: 'Harry',
    children: [
      { id: 2, text: 'David' }
    ]
  },
  { id: 3, text: 'Lisa' }
];

const renderItem = ({ item }) => item.text;

const Example = () => (
  <Nestable
    items={items}
    renderItem={renderItem}
  />
);

Options

PropertyTypeDefaultDescription
childrenPropstring"children"Name of a property for children.
classNamestringundefinedExtra class name which can be passed to a root element.
collapsedbooleanfalseMakes groups collapsed by default.
confirmChangefunction() => trueCallback which has a single parameter with keys: dragItem - item which is being dragged, destinationParent - item where dragItem is about to land (or null if moving to root). Let function return false if this movement should not happen.
disableCollapsebooleanfalseDisable toggling a collapsed state of items with children. If you need to set a specific initial state, then it is still possible to do so with the public method collapse.
disableDragboolean or functionfalseDisable dragging. Pass boolean to apply to all items. Pass a callback to target individual items. It has a single parameter with keys: item - item from your array, index - number, index of the item, depth - number, depth of the item.
groupstring or numberrandom stringDifferent group names may be passed if you have more than one nestable component on a page and want some extra styles for portal instances.
handlernodeundefinedIf you pass it, it will get wrapped with drag handlers and you may use it in your render method.
idPropstring"id"Name of a property for id.
itemsarray[]Array of items. Every item must be of shape {id: @uniq} to distinguish elements.
maxDepthnumber10Maximum available level of nesting.
onChangefunction() => {}Callback which has a single parameter with keys: items - new array after position was changed, dragItem - item which has been moved, targetPath - array of numbers, those numbers are indices and they make path to a location, to where item has been moved.
onCollapseChangefunction() => {}Callback which has a single parameter with one of two possible keys: openIds - array of ids which are open if collapsed is set to ture, or closedIds - array of ids which are closed if collapsed is set to false. Note: this callback is triggered not only when user explicitly opens or closes an item, but when implicit events happen as well, like when the only child of open item is moved out.
onDragEndfunction() => {}Callback which has no parameters. It is invoked when dragging ends via drop or cancel.
onDragStartfunction() => {}Callback which has a single parameter with keys: dragItem - item which has been moved.
renderCollapseIconfunction() => <DefaultIcon />Function for rendering collapse icon. Has a single parameter with keys: isCollapsed - boolean, true if this group has children and is collapsed, item - item from your array.
renderItemfunction({item}) => String(item)Function for rendering every item. Has a single parameter with keys: item - item from your array, index - number, index of the item, depth - number, depth of the item, collapseIcon - node, icon for items with children (allows you to collapse the group), handler - node, which you have passed via the same property, but wrapped with some additional events, isDraggable - boolean, tells if dragging is allowed for this item (see disableDrag prop for details).
thresholdnumber30Amount of pixels which mouse should move horizontally before increasing/decreasing level (nesting) of current element.

Public methods

MethodAcceptsDescription
collapsestring or array"NONE" - expand all groups; "ALL" - collapse all groups; [] - collapse all groups with ids from given array.

Todo

  • add touch
  • cover with tests

PS: Please, make an issue or create a PR if you need any more functionality.

License

ISC

3.0.2

5 months ago

3.0.1

6 months ago

3.0.0

6 months ago

2.0.0

3 years ago

1.3.0

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.0

9 years ago