metacubexd/src/components/ConnectionsModal.tsx

121 lines
3.3 KiB
TypeScript
Raw Normal View History

import { useI18n } from '@solid-primitives/i18n'
2023-09-01 00:12:33 +08:00
import type {
DragEventHandler,
Draggable,
Droppable,
} from '@thisbeyond/solid-dnd'
import {
DragDropProvider,
DragDropSensors,
DragOverlay,
SortableProvider,
closestCenter,
createSortable,
useDragDropContext,
} from '@thisbeyond/solid-dnd'
2023-09-02 13:50:24 +08:00
import { For, Show, createSignal } from 'solid-js'
2023-09-01 15:10:57 +08:00
import { AccessorKey } from '~/config/enum'
2023-08-31 22:49:34 +08:00
type ColumnVisibility = Partial<Record<AccessorKey, boolean>>
2023-09-01 00:12:33 +08:00
type ColumnOrder = AccessorKey[]
2023-08-31 22:49:34 +08:00
export default (props: {
2023-09-01 00:12:33 +08:00
order: ColumnOrder
visible: ColumnVisibility
onOrderChange: (value: ColumnOrder) => void
onVisibleChange: (value: ColumnVisibility) => void
2023-08-31 22:49:34 +08:00
}) => {
const [t] = useI18n()
2023-09-01 00:12:33 +08:00
const [activeKey, setActiveKey] = createSignal<AccessorKey | null>(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 (
<div
use:sortable
class="sortable"
classList={{
'opacity-25': sortable.isActiveDraggable,
'transition-transform': !!state.active.draggable,
}}
>
2023-09-01 00:19:14 +08:00
<div class="m-1 flex cursor-grab justify-between p-1">
2023-09-02 13:50:24 +08:00
<span class="select-none">{t(key)}</span>
2023-09-01 00:12:33 +08:00
<input
type="checkbox"
class="toggle"
checked={props.visible[key]}
onChange={(e) => {
props.onVisibleChange({
...props.visible,
[key]: e.target.checked,
})
}}
/>
</div>
</div>
)
}
2023-08-31 22:49:34 +08:00
return (
<>
<input type="checkbox" id="connection-modal" class="modal-toggle" />
<div class="modal">
<div class="modal-box w-80">
2023-09-01 00:12:33 +08:00
<DragDropProvider
onDragStart={onDragStart}
onDragEnd={onDragEnd as DragEventHandler}
collisionDetector={closestCenter}
2023-08-31 22:49:34 +08:00
>
2023-09-01 00:12:33 +08:00
<DragDropSensors />
<div class="column self-stretch">
<SortableProvider ids={props.order}>
<For each={props.order}>{(key) => <FormRow key={key} />}</For>
</SortableProvider>
</div>
<DragOverlay>
2023-09-02 13:50:24 +08:00
<Show when={activeKey()}>
<div class="sortable">{t(activeKey()!)}</div>
</Show>
2023-09-01 00:12:33 +08:00
</DragOverlay>
</DragDropProvider>
<button
class="btn btn-neutral btn-sm ml-auto mt-4 block"
onClick={() => props.onOrderChange(Object.values(AccessorKey))}
>
{t('reset')}
</button>
2023-08-31 22:49:34 +08:00
</div>
2023-09-01 13:39:26 +08:00
<label class="modal-backdrop" for="connection-modal" />
2023-08-31 22:49:34 +08:00
</div>
</>
)
}