1.0.17 • Published 4 years ago

antd-nestable v1.0.17

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

antd-nestable

Drag & drop hierarchical list made as a react component for Ant Design

npm codebeat badge npm npm bundle size GitHub

Project resource

https://github.com/primetwig/react-nestable

Install

yarn add antd-nestable

Usage

import Nestable from 'antd-nestable';

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 Demo = () => (
  <Nestable items={items} renderItem={renderItem} />
);

Props

PropertyTypeDefaultDescription
classNamestringundefinedExtra class name which can be passed to a root element.
itemsarray[]Array of items. Every item must be of shape { id: @uniq }.
thresholdint30Amount of pixels which mouse should move horizontally before increasing/decreasing level (nesting) of current element.
maxDepthint10Maximum available level of nesting.
collapsedbooleanfalseAre groups collapsed by default.
groupstring or intrandom stringDifferent group numbers may be passed if you have more than one nestable component on a page and want some extra styles for portal instances.
handlercomponentIf you pass react component here, you may use it in your render method.
childrenPropstring"children"Optional name of property with children.
renderItemfunction({ item, index }) => item.toString()Function for rendering every item. Has a single parameter with keys: item - item from your array, index - index of the item, collapseIcon - icon for items with children (allows you to collapse group), handler - component which you have passed via the same property, but covered with some additional events.
renderCollapseIconfunction({ isCollapsed }) => <DefaultIcon />Function for rendering collapse icon. Has a single parameter with keys: isCollapsed - boolean, true if this group has children and collapsed.
onChangefunction() => {}Callback which has two parameters: items - new array after position was changed, item - item which has been moved.
confirmChangefunction() => trueCallback which has two parameters: dragItem - item which is being dragged, destinationParent - item where dragItem is about to land. Let function return false if this movement should not happen.

Public methods

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

useRef

You can use ref control the component.

Example:

const nestableRef = useRef<{ collapse: (type: string | number[]) => void }>();
const collapse = (collapseCase: number) => {
  if (nestableRef.current) {
    switch (collapseCase) {
      case 0:
        nestableRef.current?.collapse('NONE');
        break;
      case 1:
        nestableRef.current?.collapse('ALL');
        break;
      case 2:
        nestableRef.current?.collapse([1]);
        break;
      default:
    }
  }
};
// Other Code
return (
  <Nestable
    ref={nestableRef}
    items={items}
    renderItem={renderItem}
    onChange={(value: []) => {
      console.log(value);
    }}
  />
)

Styles

To prevent overwriting the custom styles to antd, antd-nestable does not import the style files of components.

1.0.17

4 years ago

1.0.16

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago