0.1.17 • Published 8 months ago

@adrianbielawski/orderable-list v0.1.17

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

Orderable list

Quick start

$ npm i @adrianbielawski/orderable-list

See examples

import OrderableList, {
  OnDropParams,
  OnRemoveParams,
} from "@adrianbielawski/orderable-list";
import ToDoItem from "./toDoItem";

const initialItems = ["Buy something", "Take a walk"];

const ToDoList = () => {
  const [items, setItems] = useState<string[]>(initialItems);

  const handleAdd = (value: string) => {
    setItems([value, ...items]);
  };

  const handleDrop = (params: OnDropParams<string>) => {
    setItems(params.newItems);
  };

  const handleRemove = (params: OnRemoveParams<string>) => {
    setItems(params.newItems);
  };

  return (
    <>
      <YourFancyForm onSubmit={handleAdd} />
      <OrderableList
        items={items}
        itemComponent={ToDoItem}
        onDrop={handleDrop}
        onRemove={handleRemove}
      />
    </>
  );
};

Orderable list props

NameTypeDescription
itemsany[]Array of items displayed on the list.
itemComponentReact componentLearn more in ItemComponent docs.
classNamestringClass name for <ul> element.
itemClassNamestringClass name for <li> element.
animationDirection'right' or 'left'Direction of animation on removed item. Initial 'left'.
scrollTopAtnumberScroll body when grabbed element reach n pixels from the top. Initial 30.
scrollBottomAtnumberScroll body when grabbed element reach n pixels from the bottom. Initial 30.
onDropfunctionUse it to change your state of list items after reordering. Function takes params: OnDropParams<T> object with { newPosition: number, item: T, newItems: T[] }.
onRemovefunctionUse it to change your state of list items after removing. Function takes params: OnRemoveParams<T> object with { item: T, newItems: T[] }.

Item component

Create your ItemComponent which optionally contains Grabbable and RemoveButton:

import OrderableList, {
  ItemComponentProps,
} from "@adrianbielawski/orderable-list";

const ToDoItem = (props: ItemComponentProps<string>) => (
  <OrderableList.Grabbable className="to-do-item">
    {params.item}
    <OrderableList.RemoveButton className="remove-button">
      Remove
    </OrderableList.RemoveButton>
  </OrderableList.Grabbable>
);

export default ToDoItem;

See examples for different scenarios

ItemComponent takes following props

NameTypeDescription
itemanyItem from the items array passed to OrderableList
indexnumberCurrent position on the list starts from 0
grabbedbooleantrue if user grab this item
0.1.11

8 months ago

0.1.12

8 months ago

0.1.13

8 months ago

0.1.14

8 months ago

0.1.15

8 months ago

0.1.16

8 months ago

0.1.17

8 months ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago