Fix sortable component as the sort order was broken.

This commit is contained in:
Satish Gandham 2024-06-06 13:18:35 +05:30
parent 509989279b
commit 53740fc57a
2 changed files with 2 additions and 3 deletions

View File

@ -1,6 +1,5 @@
import type { Meta, StoryObj } from "@storybook/react"; import type { Meta, StoryObj } from "@storybook/react";
import React from "react"; import React from "react";
import { Draggable } from "./draggable";
import { Sortable } from "./sortable"; import { Sortable } from "./sortable";
const meta: Meta<typeof Sortable> = { const meta: Meta<typeof Sortable> = {
@ -13,7 +12,7 @@ type Story = StoryObj<typeof Sortable>;
const data = [ const data = [
{ id: "1", name: "John Doe" }, { id: "1", name: "John Doe" },
{ id: "2", name: "Jane Doe 2" }, { id: "2", name: "Satish" },
{ id: "3", name: "Alice" }, { id: "3", name: "Alice" },
{ id: "4", name: "Bob" }, { id: "4", name: "Bob" },
{ id: "5", name: "Charlie" }, { id: "5", name: "Charlie" },

View File

@ -17,7 +17,7 @@ const moveItem = <T,>(
destination: T & Record<symbol, string>, destination: T & Record<symbol, string>,
keyExtractor: (item: T, index: number) => string keyExtractor: (item: T, index: number) => string
) => { ) => {
const sourceIndex = data.indexOf(source); const sourceIndex = data.findIndex((item, index) => keyExtractor(item, index) === keyExtractor(source, 0));
if (sourceIndex === -1) return data; if (sourceIndex === -1) return data;
const destinationIndex = data.findIndex((item, index) => keyExtractor(item, index) === keyExtractor(destination, 0)); const destinationIndex = data.findIndex((item, index) => keyExtractor(item, index) === keyExtractor(destination, 0));