Skip to content

Commit 5db1c0c

Browse files
committed
feat(draggable-list): add-flags-to-disable-drag-and-deletion
1 parent 05fc770 commit 5db1c0c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/lib/draggable-list/index.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ interface IDraggableList
3030
| "onSelectionChange"
3131
> {
3232
items: ListItem[];
33+
/** Flag to disable drag operations in list. */
34+
dragDisabled?: boolean;
35+
/** Flag to disable delete operations in list. */
36+
deletionDisabled?: boolean;
3337
/** Returns the updated list after a delete or move operation. */
3438
updateCallback?: (updatedItems: ListItem[]) => void;
3539
/** Returns the selected item. */
@@ -45,6 +49,8 @@ function DraggableList({
4549
selectionCallback,
4650
className,
4751
renderDragPreview,
52+
dragDisabled = false,
53+
deletionDisabled = false,
4854
...props
4955
}: Readonly<IDraggableList>) {
5056
const list = useListData({
@@ -76,7 +82,7 @@ function DraggableList({
7682
aria-label={props["aria-label"] ?? "Reorderable list"}
7783
selectionMode="single"
7884
items={list.items}
79-
dragAndDropHooks={dragAndDropHooks}
85+
dragAndDropHooks={dragDisabled ? undefined : dragAndDropHooks}
8086
onSelectionChange={(keys) => {
8187
const keyArr = Array.from(keys);
8288
const selectedItem = list.getItem(keyArr[0]);
@@ -106,11 +112,13 @@ function DraggableList({
106112
>
107113
{({ isHovered }) => (
108114
<>
109-
<DragAndDropIcon className="size-4 cursor-grab" />
115+
{dragDisabled ? null : (
116+
<DragAndDropIcon className="size-4 cursor-grab" />
117+
)}
110118
<span className="text-klerosUIComponentsPrimaryText flex-1 text-base">
111119
{item.name}
112120
</span>
113-
{isHovered ? (
121+
{isHovered && !deletionDisabled ? (
114122
<Button
115123
className={"cursor-pointer hover:scale-105"}
116124
onPress={() => {

src/stories/draggable-list.stories.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const meta = {
99
component: DraggableList,
1010
title: "Draggable List",
1111
tags: ["autodocs"],
12+
argTypes: {
13+
dragDisabled: {
14+
control: "boolean",
15+
},
16+
deletionDisabled: {
17+
control: "boolean",
18+
},
19+
},
1220
} satisfies Meta<typeof DraggableList>;
1321

1422
export default meta;
@@ -30,6 +38,22 @@ export const Default: Story = {
3038
},
3139
};
3240

41+
/** Drag operations can be disabled with `dragDisabled ` */
42+
export const DragDisabled: Story = {
43+
args: {
44+
...Default.args,
45+
dragDisabled: true,
46+
},
47+
};
48+
49+
/** Delete operation can be disabled with `deletionDisabled ` */
50+
export const DeletionDisabled: Story = {
51+
args: {
52+
...Default.args,
53+
deletionDisabled: true,
54+
},
55+
};
56+
3357
export const CustomDragPreview: Story = {
3458
args: {
3559
themeUI: "light",

0 commit comments

Comments
 (0)