From 4c4011a617a5589da13dafcf357b9caef7bf5b32 Mon Sep 17 00:00:00 2001 From: Zephyruso <127948745+Zephyruso@users.noreply.github.com> Date: Fri, 1 Sep 2023 00:12:33 +0800 Subject: [PATCH] feat: sortable column --- package.json | 1 + pnpm-lock.yaml | 14 ++++ src/components/ConnectionsModal.tsx | 114 ++++++++++++++++++++++------ src/pages/Connections.tsx | 22 +++++- 4 files changed, 123 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 333b2cd..cdff480 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@tabler/icons-solidjs": "^2.32.0", "@tanstack/solid-table": "^8.9.3", "@tanstack/solid-virtual": "3.0.0-beta.6", + "@thisbeyond/solid-dnd": "^0.7.4", "@types/byte-size": "^8.1.0", "@types/node": "^20.5.7", "@types/uuid": "^9.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e1f2b21..29636ef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,6 +35,9 @@ dependencies: '@tanstack/solid-virtual': specifier: 3.0.0-beta.6 version: 3.0.0-beta.6 + '@thisbeyond/solid-dnd': + specifier: ^0.7.4 + version: 0.7.4(solid-js@1.7.11) '@types/byte-size': specifier: ^8.1.0 version: 8.1.0 @@ -1296,6 +1299,17 @@ packages: engines: { node: '>=12' } dev: false + /@thisbeyond/solid-dnd@0.7.4(solid-js@1.7.11): + resolution: + { + integrity: sha512-jgV9EtR3gAtVsILG8p1OAGrhHIgnK4W04YxpyLgJRCDKEFYQWuDrMdUe8F5Kc6pcVXlC4IMXr4cB8fS2Ut3/Ow==, + } + peerDependencies: + solid-js: ^1.5 + dependencies: + solid-js: 1.7.11 + dev: false + /@tsconfig/node10@1.0.9: resolution: { diff --git a/src/components/ConnectionsModal.tsx b/src/components/ConnectionsModal.tsx index 7bdd4ae..a9194b8 100644 --- a/src/components/ConnectionsModal.tsx +++ b/src/components/ConnectionsModal.tsx @@ -1,41 +1,105 @@ -import { For } from 'solid-js' +import type { + DragEventHandler, + Draggable, + Droppable, +} from '@thisbeyond/solid-dnd' +import { + DragDropProvider, + DragDropSensors, + DragOverlay, + SortableProvider, + closestCenter, + createSortable, + useDragDropContext, +} from '@thisbeyond/solid-dnd' +import { For, createSignal } from 'solid-js' import { AccessorKey } from '~/config/connection' type ColumnVisibility = Partial> +type ColumnOrder = AccessorKey[] export default (props: { - data: ColumnVisibility - onChange: (value: ColumnVisibility) => void + order: ColumnOrder + visible: ColumnVisibility + onOrderChange: (value: ColumnOrder) => void + onVisibleChange: (value: ColumnVisibility) => void }) => { - const { onChange } = props + const [activeKey, setActiveKey] = createSignal(null) + const onDragStart = ({ draggable }: { draggable: Draggable }) => + setActiveKey(draggable.id as AccessorKey) + const onDragEnd = ({ + draggable, + droppable, + }: { + draggable: Draggable + droppable: Droppable + }) => { + if (draggable && droppable) { + const currentItems = props.order + const fromIndex = currentItems.indexOf(draggable.id as AccessorKey) + const toIndex = currentItems.indexOf(droppable.id as AccessorKey) + + if (fromIndex !== toIndex) { + const updatedItems = currentItems.slice() + + updatedItems.splice(toIndex, 0, ...updatedItems.splice(fromIndex, 1)) + props.onOrderChange(updatedItems) + } + } + } + + const FormRow = (p: { key: AccessorKey }) => { + const key = p.key + const sortable = createSortable(key) + const [state] = useDragDropContext()! + + return ( +
+
+ {key} + { + props.onVisibleChange({ + ...props.visible, + [key]: e.target.checked, + }) + }} + /> +
+
+ ) + } return ( <>