-
Notifications
You must be signed in to change notification settings - Fork 3
Chore/build configuration #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import React, { useEffect } from "react"; | ||
import { useListData } from "react-stately"; | ||
import { | ||
Button, | ||
ListBox, | ||
ListBoxItem, | ||
useDragAndDrop, | ||
type ListBoxItemProps, | ||
type ListBoxProps, | ||
type DragAndDropOptions, | ||
} from "react-aria-components"; | ||
import { cn } from "../../utils"; | ||
import DragAndDropIcon from "../../assets/svgs/drag-and-drop.svg"; | ||
import Trash from "../../assets/svgs/trash.svg"; | ||
import clsx from "clsx"; | ||
|
||
type ListItem = { | ||
id: string | number; | ||
name: string; | ||
value: any; | ||
}; | ||
interface IDraggableList | ||
extends Omit< | ||
ListBoxProps<ListBoxItemProps>, | ||
| "items" | ||
| "selectionMode" | ||
| "dragAndDropHooks" | ||
| "selectionBehavior" | ||
| "orientation" | ||
| "onSelectionChange" | ||
> { | ||
items: ListItem[]; | ||
/** Returns the updated list after a delete or move operation. */ | ||
updateCallback?: (updatedItems: ListItem[]) => void; | ||
/** Returns the selected item. */ | ||
selectionCallback?: (list: ListItem) => void; | ||
/** Display custom preview for the item being dragged. */ | ||
renderDragPreview?: DragAndDropOptions["renderDragPreview"]; | ||
} | ||
|
||
/** List that allows users to reorder items via drag and drop */ | ||
function DraggableList({ | ||
items, | ||
updateCallback, | ||
selectionCallback, | ||
className, | ||
renderDragPreview, | ||
...props | ||
}: Readonly<IDraggableList>) { | ||
const list = useListData({ | ||
initialItems: items, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!updateCallback) return; | ||
updateCallback(list.items); | ||
}, [list, updateCallback, items]); | ||
|
||
const { dragAndDropHooks } = useDragAndDrop({ | ||
getItems: (keys) => | ||
[...keys].map((key) => ({ "text/plain": list.getItem(key)!.name })), | ||
alcercu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getAllowedDropOperations: () => ["move"], | ||
onReorder(e) { | ||
if (e.target.dropPosition === "before") { | ||
list.moveBefore(e.target.key, e.keys); | ||
} else if (e.target.dropPosition === "after") { | ||
list.moveAfter(e.target.key, e.keys); | ||
} | ||
}, | ||
renderDragPreview, | ||
}); | ||
|
||
return ( | ||
<ListBox | ||
{...props} | ||
aria-label={props["aria-label"] ?? "Reorderable list"} | ||
selectionMode="single" | ||
items={list.items} | ||
dragAndDropHooks={dragAndDropHooks} | ||
onSelectionChange={(keys) => { | ||
const keyArr = Array.from(keys); | ||
const selectedItem = list.getItem(keyArr[0]); | ||
|
||
if (selectionCallback && selectedItem) selectionCallback(selectedItem); | ||
}} | ||
alcercu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
className={cn( | ||
"bg-klerosUIComponentsLightBackground rounded-base border-klerosUIComponentsStroke border", | ||
"w-95.5 py-4", | ||
"[&_div]:data-drop-target:outline-klerosUIComponentsPrimaryBlue [&_div]:data-drop-target:outline", | ||
className, | ||
)} | ||
> | ||
{(item) => ( | ||
<ListBoxItem | ||
textValue={item.name} | ||
className={({ isHovered, isDragging, isSelected }) => | ||
cn( | ||
"h-11.25 w-full cursor-pointer border-l-3 border-l-transparent", | ||
"flex items-center gap-4 px-4", | ||
"focus-visible:outline-klerosUIComponentsPrimaryBlue focus-visible:outline", | ||
(isHovered || isSelected) && "bg-klerosUIComponentsMediumBlue", | ||
isSelected && "border-l-klerosUIComponentsPrimaryBlue", | ||
isDragging && "cursor-grabbing opacity-60", | ||
) | ||
} | ||
> | ||
{({ isHovered }) => ( | ||
<> | ||
<DragAndDropIcon className="size-4 cursor-grab" /> | ||
<span className="text-klerosUIComponentsPrimaryText flex-1 text-base"> | ||
{item.name} | ||
</span> | ||
{isHovered ? ( | ||
<Button | ||
className={"cursor-pointer hover:scale-105"} | ||
onPress={() => { | ||
list.remove(item.id); | ||
}} | ||
> | ||
{({ isHovered: isButtonHovered }) => ( | ||
<Trash | ||
className={clsx( | ||
"ease-ease size-4 transition", | ||
isButtonHovered && | ||
"[&_path]:fill-klerosUIComponentsPrimaryBlue", | ||
)} | ||
/> | ||
)} | ||
</Button> | ||
) : null} | ||
alcercu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</> | ||
)} | ||
</ListBoxItem> | ||
)} | ||
</ListBox> | ||
); | ||
} | ||
|
||
export default DraggableList; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from "react"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { IPreviewArgs } from "./utils"; | ||
|
||
import DraggableList from "../lib/draggable-list"; | ||
|
||
const meta = { | ||
component: DraggableList, | ||
title: "Draggable List", | ||
tags: ["autodocs"], | ||
} satisfies Meta<typeof DraggableList>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta> & IPreviewArgs; | ||
|
||
export const Default: Story = { | ||
args: { | ||
themeUI: "light", | ||
backgroundUI: "light", | ||
items: [ | ||
{ id: 1, name: "Illustrator", value: "" }, | ||
{ id: 2, name: "Premiere", value: "" }, | ||
{ id: 3, name: "Acrobat", value: "" }, | ||
], | ||
}, | ||
render: (args) => { | ||
return <DraggableList {...args} />; | ||
}, | ||
}; | ||
|
||
export const CustomDragPreview: Story = { | ||
args: { | ||
themeUI: "light", | ||
backgroundUI: "light", | ||
items: [ | ||
{ id: 1, name: "Illustrator", value: "" }, | ||
{ id: 2, name: "Premiere", value: "" }, | ||
{ id: 3, name: "Acrobat", value: "" }, | ||
], | ||
renderDragPreview: (items) => ( | ||
<div className="rounded-base bg-klerosUIComponentsPrimaryBlue px-4 py-2"> | ||
<span className="text-klerosUIComponentsPrimaryText text-base"> | ||
{items[0]["text/plain"]} | ||
</span> | ||
</div> | ||
), | ||
}, | ||
render: (args) => { | ||
return <DraggableList {...args} />; | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.