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

@wazza99/use-sortable

v1.0.7

Published

A react hook used in sorting and managing data in Drag n Drop layouts, it allows sorting and reordering of items within its column and also across multiple columns, it also allows the reordering of columns in the context. this hook also reorder based on t

Downloads

2

Readme

Use Sortable

A react hook used in sorting and managing data in Drag n Drop layouts, it allows sorting and reordering of items within its column and also across multiple columns, it also allows the reordering of columns in the context. this hook also reorder based on the data type order, this allows the persistence of the DnD layout with the backend of your application. This hook can also be used with any react Drag n Drop library as long as the input type is consistent.

Features

  • Allows sorting and reordering of items within its column and also across multiple columns
  • Allows the reordering of columns in the context
  • Sorts based on the data type order, this allows the persistence of the DnD layout with the backend of your application
  • Can be used with any react Drag n Drop library as long as the input type is consistent
  • Uses update functions to persist data on your backend, while using optimistic updates.
  • Reverts optimistic updates in the event of an error.

Installation

Using npm:

$ npm install @wazza99/use-sortable

Data Structure

Using a data structure where we can have multiple columns and each columns could contain multiple items.

graph TB
C[Columns] --> C1
C[Columns] --> C2
C1[Column 1]  --> I1[Item 1]
C1[Column 1]  --> I2[Item 2]
C1[Column 1]  --> I3[Item 3]
C2[Column 2]  --> I4[Item 4]
C2[Column 2]  --> I5[Item 5]
C2[Column 2]  --> I6[Item 6]

Usage

Basic

import { useSortable } from '@wazza99/use-sortable';

//initial data
const initialColumns= [
	{
	id:  'column1',
	order:  1,
	name:  'Todo',
	tasks: [
		{
			id:'task1',
			title:'Task 1 title',
			order:1,
			//other data points...
		},
		{
			id:'task2',
			title:'Task 2 title',
			order:2,
		}
	],
	//other data points...
 },
	{
	id:  'column2',
	order:  2,
	name:  'In-Progress',
	tasks:[
		{
			id:"task3",
			order:1
			title:"Task 3 title"
		}
	]
	}
	]

const { columns, dragEndHandler, fns} = useSortable(initialColumns, 'tasks');

const options = { reorderColumn:true, columnsDroppableId:'all-columns' }

const  handleDrag = (result) => {
	dragEndHandler( result, options );
};

The result parameter

The result parameter is of type DropInfo which contains the destination and source information of the dropped item, it also contains the draggableId of the dropped item. The result parameter will look something like this:

const result = {
  destination: { droppableId: 'column1', index: 1 },
  source: { droppableId: 'column1', index: 0 },
  draggableId: 'task1',
};

Its important that no matter the DnD library you use, the result data you pass in after a drag drop operation should always look like this.

With optimistic updates

import type {
  OptimisticItemData,
  OptimisticColumnData,
} from '@wazza99/use-sortable';

async function updateItem(values: OptimisticItemData) {
  //values contains the previous and current optimistc info of the dropped item, and also the operation type, you can use this to update your backend
  const data = await createPromise(values, 3000);
}

async function updateColumn(values: OptimisticColumnData) {
  //values contains the previous and current optimistc info of the dropped column, and also the operation type, you can use this to update your backend
  const data = await createPromise(values, 3000);
}

const options = { reorderColumns: true, columnsDroppableId: 'all-columns' };
const updates = { updateItem, updateColumn };

const handleDrag = (result) => {
  dragEndHandler(result, options, updates);
};

Helper functions

const { columns, dragEndHandler, fns } = useSortable(initialColumns, 'tasks');

// Example usage of the functions
fns.createColumnItem('column2', { id: 'task4', title: 'Task 4 title' });
fns.removeColumnItem('task1');
fns.updateColumnItem('task1', { title: 'New title' });
fns.createColumn({ id: 'column2', name: 'Doing', tasks: [] });
fns.updateColumn('column2', { name: 'New name' });
fns.removeColumn('column2');
fns.changeItemColumn('task1', 'column2');

API

Parameters

| Parameter | Type | Description | | ---------------- | -------- | ----------------------------------------- | | initialColumns | array | Initial set of columns. | | key | string | Key to identify the items key in columns. |

Returns

| Property | Type | Description | | ---------------- | ---------- | --------------------------------------------------- | | columns | array | The state of sorted columns and items. | | dragEndHandler | function | Function to handle drag end events. | | fns | object | Object containing functions to manipulate columns . |

fns Object

| Function | Description | Type | | ------------------ | ------------------------------------------- | ---------- | | createColumnItem | Creates a new item in the specified columnn | function | | removeColumnItem | Removes an item from the columns. | function | | updateColumnItem | Updates an item in the columns. | function | | createColumn | Creates a new column. | function | | updateColumn | Updates a column. | function | | removeColumn | Removes a column. | function | | changeItemColumn | Changes the column of an item. | function |

Important Notes

  • All columns and items must have a unique Id and and initial order.
  • If using a DnD react library, it is important that the draggable id of every item is the same as its item id, this also applies in the case of columns.
  • Any sort of data could be passed into columns or items, but it is required they each have an order and a unique id, it is also required that each column have an array of items.

Live example

This example uses @hello-pangea/dnd as the DnD library.

Contributing

Contributions are welcome in this project, follow the steps below to get started.

  1. Fork the project
  2. Create a new branch for your feature or bug fix
  3. Commit your changes and push to your forked repo
  4. Submit a pull request

Please note that before writing any code, it's a good idea to discuss the change you wish to make via an issue or a pull request. This will help prevent wasted time and ensure that your changes are in line with the project's goals.