File size: 5,090 Bytes
b9fe2b4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
'use client';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import {
ChevronLeftIcon,
ChevronRightIcon,
SquareCheckIcon,
SquareIcon,
} from 'lucide-react';
import React from 'react';
export type TransferListItemType = {
key: string;
label: string;
selected?: boolean;
};
export default function TransferList({
items,
}: {
items: TransferListItemType[];
}) {
const [leftList, setLeftList] = React.useState<TransferListItemType[]>(items);
const [rightList, setRightList] = React.useState<TransferListItemType[]>([]);
const [leftSearch, setLeftSearch] = React.useState('');
const [rightSearch, setRightSearch] = React.useState('');
const moveToRight = () => {
const selectedItems = leftList.filter((item) => item.selected);
setRightList([...rightList, ...selectedItems]);
setLeftList(leftList.filter((item) => !item.selected));
};
const moveToLeft = () => {
const selectedItems = rightList.filter((item) => item.selected);
setLeftList([...leftList, ...selectedItems]);
setRightList(rightList.filter((item) => !item.selected));
};
const toggleSelection = (
list: TransferListItemType[],
setList: React.Dispatch<React.SetStateAction<TransferListItemType[]>>,
key: string,
) => {
const updatedList = list.map((item) => {
if (item.key === key) {
return { ...item, selected: !item.selected };
}
return item;
});
setList(updatedList);
};
return (
<div className="flex space-x-4">
<div className="w-1/2 shadow-sm bg-background rounded-sm">
<div className="flex items-center justify-between">
<Input
placeholder="Search"
className="rounded-br-none rounded-bl-none rounded-tr-none focus-visible:ring-0 focus-visible:border-blue-500"
value={leftSearch}
onChange={(e) => setLeftSearch(e.target.value)}
/>
<Button
className="rounded-tl-none rounded-bl-none rounded-br-none border-l-0"
onClick={moveToRight}
size="icon"
variant="outline"
>
<ChevronRightIcon className="h-4 w-4" />
</Button>
</div>
<ul className="h-[200px] border-l border-r border-b rounded-br-sm rounded-bl-sm p-1.5 overflow-y-scroll">
{leftList
.filter((item) =>
item.label.toLowerCase().includes(leftSearch.toLowerCase()),
)
.map((item) => (
<li
className="flex items-center gap-1.5 text-sm hover:bg-muted rounded-sm"
key={item.key}
>
<button
type={'button'}
className="flex items-center gap-1.5 w-full p-1.5"
onClick={() =>
toggleSelection(leftList, setLeftList, item.key)
}
>
{item.selected ? (
<SquareCheckIcon className="h-5 w-5 text-muted-foreground/50" />
) : (
<SquareIcon className="h-5 w-5 text-muted-foreground/50" />
)}
{item.label}
</button>
</li>
))}
</ul>
</div>
<div className="w-1/2 shadow-sm bg-background rounded-sm">
<div className="flex items-center justify-between">
<Button
className="rounded-tr-none rounded-br-none rounded-bl-none border-r-0"
onClick={moveToLeft}
size="icon"
variant="outline"
>
<ChevronLeftIcon className="h-4 w-4" />
</Button>
<Input
placeholder="Search"
className="rounded-bl-none rounded-br-none rounded-tl-none focus-visible:ring-0 focus-visible:border-blue-500"
value={rightSearch}
onChange={(e) => setRightSearch(e.target.value)}
/>
</div>
<ul className="h-[200px] border-l border-r border-b rounded-br-sm rounded-bl-sm p-1.5 overflow-y-scroll">
{rightList
.filter((item) =>
item.label.toLowerCase().includes(rightSearch.toLowerCase()),
)
.map((item) => (
<li
className="flex items-center gap-1.5 text-sm hover:bg-muted rounded-sm"
key={item.key}
>
<button
type="button"
className="flex items-center gap-1.5 w-full p-1.5"
onClick={() =>
toggleSelection(rightList, setRightList, item.key)
}
>
{item.selected ? (
<SquareCheckIcon className="h-4 w-4 text-muted-foreground/50" />
) : (
<SquareIcon className="h-4 w-4 text-muted-foreground/50" />
)}
{item.label}
</button>
</li>
))}
</ul>
</div>
</div>
);
}
|