intable 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/docs/index-BaMALNy6.css +1 -0
- package/docs/index-CDN48t9E.js +3 -0
- package/docs/index-Cc4RNkLY.css +1 -0
- package/docs/index-MRnbkYmU.js +3 -0
- package/docs/index.html +15 -0
- package/docs/vite.svg +1 -0
- package/index.html +14 -0
- package/package.json +30 -37
- package/packages/intable/README.md +379 -0
- package/packages/intable/package.json +51 -0
- package/packages/intable/src/assets/ClearFormat.svg +3 -0
- package/packages/intable/src/assets/Forms.svg +4 -0
- package/packages/intable/src/assets/MergeCell.svg +4 -0
- package/packages/intable/src/assets/SplitCell.svg +4 -0
- package/packages/intable/src/assets/gap.svg +3 -0
- package/packages/intable/src/assets/loading.svg +12 -0
- package/packages/intable/src/assets/paint.svg +9 -0
- package/packages/intable/src/assets/solid.svg +1 -0
- package/packages/intable/src/components/Columns.tsx +86 -0
- package/packages/intable/src/components/DocTree.tsx +36 -0
- package/packages/intable/src/components/Menu.tsx +109 -0
- package/packages/intable/src/components/Popover.tsx +55 -0
- package/packages/intable/src/components/RecycleList.tsx +99 -0
- package/packages/intable/src/components/Render.tsx +26 -0
- package/packages/intable/src/components/Split.tsx +56 -0
- package/packages/intable/src/components/Tree.tsx +115 -0
- package/packages/intable/src/components/utils.tsx +12 -0
- package/packages/intable/src/hooks/index.ts +200 -0
- package/packages/intable/src/hooks/useDir.ts +78 -0
- package/packages/intable/src/hooks/useSelector.ts +91 -0
- package/packages/intable/src/hooks/useSort.tsx +118 -0
- package/packages/intable/src/hooks/useVirtualizer.ts +180 -0
- package/packages/intable/src/index.tsx +481 -0
- package/packages/intable/src/plugins/CellChangeHighlightPlugin.tsx +5 -0
- package/packages/intable/src/plugins/CellMergePlugin.tsx +153 -0
- package/packages/intable/src/plugins/CellSelectionPlugin.tsx +175 -0
- package/packages/intable/src/plugins/CommandPlugin.tsx +74 -0
- package/packages/intable/src/plugins/CopyPastePlugin.tsx +63 -0
- package/packages/intable/src/plugins/DiffPlugin.tsx +107 -0
- package/packages/intable/src/plugins/DragPlugin.tsx +81 -0
- package/packages/intable/src/plugins/EditablePlugin.tsx +252 -0
- package/packages/intable/src/plugins/ExpandPlugin.tsx +80 -0
- package/packages/intable/src/plugins/HeaderGroup.tsx +289 -0
- package/packages/intable/src/plugins/HistoryPlugin.tsx +49 -0
- package/packages/intable/src/plugins/MenuPlugin.tsx +195 -0
- package/packages/intable/src/plugins/RenderPlugin/components.tsx +51 -0
- package/packages/intable/src/plugins/RenderPlugin/index.tsx +81 -0
- package/packages/intable/src/plugins/ResizePlugin.tsx +122 -0
- package/packages/intable/src/plugins/RowGroupPlugin.tsx +122 -0
- package/packages/intable/src/plugins/RowSelectionPlugin.tsx +65 -0
- package/packages/intable/src/plugins/TreePlugin.tsx +212 -0
- package/packages/intable/src/plugins/VirtualScrollPlugin.tsx +190 -0
- package/packages/intable/src/plugins/ZodValidatorPlugin.tsx +61 -0
- package/packages/intable/src/style.scss +244 -0
- package/{dist → packages/intable/src}/theme/antd.scss +14 -5
- package/{dist → packages/intable/src}/theme/element-plus.scss +6 -5
- package/packages/intable/src/tree.ts +13 -0
- package/packages/intable/src/types/auto-imports.d.ts +13 -0
- package/packages/intable/src/utils.ts +122 -0
- package/packages/intable/src/wc.tsx +35 -0
- package/packages/intable/src/web-component.ts +1 -0
- package/packages/react/package.json +31 -0
- package/packages/react/src/index.ts +44 -0
- package/packages/react/src/plugins/antd.ts +94 -0
- package/packages/react/src/style.scss +12 -0
- package/packages/react/src/types/auto-imports.d.ts +10 -0
- package/packages/vue/package.json +34 -0
- package/packages/vue/src/index.ts +63 -0
- package/packages/vue/src/plugins/element-plus.ts +69 -0
- package/packages/vue/src/style.scss +12 -0
- package/packages/vue/src/types/auto-imports.d.ts +10 -0
- package/pnpm-workspace.yaml +2 -0
- package/public/vite.svg +1 -0
- package/scripts/build.js +184 -0
- package/scripts/publish.js +95 -0
- package/src/assets/ClearFormat.svg +3 -0
- package/src/assets/Forms.svg +4 -0
- package/src/assets/MergeCell.svg +4 -0
- package/src/assets/SplitCell.svg +4 -0
- package/src/assets/gap.svg +3 -0
- package/src/assets/loading.svg +12 -0
- package/src/assets/paint.svg +9 -0
- package/src/assets/solid.svg +1 -0
- package/src/demo-vue.ts +54 -0
- package/src/demo.tsx +107 -0
- package/src/index.scss +105 -0
- package/src/styles/index.scss +172 -0
- package/src/types/auto-imports.d.ts +13 -0
- package/stats.html +4949 -0
- package/tsconfig.app.json +34 -0
- package/tsconfig.json +7 -0
- package/tsconfig.node.json +26 -0
- package/vite.config.ts +63 -0
- package/dist/__uno.css +0 -1
- package/dist/chevron-right.js +0 -6
- package/dist/components/Columns.d.ts +0 -3
- package/dist/components/Columns.js +0 -71
- package/dist/components/DocTree.d.ts +0 -4
- package/dist/components/DocTree.js +0 -32
- package/dist/components/Menu.d.ts +0 -1
- package/dist/components/Menu.js +0 -107
- package/dist/components/Popover.d.ts +0 -14
- package/dist/components/Popover.js +0 -41
- package/dist/components/Render.d.ts +0 -4
- package/dist/components/Render.js +0 -20
- package/dist/components/Split.d.ts +0 -15
- package/dist/components/Split.js +0 -76
- package/dist/components/Tree.d.ts +0 -37
- package/dist/components/Tree.js +0 -82
- package/dist/components/utils.d.ts +0 -3
- package/dist/components/utils.js +0 -8
- package/dist/hooks/index.d.ts +0 -40
- package/dist/hooks/index.js +0 -157
- package/dist/hooks/useDir.d.ts +0 -11
- package/dist/hooks/useDir.js +0 -42
- package/dist/hooks/useSelector.d.ts +0 -16
- package/dist/hooks/useSelector.js +0 -35
- package/dist/hooks/useSort.d.ts +0 -18
- package/dist/hooks/useSort.js +0 -83
- package/dist/hooks/useVirtualizer.d.ts +0 -25
- package/dist/hooks/useVirtualizer.js +0 -67
- package/dist/index.d.ts +0 -130
- package/dist/index.js +0 -347
- package/dist/loading.js +0 -6
- package/dist/plugins/CellChangeHighlightPlugin.d.ts +0 -2
- package/dist/plugins/CellChangeHighlightPlugin.js +0 -4
- package/dist/plugins/CellMergePlugin.d.ts +0 -12
- package/dist/plugins/CellMergePlugin.js +0 -2
- package/dist/plugins/CellSelectionPlugin.d.ts +0 -15
- package/dist/plugins/CellSelectionPlugin.js +0 -115
- package/dist/plugins/CommandPlugin.d.ts +0 -14
- package/dist/plugins/CommandPlugin.js +0 -12
- package/dist/plugins/CopyPastePlugin.d.ts +0 -14
- package/dist/plugins/CopyPastePlugin.js +0 -42
- package/dist/plugins/DiffPlugin.d.ts +0 -23
- package/dist/plugins/DiffPlugin.js +0 -56
- package/dist/plugins/DragPlugin.d.ts +0 -14
- package/dist/plugins/DragPlugin.js +0 -47
- package/dist/plugins/EditablePlugin.d.ts +0 -48
- package/dist/plugins/EditablePlugin.js +0 -141
- package/dist/plugins/ExpandPlugin.d.ts +0 -18
- package/dist/plugins/ExpandPlugin.js +0 -50
- package/dist/plugins/HistoryPlugin.d.ts +0 -10
- package/dist/plugins/HistoryPlugin.js +0 -30
- package/dist/plugins/MenuPlugin.d.ts +0 -18
- package/dist/plugins/MenuPlugin.js +0 -107
- package/dist/plugins/RenderPlugin/components.d.ts +0 -5
- package/dist/plugins/RenderPlugin/components.js +0 -87
- package/dist/plugins/RenderPlugin/index.d.ts +0 -30
- package/dist/plugins/RenderPlugin/index.js +0 -49
- package/dist/plugins/ResizePlugin.d.ts +0 -27
- package/dist/plugins/ResizePlugin.js +0 -81
- package/dist/plugins/RowGroupPlugin.d.ts +0 -17
- package/dist/plugins/RowGroupPlugin.js +0 -83
- package/dist/plugins/RowSelectionPlugin.d.ts +0 -20
- package/dist/plugins/RowSelectionPlugin.js +0 -42
- package/dist/plugins/VirtualScrollPlugin.d.ts +0 -15
- package/dist/plugins/VirtualScrollPlugin.js +0 -96
- package/dist/plus.js +0 -6
- package/dist/style.css +0 -3
- package/dist/types/auto-imports.d.js +0 -0
- package/dist/utils.d.ts +0 -30
- package/dist/utils.js +0 -70
- package/dist/wc.d.ts +0 -1
- package/dist/wc.js +0 -21
- package/dist/web-component.d.ts +0 -1
- package/dist/web-component.js +0 -2
- package/dist/x.js +0 -6
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { batch, createEffect, createMemo, createSignal, mapArray, on } from 'solid-js'
|
|
2
|
+
import { combineProps } from '@solid-primitives/props'
|
|
3
|
+
import { createEventListener } from '@solid-primitives/event-listener'
|
|
4
|
+
import { delay, range, remove } from 'es-toolkit'
|
|
5
|
+
import { autoPlacement, computePosition } from '@floating-ui/dom'
|
|
6
|
+
import { type Plugin, type TableStore } from '..'
|
|
7
|
+
import { useMemoAsync, useTinykeys } from '../hooks'
|
|
8
|
+
import { Menu } from '../components/Menu'
|
|
9
|
+
import { tree } from '../utils'
|
|
10
|
+
|
|
11
|
+
declare module '../index' {
|
|
12
|
+
interface TableProps {
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
interface TableStore {
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
interface Plugin {
|
|
19
|
+
menus?: (store: TableStore) => any[]
|
|
20
|
+
}
|
|
21
|
+
interface Commands {
|
|
22
|
+
rowEquals: (a, b) => boolean
|
|
23
|
+
rowIndexOf: (data: any[], row) => number
|
|
24
|
+
rowChange: (row, i?) => void
|
|
25
|
+
//
|
|
26
|
+
addRows: (i: number, rows: any[], before?: boolean) => void
|
|
27
|
+
deleteRows: (i: number[]) => void
|
|
28
|
+
moveRows: (ii: number[], to: number) => void
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const MenuPlugin: Plugin = {
|
|
33
|
+
name: 'menu',
|
|
34
|
+
priority: Infinity,
|
|
35
|
+
store: (store) => ({
|
|
36
|
+
|
|
37
|
+
}),
|
|
38
|
+
rewriteProps: {
|
|
39
|
+
Table: ({ Table }, { store }) => o => {
|
|
40
|
+
const [menuEl, setMenuEl] = createSignal<HTMLElement>()
|
|
41
|
+
|
|
42
|
+
const _menus = mapArray(() => store.plugins || [], plugin => createMemo(() => plugin.menus?.(store)))
|
|
43
|
+
const menus = createMemo(() => _menus().flatMap(e => e() || []))
|
|
44
|
+
|
|
45
|
+
const [pos, setPos] = createSignal<{ x: number; y: number }>()
|
|
46
|
+
function onContextMenu(e: PointerEvent) {
|
|
47
|
+
e.preventDefault()
|
|
48
|
+
setPos({ x: e.x, y: e.y })
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
createEventListener(document, 'pointerdown', e => {
|
|
52
|
+
menuEl()?.contains(e.target as Element) || setPos()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const style = useMemoAsync(() => {
|
|
56
|
+
const mel = menuEl()
|
|
57
|
+
if (!mel) return
|
|
58
|
+
return computePosition({ getBoundingClientRect: () => DOMRect.fromRect(pos()) }, mel, {
|
|
59
|
+
strategy: 'fixed',
|
|
60
|
+
placement: 'top-start',
|
|
61
|
+
middleware: [autoPlacement({ boundary: document.body, alignment: 'start' })]
|
|
62
|
+
})
|
|
63
|
+
.then(({ x, y }) => ({
|
|
64
|
+
position: 'fixed',
|
|
65
|
+
transform: `translate(${x}px, ${y}px)`,
|
|
66
|
+
top: 0,
|
|
67
|
+
left: 0,
|
|
68
|
+
'z-index': 10
|
|
69
|
+
}))
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
o = combineProps({ tabindex: -1, onContextMenu }, o)
|
|
73
|
+
return (
|
|
74
|
+
<Table {...o}>
|
|
75
|
+
{pos() && <Menu ref={setMenuEl} style={style() || 'position: fixed'} items={menus()} onAction={() => setPos()} />}
|
|
76
|
+
{o.children}
|
|
77
|
+
</Table>
|
|
78
|
+
)
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
menus: (store) => [
|
|
82
|
+
{ label: '新增行 ↑', cb: () => store.commands.addRows(store.selected.end[1], [store.props!.newRow(store.selected.end[1])]) },
|
|
83
|
+
{ label: '新增行 ↓', cb: () => store.commands.addRows(store.selected.end[1], [store.props!.newRow(store.selected.end[1])], false) },
|
|
84
|
+
{ label: '删除行', cb: () => store.commands.deleteRows(range(...(e => [e[0], e[1] + 1])([store.selected.start[1], store.selected.end[1]].sort((a, b) => a - b)) as [number, number])) },
|
|
85
|
+
],
|
|
86
|
+
commands: (store) => ({
|
|
87
|
+
rowEquals(a, b) {
|
|
88
|
+
// return a[store.props!.rowKey] == b[store.props!.rowKey]
|
|
89
|
+
return a == b
|
|
90
|
+
},
|
|
91
|
+
rowIndexOf(data, row) {
|
|
92
|
+
return data.findIndex(e => store.commands.rowEquals(e, row))
|
|
93
|
+
},
|
|
94
|
+
rowChange(row, i) {
|
|
95
|
+
const data = [...store.rawProps.data || []]
|
|
96
|
+
i = i != null
|
|
97
|
+
? data.findIndex(ee => ee == store.props!.data[i])
|
|
98
|
+
: store.commands.rowIndexOf(data, row)
|
|
99
|
+
if (i > -1) {
|
|
100
|
+
data[i] = row
|
|
101
|
+
store.props!.onDataChange?.(data)
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
addRows(i, rows, before = true) {
|
|
105
|
+
addRows(store, i, rows, before)
|
|
106
|
+
},
|
|
107
|
+
deleteRows(ii) {
|
|
108
|
+
const { rowKey, data } = store.props!
|
|
109
|
+
const val = [...store.rawProps.data || []]
|
|
110
|
+
// const ids = new Set(data.filter((e, i) => ii.includes(i)).map(e => e[rowKey]))
|
|
111
|
+
// remove(val, e => ids.has(e[rowKey]))
|
|
112
|
+
const ids = new Set(ii.map(i => data[i]))
|
|
113
|
+
remove(val, e => ids.has(e))
|
|
114
|
+
store.props?.onDataChange?.(val)
|
|
115
|
+
},
|
|
116
|
+
moveRows(ii, to) {
|
|
117
|
+
const { data: flatData } = store.props!
|
|
118
|
+
|
|
119
|
+
// Collect the actual row objects, skipping internal/system rows
|
|
120
|
+
const rows = ii.map(i => flatData[i]).filter(r => r && !r?.[store.internal])
|
|
121
|
+
if (!rows.length) return
|
|
122
|
+
|
|
123
|
+
// Destination row — undefined means "append to end"
|
|
124
|
+
const destRow = to >= 0 && to < flatData.length ? flatData[to] : null
|
|
125
|
+
|
|
126
|
+
const { rowKey } = store.props
|
|
127
|
+
const childrenField = store.props.tree?.children || Symbol()
|
|
128
|
+
const movedKeys = new Set(rows.map((r: any) => r[rowKey]))
|
|
129
|
+
|
|
130
|
+
// 1. Remove moved nodes from wherever they live in the nested tree, preserving their original objects (no clone) so children are kept.
|
|
131
|
+
const removed: any[] = []
|
|
132
|
+
function removeNodes(nodes: any[]): any[] {
|
|
133
|
+
return nodes.reduce((acc: any[], node: any) => {
|
|
134
|
+
if (movedKeys.has(node[rowKey])) {
|
|
135
|
+
removed.push(node)
|
|
136
|
+
return acc
|
|
137
|
+
}
|
|
138
|
+
if (Array.isArray(node[childrenField]) && node[childrenField].length) {
|
|
139
|
+
const newChildren = removeNodes(node[childrenField])
|
|
140
|
+
node = newChildren.length !== node[childrenField].length
|
|
141
|
+
? { ...node, [childrenField]: newChildren }
|
|
142
|
+
: node
|
|
143
|
+
}
|
|
144
|
+
acc.push(node)
|
|
145
|
+
return acc
|
|
146
|
+
}, [])
|
|
147
|
+
}
|
|
148
|
+
const rawData = removeNodes([...(store.rawProps.data || [])])
|
|
149
|
+
|
|
150
|
+
// 2. Insert removed nodes just before the destination node in the tree.
|
|
151
|
+
const list = tree.findParent(rawData, e => e[rowKey] === destRow?.[rowKey])?.children ?? rawData
|
|
152
|
+
const index = destRow ? list.findIndex(e => e[rowKey] === destRow[rowKey]) : -1
|
|
153
|
+
if (index > -1) list.splice(index, 0, removed)
|
|
154
|
+
else list.push(...removed)
|
|
155
|
+
|
|
156
|
+
// 3. Update selection to the new flat position (best-effort: use to - ii.length)
|
|
157
|
+
if (store.selected) {
|
|
158
|
+
const newIdx = Math.max(0, to - rows.length)
|
|
159
|
+
store.selected.start = [0, newIdx]
|
|
160
|
+
store.selected.end = [Infinity, newIdx + removed.length - 1]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
store.props?.onDataChange?.(rawData)
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function addRows(store: TableStore, i: number, rows: any[], before: boolean) {
|
|
169
|
+
const { data } = store.props!
|
|
170
|
+
const prev = i => {
|
|
171
|
+
before = false
|
|
172
|
+
while (--i >= 0 && data[i]?.[store.internal]) { }
|
|
173
|
+
return i >= 0 ? data[i] : null
|
|
174
|
+
}
|
|
175
|
+
const next = i => {
|
|
176
|
+
before = true
|
|
177
|
+
while (++i < data.length && data[i]?.[store.internal]) { }
|
|
178
|
+
return i < data.length ? data[i] : null
|
|
179
|
+
}
|
|
180
|
+
const anchor = !data[i]?.[store.internal] ? data[i] : before ? prev(i) || next(i) : next(i) || prev(i)
|
|
181
|
+
if (anchor) {
|
|
182
|
+
batch(() => {
|
|
183
|
+
i = store.commands.rowIndexOf(data, anchor)
|
|
184
|
+
if (!store.selected) return
|
|
185
|
+
store.selected.start = [0, i + (before ? 0 : 1)]
|
|
186
|
+
store.selected.end = [Infinity, i + rows.length - 1 + (before ? 0 : 1)]
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
; (() => {
|
|
190
|
+
const data = [...store.rawProps.data || []]
|
|
191
|
+
const i = anchor ? store.commands.rowIndexOf(data, anchor) + (before ? 0 : 1) : data.length
|
|
192
|
+
data.splice(i, 0, ...rows)
|
|
193
|
+
store.props?.onDataChange?.(data)
|
|
194
|
+
})()
|
|
195
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { For } from 'solid-js'
|
|
2
|
+
import { component } from 'undestructure-macros'
|
|
3
|
+
import { combineProps } from '@solid-primitives/props'
|
|
4
|
+
|
|
5
|
+
export const Checkbox = component(({ value, onChange, ...props }) => {
|
|
6
|
+
props = combineProps({ get class() { return `you-checkbox ${value && 'checked'}` } }, props)
|
|
7
|
+
return (
|
|
8
|
+
<input checked={value || false} onChange={(e) => onChange?.(e.currentTarget.checked)} type="checkbox" {...props} />
|
|
9
|
+
)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export const Files = component(({ ...props }) => {
|
|
13
|
+
return (
|
|
14
|
+
<Tags {...props} />
|
|
15
|
+
)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
export const Tags = component(({ value, children, disabled, onChange, onAdd, ...props }) => {
|
|
19
|
+
props = combineProps({ class: 'flex flex-wrap items-center gap-2 h-full' }, props)
|
|
20
|
+
const toarr = v => Array.isArray(v) ? v : (v != null ? [v] : [])
|
|
21
|
+
return (
|
|
22
|
+
<div {...props}>
|
|
23
|
+
<For each={toarr(value)}>{e => (
|
|
24
|
+
<Tag style={`background: ${e.color}`} disabled={disabled} onDel={() => onChange(toarr(value).filter(e2 => e2 != e))}>
|
|
25
|
+
{children ? children(e) : (e?.text ?? e?.label ?? e?.name ?? e)}
|
|
26
|
+
</Tag>
|
|
27
|
+
)}</For>
|
|
28
|
+
{!disabled && <Tag disabled onClick={onAdd}><ILucidePlus /></Tag>}
|
|
29
|
+
</div>
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export const Tag = component(({ disabled, children, onDel, ...props }) => {
|
|
34
|
+
props = combineProps({ class: 'flex items-center px-2 py-1 rd-sm bg-gray/20 text-3.5 lh-[1]' }, props)
|
|
35
|
+
return (
|
|
36
|
+
<div {...props}>
|
|
37
|
+
{children}
|
|
38
|
+
{!disabled && <ILucideX class='icon-clickable flex-shrink-0 size-4! ml-1 mr--1 op-75' onClick={onDel} />}
|
|
39
|
+
</div>
|
|
40
|
+
)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
// 评估公式
|
|
44
|
+
export const evaluateFormula = (formula: string, data: any) => {
|
|
45
|
+
try {
|
|
46
|
+
const ctx = { data }
|
|
47
|
+
return (new Function(...Object.keys(ctx), `return ` + formula))(...Object.values(ctx))
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return '公式错误'
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { mergeProps, type JSX } from 'solid-js'
|
|
2
|
+
import { component } from 'undestructure-macros'
|
|
3
|
+
import { type Plugin, type TD } from '../..'
|
|
4
|
+
import { Checkbox, Files } from './components'
|
|
5
|
+
import { resolveOptions } from '../../utils'
|
|
6
|
+
import { renderComponent, solidComponent } from '../../components/utils'
|
|
7
|
+
|
|
8
|
+
declare module '../../index' {
|
|
9
|
+
interface TableProps {
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
interface TableColumn {
|
|
13
|
+
// render?: string | Render
|
|
14
|
+
render?: Render
|
|
15
|
+
enum?: Record<string, any> | { label?: string; value: any }[]
|
|
16
|
+
}
|
|
17
|
+
interface TableStore {
|
|
18
|
+
renders: { [key: string]: Render }
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type RenderProps = Parameters<TD>[0] & { onChange: (v) => void }
|
|
23
|
+
export type Render = (props: RenderProps) => JSX.Element | any
|
|
24
|
+
|
|
25
|
+
export const RenderPlugin: Plugin = {
|
|
26
|
+
name: 'render',
|
|
27
|
+
priority: -Infinity,
|
|
28
|
+
store: () => ({
|
|
29
|
+
renders: { ...renders }
|
|
30
|
+
}),
|
|
31
|
+
rewriteProps: {
|
|
32
|
+
Td: ({ Td }, { store }) => o => {
|
|
33
|
+
return (
|
|
34
|
+
<Td {...o}>
|
|
35
|
+
{(() => {
|
|
36
|
+
let Comp = (e => typeof e == 'string' ? store.renders[e] : e)(o.col.render) || text
|
|
37
|
+
return renderComponent(Comp, mergeProps(o, { onChange: v => store.commands.rowChange({ ...o.data, [o.col.id]: v }, o.y) }), store.props!.renderer!)
|
|
38
|
+
})()}
|
|
39
|
+
</Td>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const text: Render = component(({ data, col, onChange }) => {
|
|
46
|
+
return <>{
|
|
47
|
+
(v =>
|
|
48
|
+
col.enum ? resolveOptions(col.enum).find(e => e.value == v)?.label ?? v : v
|
|
49
|
+
)(data[col.id])
|
|
50
|
+
}</>
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const number = text
|
|
54
|
+
|
|
55
|
+
const date = text
|
|
56
|
+
|
|
57
|
+
const checkbox: Render = component(({ data, col, onChange }) => {
|
|
58
|
+
return (
|
|
59
|
+
<div class='flex items-center h-full'>
|
|
60
|
+
<Checkbox class='' value={data[col.id]} onChange={onChange} />
|
|
61
|
+
</div>
|
|
62
|
+
)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const file: Render = component(({ data, col, onChange }) => {
|
|
66
|
+
return (
|
|
67
|
+
<Files value={data[col.id]} onChange={onChange} disabled />
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
export const renders = {
|
|
72
|
+
text,
|
|
73
|
+
number,
|
|
74
|
+
date,
|
|
75
|
+
checkbox,
|
|
76
|
+
file
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (const k in renders) {
|
|
80
|
+
renders[k] = solidComponent(renders[k])
|
|
81
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { batch, untrack, useContext } from 'solid-js'
|
|
2
|
+
import { combineProps } from '@solid-primitives/props'
|
|
3
|
+
import { clamp } from 'es-toolkit'
|
|
4
|
+
import { defaultsDeep } from 'es-toolkit/compat'
|
|
5
|
+
import { usePointerDrag } from '../hooks'
|
|
6
|
+
|
|
7
|
+
import { Ctx, type Plugin, type TableColumn, type TDProps, type THProps } from "../index"
|
|
8
|
+
import { log, unFn } from '../utils'
|
|
9
|
+
import { createEventListener } from '@solid-primitives/event-listener'
|
|
10
|
+
import { reconcile } from 'solid-js/store'
|
|
11
|
+
|
|
12
|
+
declare module '../index' {
|
|
13
|
+
interface TableProps {
|
|
14
|
+
resizable?: {
|
|
15
|
+
col?: Partial<{ enable: boolean; min: number; max: number }>
|
|
16
|
+
row?: Partial<{ enable: boolean; min: number; max: number }>
|
|
17
|
+
}
|
|
18
|
+
onColumnsChange?: (columns: TableColumn[]) => void
|
|
19
|
+
}
|
|
20
|
+
interface TableColumn {
|
|
21
|
+
resizable?: boolean
|
|
22
|
+
onWidthChange?: (width: number) => void
|
|
23
|
+
}
|
|
24
|
+
interface TableStore {
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
interface Commands {
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const COL = Symbol('col_size')
|
|
33
|
+
const ROW = Symbol('row_size')
|
|
34
|
+
|
|
35
|
+
const ColHandle = (o: THProps) => {
|
|
36
|
+
const { props, store } = useContext(Ctx)
|
|
37
|
+
let el!: HTMLDivElement
|
|
38
|
+
usePointerDrag(() => el, {
|
|
39
|
+
start(e, move, end) {
|
|
40
|
+
e.stopPropagation()
|
|
41
|
+
const i = o.x
|
|
42
|
+
const { min, max } = props.resizable!.col
|
|
43
|
+
const th = el.parentElement as HTMLTableColElement
|
|
44
|
+
const sw = th.offsetWidth
|
|
45
|
+
move((e, { ox }) => store[COL][o.x] = clamp(sw + ox, min, max))
|
|
46
|
+
end(() => {
|
|
47
|
+
const col = props.columns[i]
|
|
48
|
+
const cols = [...store.rawProps.columns || []]
|
|
49
|
+
const index = cols.indexOf(col[store.raw] ?? col)
|
|
50
|
+
if (index > -1) {
|
|
51
|
+
cols[index] = { ...cols[index], width: th.offsetWidth }
|
|
52
|
+
props.onColumnsChange?.(cols)
|
|
53
|
+
}
|
|
54
|
+
col.onWidthChange?.(th.offsetWidth)
|
|
55
|
+
})
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
return <div ref={el} class={`in-cell__resize-handle absolute top-0 right-0 flex justify-center w-10px! ${o.x == props.columns.length - 1 ? 'justify-end!' : 'w-10px! translate-x-1/2'} after:w-1 cursor-w-resize z-1`} />
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const RowHandle = (o: TDProps) => {
|
|
62
|
+
const { props, store } = useContext(Ctx)
|
|
63
|
+
let el!: HTMLDivElement
|
|
64
|
+
usePointerDrag(() => el, {
|
|
65
|
+
start(e, move, end) {
|
|
66
|
+
e.stopPropagation()
|
|
67
|
+
const i = o.y
|
|
68
|
+
const { min, max } = props.resizable!.row
|
|
69
|
+
const th = el.parentElement as HTMLTableColElement
|
|
70
|
+
const sh = th.offsetHeight
|
|
71
|
+
move((e, { oy }) => store[ROW][o.y] = clamp(sh + oy, min, max))
|
|
72
|
+
end(() => {
|
|
73
|
+
const row = props.data[i]
|
|
74
|
+
const data = [...store.rawProps.data || []]
|
|
75
|
+
const index = data.indexOf(row[store.raw] ?? row)
|
|
76
|
+
if (index > -1) {
|
|
77
|
+
// todo
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
createEventListener(() => el, 'dblclick', () => o.data[COL]= void 0)
|
|
83
|
+
return <div ref={el} class={`in-cell__resize-handle absolute bottom-0 left-0 flex flex-col justify-center h-1! ${o.y == props.data.length - 1 ? 'justify-end!' : ''} after:h-1 cursor-s-resize z-1`} />
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const ResizePlugin: Plugin = {
|
|
87
|
+
name: 'resize',
|
|
88
|
+
priority: -Infinity,
|
|
89
|
+
store: () => ({
|
|
90
|
+
[COL]: [],
|
|
91
|
+
[ROW]: []
|
|
92
|
+
}),
|
|
93
|
+
rewriteProps: {
|
|
94
|
+
resizable: ({ resizable }) => defaultsDeep(resizable, {
|
|
95
|
+
col: { enable: true, min: 45, max: 800 },
|
|
96
|
+
row: { enable: false, min: 20, max: 400 }
|
|
97
|
+
}),
|
|
98
|
+
columns: ({ columns }, { store }) => (
|
|
99
|
+
columns = columns.map((e, i) => ({ ...e, [store.raw]: e[store.raw] ?? e })),
|
|
100
|
+
columns = columns.map(e => e.resizable === void 0 ? { ...e, resizable: store.props?.resizable?.col.enable, [store.raw]: e[store.raw] ?? e } : e),
|
|
101
|
+
columns = columns.map((e, i) => store[COL][i] ? { ...e, width: store[COL][i], [store.raw]: e[store.raw] ?? e } : e),
|
|
102
|
+
untrack(() => batch(() => reconcile(columns, { key: store.raw })(store.__resize__cols ??= [])))
|
|
103
|
+
),
|
|
104
|
+
Th: ({ Th }, { store }) => o => {
|
|
105
|
+
o = combineProps({ class: 'relative' }, o)
|
|
106
|
+
return <Th {...o}>
|
|
107
|
+
{o.children}
|
|
108
|
+
{o.col.resizable && <ColHandle {...o} />}
|
|
109
|
+
</Th>
|
|
110
|
+
},
|
|
111
|
+
Td: ({ Td }, { store }) => !store.props?.resizable?.row.enable ? Td : o => {
|
|
112
|
+
o = combineProps({ class: 'relative' }, o)
|
|
113
|
+
return <Td {...o}>
|
|
114
|
+
{o.children}
|
|
115
|
+
{o.x == 0 && store.props?.resizable?.row.enable && <RowHandle {...o} />}
|
|
116
|
+
</Td>
|
|
117
|
+
},
|
|
118
|
+
cellStyle: ({ cellStyle }, { store }) => o => {
|
|
119
|
+
return `${unFn(cellStyle, o)};` + (store[ROW][o.y] ? `height: ${store[ROW][o.y]}px` : '')
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { groupBy, zipObject } from 'es-toolkit'
|
|
2
|
+
import { findLast } from 'es-toolkit/compat'
|
|
3
|
+
import { Ctx, type Plugin } from '..'
|
|
4
|
+
import { batch, createMemo, useContext } from 'solid-js'
|
|
5
|
+
import type { TableStore } from '..'
|
|
6
|
+
|
|
7
|
+
declare module '../index' {
|
|
8
|
+
interface TableProps {
|
|
9
|
+
rowGroup?: {
|
|
10
|
+
fields?: string[]
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
interface TableStore {
|
|
14
|
+
rowGroup: {
|
|
15
|
+
/** Plain-object map used as Set<string>; property access is tracked by createMutable */
|
|
16
|
+
expandKeys: Record<string, true | undefined>
|
|
17
|
+
isExpand: (data) => boolean
|
|
18
|
+
expand: (data, r?) => void
|
|
19
|
+
toggleExpand: (data) => void
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Serialize a group path to a plain string key for O(1) Set-style lookup */
|
|
25
|
+
const pathKey = (path: any[]): string => path.join('\0')
|
|
26
|
+
|
|
27
|
+
export const RowGroupPlugin: Plugin = {
|
|
28
|
+
priority: -Infinity,
|
|
29
|
+
store: (store) => ({
|
|
30
|
+
rowGroup: {
|
|
31
|
+
// Plain object acts as a reactive Map<string, true> inside createMutable.
|
|
32
|
+
// Property reads are tracked by SolidJS; O(1) vs previous O(n) isEqual scan.
|
|
33
|
+
expandKeys: {} as Record<string, true | undefined>,
|
|
34
|
+
isExpand: data => !!store.rowGroup.expandKeys[pathKey(data[GROUP].path)],
|
|
35
|
+
expand: (data, r) => batch(() =>
|
|
36
|
+
r
|
|
37
|
+
? data[GROUP].path2.forEach(e => { store.rowGroup.expandKeys[pathKey(e[GROUP].path)] = true })
|
|
38
|
+
: (store.rowGroup.expandKeys[pathKey(data[GROUP].path)] = true)
|
|
39
|
+
),
|
|
40
|
+
toggleExpand: data => {
|
|
41
|
+
const k = pathKey(data[GROUP].path)
|
|
42
|
+
store.rowGroup.expandKeys[k] ? delete store.rowGroup.expandKeys[k] : (store.rowGroup.expandKeys[k] = true)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}),
|
|
46
|
+
commands: (store, { addRows }) => ({
|
|
47
|
+
addRows(i, rows, before) {
|
|
48
|
+
const { data, rowGroup, rowKey } = store.props!
|
|
49
|
+
if (rowGroup?.fields?.length) {
|
|
50
|
+
const group = findLast(data, e => e[GROUP], i)
|
|
51
|
+
if (group) {
|
|
52
|
+
if (data[i][GROUP]) {
|
|
53
|
+
const leaf = (function r(group) { return group[GROUP]?.children[0]?.[GROUP] ? r(group[GROUP].children[0]) : group })(group)
|
|
54
|
+
store.rowGroup.expand(leaf, true)
|
|
55
|
+
const anchor = leaf[GROUP].children[0]
|
|
56
|
+
i = store.props!.data.indexOf(anchor)
|
|
57
|
+
before = true
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
addRows?.(i, rows, before)
|
|
61
|
+
} else {
|
|
62
|
+
addRows?.(...arguments)
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
rewriteProps: {
|
|
67
|
+
data: ({ data }, { store }) => (
|
|
68
|
+
store.props?.rowGroup?.fields?.length
|
|
69
|
+
? expandData(data, store)
|
|
70
|
+
: data
|
|
71
|
+
),
|
|
72
|
+
newRow: ({ newRow }, { store }) => function (i) {
|
|
73
|
+
const row = newRow(...arguments)
|
|
74
|
+
const { data, rowGroup } = store.props!
|
|
75
|
+
if (rowGroup?.fields?.length) {
|
|
76
|
+
const group = findLast(data, e => e[GROUP], i)
|
|
77
|
+
if (group) {
|
|
78
|
+
const leaf = (function r(group) { return group[GROUP]?.children[0]?.[GROUP] ? r(group[GROUP].children[0]) : group })(group)
|
|
79
|
+
const extra = zipObject(rowGroup!.fields!, leaf[GROUP].path)
|
|
80
|
+
Object.assign(row, extra)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return row
|
|
84
|
+
},
|
|
85
|
+
Td: ({ Td }, { store }) => o => {
|
|
86
|
+
if (!o.data?.[GROUP]) return <Td {...o} />
|
|
87
|
+
|
|
88
|
+
const { props } = useContext(Ctx)
|
|
89
|
+
const show = createMemo(() => store.rowGroup.isExpand(o.data))
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<Td {...o}>
|
|
93
|
+
{props.columns?.findIndex(e => !e[store.internal]) == o.x ? (
|
|
94
|
+
<div class='flex items-center' style={`padding-left: ${(o.data[GROUP].path.length - 1) * 16}px`} onDblClick={() => store.rowGroup.toggleExpand(o.data)}>
|
|
95
|
+
<ILucideChevronRight class='icon-clickable mr-2' style={`transform: rotate(${show() ? 90 : 0}deg); opacity: .6`} onClick={() => store.rowGroup.toggleExpand(o.data)} />
|
|
96
|
+
{o.data[GROUP].value}
|
|
97
|
+
</div>
|
|
98
|
+
) : o.children}
|
|
99
|
+
</Td>
|
|
100
|
+
)
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const GROUP = Symbol('row-group')
|
|
106
|
+
|
|
107
|
+
const expandData = (data, store: TableStore, path2 = [] as any[]) => {
|
|
108
|
+
const fields = store.props!.rowGroup!.fields!
|
|
109
|
+
const col = store.props!.columns.find(e => !e[store.internal])
|
|
110
|
+
if (!col) return data
|
|
111
|
+
if (fields.length == path2.length) return data
|
|
112
|
+
const path = path2[path2.length - 1]?.[GROUP].path || []
|
|
113
|
+
const obj = groupBy(data, e => e[fields[path.length]])
|
|
114
|
+
return Object.keys(obj).flatMap(k => {
|
|
115
|
+
const group = { [col.id]: k, [store.internal]: 1 } as any
|
|
116
|
+
const ps = [...path2, group]
|
|
117
|
+
group[GROUP] = { path: [...path, k], value: k, path2: ps }
|
|
118
|
+
group[GROUP].children = expandData(obj[k], store, ps)
|
|
119
|
+
const arr = store.rowGroup.isExpand(group) ? group[GROUP].children : []
|
|
120
|
+
return [group, ...arr]
|
|
121
|
+
})
|
|
122
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { createEffect, createMemo, createRenderEffect, mergeProps, on } from 'solid-js'
|
|
2
|
+
import { createMutable, reconcile } from 'solid-js/store'
|
|
3
|
+
import { keyBy } from 'es-toolkit'
|
|
4
|
+
import { defaultsDeep, isEqual } from 'es-toolkit/compat'
|
|
5
|
+
import { type Commands, type Plugin, type TableColumn, type TableProps } from '..'
|
|
6
|
+
import { Checkbox } from './RenderPlugin/components'
|
|
7
|
+
import { solidComponent } from '../components/utils'
|
|
8
|
+
import { useSelector } from '../hooks/useSelector'
|
|
9
|
+
|
|
10
|
+
declare module '../index' {
|
|
11
|
+
interface TableProps {
|
|
12
|
+
rowSelection?: {
|
|
13
|
+
enable?: boolean
|
|
14
|
+
multiple?: boolean
|
|
15
|
+
value?: any
|
|
16
|
+
selectable?: (row) => boolean
|
|
17
|
+
onChange?: (selected) => void
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
interface TableStore {
|
|
21
|
+
rowSelectionCol: TableColumn
|
|
22
|
+
}
|
|
23
|
+
interface Commands {
|
|
24
|
+
rowSelector: ReturnType<typeof useSelector>
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const RowSelectionPlugin: Plugin = {
|
|
29
|
+
name: 'row-selection',
|
|
30
|
+
priority: -Infinity,
|
|
31
|
+
store: (store) => ({
|
|
32
|
+
rowSelectionCol: {
|
|
33
|
+
[store.internal]: 1,
|
|
34
|
+
id: Symbol('row-selection'),
|
|
35
|
+
fixed: 'left',
|
|
36
|
+
class: 'row-selection',
|
|
37
|
+
width: 45,
|
|
38
|
+
resizable: false,
|
|
39
|
+
render: solidComponent((o) => (
|
|
40
|
+
<label>
|
|
41
|
+
<Checkbox
|
|
42
|
+
style='position: absolute'
|
|
43
|
+
value={store.commands.rowSelector.has(o.data)}
|
|
44
|
+
onChange={v => v ? store.commands.rowSelector.add(o.data) : store.commands.rowSelector.del(o.data)}
|
|
45
|
+
disabled={!store.props?.rowSelection?.selectable?.(o.data)}
|
|
46
|
+
/>
|
|
47
|
+
</label>
|
|
48
|
+
))
|
|
49
|
+
} as TableColumn,
|
|
50
|
+
}),
|
|
51
|
+
commands: (store) => ({
|
|
52
|
+
rowSelector: useSelector(mergeProps(() => ({ ...store.props?.rowSelection })))
|
|
53
|
+
}),
|
|
54
|
+
rewriteProps: {
|
|
55
|
+
rowSelection: ({ rowSelection }) => defaultsDeep(rowSelection, {
|
|
56
|
+
enable: false,
|
|
57
|
+
multiple: false,
|
|
58
|
+
selectable: () => true,
|
|
59
|
+
} as TableProps['rowSelection']),
|
|
60
|
+
|
|
61
|
+
columns: ({ columns }, { store }) => store.props?.rowSelection?.enable
|
|
62
|
+
? [store.rowSelectionCol, ...columns]
|
|
63
|
+
: columns
|
|
64
|
+
}
|
|
65
|
+
}
|