Skip to content

Instantly share code, notes, and snippets.

@yeehaa123
Created April 29, 2018 11:58
Show Gist options
  • Save yeehaa123/1e7bab249aed1a746983ecc18c05e54c to your computer and use it in GitHub Desktop.
Save yeehaa123/1e7bab249aed1a746983ecc18c05e54c to your computer and use it in GitHub Desktop.
Sortable List

Lists can be sortable...

initialState = {
  items: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]
};
const onSortEnd = ({ oldIndex, newIndex }) => {
  setState({
    items: Sortable.move(state.items, oldIndex, newIndex)
  });
};
<List direction="horizontal" spacing="wide" sortable onSortEnd={onSortEnd}>
  {state.items.map((item, index) => <List.Item key={index}>{item}</List.Item>)}
</List>;

...with or without an handle

initialState = {
  items: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]
};

const SortHandle = <Icon is="button" name="sort" tabindex="-1" />;

const onSortEnd = ({ oldIndex, newIndex }) => {
  setState({
    items: Sortable.move(state.items, oldIndex, newIndex)
  });
};

<List sortable SortHandle={SortHandle} onSortEnd={onSortEnd}>
  {state.items.map((item, index) => <List.Item key={index}>{item}</List.Item>)}
</List>;

...with or without an handle

initialState = {
  items: [
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5",
    "Item 6",
    "Item 7",
    "Item 8",
    "Item 9"
  ]
};

const SortHandle = <Icon is="button" name="sort" tabindex="-1" />;

const onSortEnd = ({ oldIndex, newIndex }) => {
  setState({
    items: Sortable.move(state.items, oldIndex, newIndex)
  });
};
<List sortable direction="both" SortHandle={SortHandle} onSortEnd={onSortEnd}>
  {state.items.map((item, index) => <List.Item key={index}>{item}</List.Item>)}
</List>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment