@isoftdata/svelte-table 2.9.2 → 2.9.4
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 +387 -387
- package/dist/DraggableRows.svelte +285 -276
- package/dist/Pagination.svelte +303 -303
- package/dist/Table.svelte +1074 -1074
- package/dist/Td.svelte +156 -156
- package/dist/TreeRow.svelte +170 -170
- package/package.json +2 -2
package/dist/Td.svelte
CHANGED
|
@@ -1,156 +1,156 @@
|
|
|
1
|
-
<script
|
|
2
|
-
lang="ts"
|
|
3
|
-
generics="R extends UuidRowProps = any"
|
|
4
|
-
>
|
|
5
|
-
import type { ColumnInfo, RowProperties, UuidRowProps } from './'
|
|
6
|
-
import type { ColumnInfoRunicStore } from './column-info-store.svelte'
|
|
7
|
-
import type { HTMLTdAttributes, ClassValue } from 'svelte/elements'
|
|
8
|
-
import type { Writable } from 'svelte/store'
|
|
9
|
-
import { getContext, onMount, type Snippet } from 'svelte'
|
|
10
|
-
|
|
11
|
-
// Get the columnInfo store from the Table component
|
|
12
|
-
const columnInfo = getContext<ColumnInfoRunicStore<UuidRowProps>>('columnInfo')
|
|
13
|
-
const columnResizingEnabled = getContext<Writable<boolean>>('columnResizingEnabled')
|
|
14
|
-
const bs5 = getContext<boolean>('bs5')
|
|
15
|
-
|
|
16
|
-
interface Props extends Omit<HTMLTdAttributes, 'align'> {
|
|
17
|
-
class?: ClassValue
|
|
18
|
-
property: ColumnInfo<R>['property']
|
|
19
|
-
/** Direction to align the column's contents
|
|
20
|
-
*
|
|
21
|
-
* `start` and `end` are only available in Bootstrap 5+
|
|
22
|
-
*
|
|
23
|
-
* `left` and `right`should only be used while you transition to Boostrap 5+
|
|
24
|
-
*/
|
|
25
|
-
align?: 'start' | 'left' | 'center' | 'right' | 'end'
|
|
26
|
-
/** If enabled, pressing enter while focused in this Td will focus the next non-disabled input/select/textarea in the column. Holding shift will go up instead of down.
|
|
27
|
-
*
|
|
28
|
-
* Can also be a callback that is fired when the enter key is used to move between rows.
|
|
29
|
-
*/
|
|
30
|
-
enterGoesDown?: boolean | ((ctx: { event: KeyboardEvent; tr: HTMLTableRowElement }) => void)
|
|
31
|
-
/** If enabled, keypress (enter only) and click events on the Td's contents will have `stopPropagation`/`preventDefault` called on them. */
|
|
32
|
-
stopPropagation?: boolean
|
|
33
|
-
tagName?: 'TD' | 'TH'
|
|
34
|
-
children?: Snippet
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let {
|
|
38
|
-
//
|
|
39
|
-
class: classNames = '',
|
|
40
|
-
property,
|
|
41
|
-
align = $bindable(columnInfo.current[property]?.align),
|
|
42
|
-
enterGoesDown = false,
|
|
43
|
-
stopPropagation = false,
|
|
44
|
-
tagName = 'TD',
|
|
45
|
-
children,
|
|
46
|
-
...rest
|
|
47
|
-
}: Props = $props()
|
|
48
|
-
|
|
49
|
-
const thisColumnInfo = $derived(columnInfo.current[property])
|
|
50
|
-
const alignClass = $derived.by(() => {
|
|
51
|
-
let alignment = align || thisColumnInfo?.align || (thisColumnInfo?.numeric ? 'right' : undefined)
|
|
52
|
-
if (bs5 && alignment === 'right') {
|
|
53
|
-
alignment = 'end'
|
|
54
|
-
} else if (bs5 && alignment === 'left') {
|
|
55
|
-
alignment = 'start'
|
|
56
|
-
}
|
|
57
|
-
return alignment ? (`text-${alignment}` as const) : ''
|
|
58
|
-
})
|
|
59
|
-
const visible = $derived(thisColumnInfo?.visible ?? true)
|
|
60
|
-
|
|
61
|
-
/** Handle `stopPropagation` and `enterGoesDown` props */
|
|
62
|
-
function onkeypress(event: KeyboardEvent) {
|
|
63
|
-
if (event.key === 'Enter') {
|
|
64
|
-
event.stopPropagation()
|
|
65
|
-
event.preventDefault()
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (event.key === 'Enter' && enterGoesDown) {
|
|
69
|
-
const target = event.target
|
|
70
|
-
if (!(target instanceof HTMLElement)) {
|
|
71
|
-
return
|
|
72
|
-
}
|
|
73
|
-
const tr = target?.closest('tr') ?? null
|
|
74
|
-
const td = target?.closest('td') ?? null
|
|
75
|
-
const tdIndex = td ? Array.from(tr?.children ?? []).indexOf(td) : -1
|
|
76
|
-
const allTrs = Array.from(tr?.parentElement?.children ?? [])
|
|
77
|
-
|
|
78
|
-
// if they're holding shift, reverse the tr list
|
|
79
|
-
if (event.shiftKey) {
|
|
80
|
-
allTrs.reverse()
|
|
81
|
-
}
|
|
82
|
-
// Find the next Td in the column with a non-disabled input, select, or textarea
|
|
83
|
-
const lastTrIndex = tr ? allTrs.indexOf(tr) : -1
|
|
84
|
-
const newTrIndex = allTrs.findIndex(
|
|
85
|
-
(tr, index) =>
|
|
86
|
-
index > lastTrIndex &&
|
|
87
|
-
tr.children[tdIndex]?.querySelector('input:enabled, select:enabled, textarea:enabled') &&
|
|
88
|
-
tr instanceof HTMLTableRowElement &&
|
|
89
|
-
tr.offsetParent !== null,
|
|
90
|
-
)
|
|
91
|
-
const theTd = allTrs[newTrIndex]?.children[tdIndex] ?? null
|
|
92
|
-
theTd?.querySelector<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>('input,select,textarea')?.focus()
|
|
93
|
-
if (typeof enterGoesDown === 'function') {
|
|
94
|
-
enterGoesDown({
|
|
95
|
-
tr: allTrs[newTrIndex] as HTMLTableRowElement,
|
|
96
|
-
event,
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
onMount(() => {
|
|
103
|
-
if (!(property in columnInfo.current)) {
|
|
104
|
-
console.warn(
|
|
105
|
-
`Column "${property}" does not exist in its parent Table component's list of columns.\r\n\r\nPlease check that this Td's "property" prop matches the "property" prop of one of the columns in the parent Table component.`,
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
</script>
|
|
110
|
-
|
|
111
|
-
{#if visible}
|
|
112
|
-
<svelte:element
|
|
113
|
-
this={tagName}
|
|
114
|
-
class={[classNames, alignClass]}
|
|
115
|
-
{...rest}
|
|
116
|
-
style:font-variant-numeric={thisColumnInfo?.numeric ? 'tabular-nums' : undefined}
|
|
117
|
-
style:overflow={$columnResizingEnabled ? 'hidden' : undefined}
|
|
118
|
-
style:text-overflow={$columnResizingEnabled ? 'ellipsis' : undefined}
|
|
119
|
-
>
|
|
120
|
-
<!--
|
|
121
|
-
Don't listen to events that the user hasn't turned on the features for.
|
|
122
|
-
Also, put them on a span so they can still click in the td to click the row but not the td's child content
|
|
123
|
-
-->
|
|
124
|
-
{#if stopPropagation || enterGoesDown}
|
|
125
|
-
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
126
|
-
<span
|
|
127
|
-
onclick={stopPropagation ? e => e.stopPropagation() : undefined}
|
|
128
|
-
{onkeypress}
|
|
129
|
-
>
|
|
130
|
-
{@render children?.()}
|
|
131
|
-
</span>
|
|
132
|
-
{:else}
|
|
133
|
-
{@render children?.()}
|
|
134
|
-
{/if}
|
|
135
|
-
</svelte:element>
|
|
136
|
-
{/if}
|
|
137
|
-
|
|
138
|
-
<style>
|
|
139
|
-
td.text-right :global(input),
|
|
140
|
-
td.text-end :global(input) {
|
|
141
|
-
text-align: right;
|
|
142
|
-
}
|
|
143
|
-
td.text-right :global(select),
|
|
144
|
-
td.text-end :global(select) {
|
|
145
|
-
text-align: right;
|
|
146
|
-
}
|
|
147
|
-
td.text-right :global(textarea),
|
|
148
|
-
td.text-end :global(textarea) {
|
|
149
|
-
text-align: right;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/* BS5 uses floats for checkbox alignment, which overrides the text align classes, so undo that here. */
|
|
153
|
-
td:where(.text-start, .text-center, .text-end) :global(.form-check-input) {
|
|
154
|
-
float: unset;
|
|
155
|
-
}
|
|
156
|
-
</style>
|
|
1
|
+
<script
|
|
2
|
+
lang="ts"
|
|
3
|
+
generics="R extends UuidRowProps = any"
|
|
4
|
+
>
|
|
5
|
+
import type { ColumnInfo, RowProperties, UuidRowProps } from './'
|
|
6
|
+
import type { ColumnInfoRunicStore } from './column-info-store.svelte'
|
|
7
|
+
import type { HTMLTdAttributes, ClassValue } from 'svelte/elements'
|
|
8
|
+
import type { Writable } from 'svelte/store'
|
|
9
|
+
import { getContext, onMount, type Snippet } from 'svelte'
|
|
10
|
+
|
|
11
|
+
// Get the columnInfo store from the Table component
|
|
12
|
+
const columnInfo = getContext<ColumnInfoRunicStore<UuidRowProps>>('columnInfo')
|
|
13
|
+
const columnResizingEnabled = getContext<Writable<boolean>>('columnResizingEnabled')
|
|
14
|
+
const bs5 = getContext<boolean>('bs5')
|
|
15
|
+
|
|
16
|
+
interface Props extends Omit<HTMLTdAttributes, 'align'> {
|
|
17
|
+
class?: ClassValue
|
|
18
|
+
property: ColumnInfo<R>['property']
|
|
19
|
+
/** Direction to align the column's contents
|
|
20
|
+
*
|
|
21
|
+
* `start` and `end` are only available in Bootstrap 5+
|
|
22
|
+
*
|
|
23
|
+
* `left` and `right`should only be used while you transition to Boostrap 5+
|
|
24
|
+
*/
|
|
25
|
+
align?: 'start' | 'left' | 'center' | 'right' | 'end'
|
|
26
|
+
/** If enabled, pressing enter while focused in this Td will focus the next non-disabled input/select/textarea in the column. Holding shift will go up instead of down.
|
|
27
|
+
*
|
|
28
|
+
* Can also be a callback that is fired when the enter key is used to move between rows.
|
|
29
|
+
*/
|
|
30
|
+
enterGoesDown?: boolean | ((ctx: { event: KeyboardEvent; tr: HTMLTableRowElement }) => void)
|
|
31
|
+
/** If enabled, keypress (enter only) and click events on the Td's contents will have `stopPropagation`/`preventDefault` called on them. */
|
|
32
|
+
stopPropagation?: boolean
|
|
33
|
+
tagName?: 'TD' | 'TH'
|
|
34
|
+
children?: Snippet
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let {
|
|
38
|
+
//
|
|
39
|
+
class: classNames = '',
|
|
40
|
+
property,
|
|
41
|
+
align = $bindable(columnInfo.current[property]?.align),
|
|
42
|
+
enterGoesDown = false,
|
|
43
|
+
stopPropagation = false,
|
|
44
|
+
tagName = 'TD',
|
|
45
|
+
children,
|
|
46
|
+
...rest
|
|
47
|
+
}: Props = $props()
|
|
48
|
+
|
|
49
|
+
const thisColumnInfo = $derived(columnInfo.current[property])
|
|
50
|
+
const alignClass = $derived.by(() => {
|
|
51
|
+
let alignment = align || thisColumnInfo?.align || (thisColumnInfo?.numeric ? 'right' : undefined)
|
|
52
|
+
if (bs5 && alignment === 'right') {
|
|
53
|
+
alignment = 'end'
|
|
54
|
+
} else if (bs5 && alignment === 'left') {
|
|
55
|
+
alignment = 'start'
|
|
56
|
+
}
|
|
57
|
+
return alignment ? (`text-${alignment}` as const) : ''
|
|
58
|
+
})
|
|
59
|
+
const visible = $derived(thisColumnInfo?.visible ?? true)
|
|
60
|
+
|
|
61
|
+
/** Handle `stopPropagation` and `enterGoesDown` props */
|
|
62
|
+
function onkeypress(event: KeyboardEvent) {
|
|
63
|
+
if (event.key === 'Enter') {
|
|
64
|
+
event.stopPropagation()
|
|
65
|
+
event.preventDefault()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (event.key === 'Enter' && enterGoesDown) {
|
|
69
|
+
const target = event.target
|
|
70
|
+
if (!(target instanceof HTMLElement)) {
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
const tr = target?.closest('tr') ?? null
|
|
74
|
+
const td = target?.closest('td') ?? null
|
|
75
|
+
const tdIndex = td ? Array.from(tr?.children ?? []).indexOf(td) : -1
|
|
76
|
+
const allTrs = Array.from(tr?.parentElement?.children ?? [])
|
|
77
|
+
|
|
78
|
+
// if they're holding shift, reverse the tr list
|
|
79
|
+
if (event.shiftKey) {
|
|
80
|
+
allTrs.reverse()
|
|
81
|
+
}
|
|
82
|
+
// Find the next Td in the column with a non-disabled input, select, or textarea
|
|
83
|
+
const lastTrIndex = tr ? allTrs.indexOf(tr) : -1
|
|
84
|
+
const newTrIndex = allTrs.findIndex(
|
|
85
|
+
(tr, index) =>
|
|
86
|
+
index > lastTrIndex &&
|
|
87
|
+
tr.children[tdIndex]?.querySelector('input:enabled, select:enabled, textarea:enabled') &&
|
|
88
|
+
tr instanceof HTMLTableRowElement &&
|
|
89
|
+
tr.offsetParent !== null,
|
|
90
|
+
)
|
|
91
|
+
const theTd = allTrs[newTrIndex]?.children[tdIndex] ?? null
|
|
92
|
+
theTd?.querySelector<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>('input,select,textarea')?.focus()
|
|
93
|
+
if (typeof enterGoesDown === 'function') {
|
|
94
|
+
enterGoesDown({
|
|
95
|
+
tr: allTrs[newTrIndex] as HTMLTableRowElement,
|
|
96
|
+
event,
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
onMount(() => {
|
|
103
|
+
if (!(property in columnInfo.current)) {
|
|
104
|
+
console.warn(
|
|
105
|
+
`Column "${property}" does not exist in its parent Table component's list of columns.\r\n\r\nPlease check that this Td's "property" prop matches the "property" prop of one of the columns in the parent Table component.`,
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
</script>
|
|
110
|
+
|
|
111
|
+
{#if visible}
|
|
112
|
+
<svelte:element
|
|
113
|
+
this={tagName}
|
|
114
|
+
class={[classNames, alignClass]}
|
|
115
|
+
{...rest}
|
|
116
|
+
style:font-variant-numeric={thisColumnInfo?.numeric ? 'tabular-nums' : undefined}
|
|
117
|
+
style:overflow={$columnResizingEnabled ? 'hidden' : undefined}
|
|
118
|
+
style:text-overflow={$columnResizingEnabled ? 'ellipsis' : undefined}
|
|
119
|
+
>
|
|
120
|
+
<!--
|
|
121
|
+
Don't listen to events that the user hasn't turned on the features for.
|
|
122
|
+
Also, put them on a span so they can still click in the td to click the row but not the td's child content
|
|
123
|
+
-->
|
|
124
|
+
{#if stopPropagation || enterGoesDown}
|
|
125
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
126
|
+
<span
|
|
127
|
+
onclick={stopPropagation ? e => e.stopPropagation() : undefined}
|
|
128
|
+
{onkeypress}
|
|
129
|
+
>
|
|
130
|
+
{@render children?.()}
|
|
131
|
+
</span>
|
|
132
|
+
{:else}
|
|
133
|
+
{@render children?.()}
|
|
134
|
+
{/if}
|
|
135
|
+
</svelte:element>
|
|
136
|
+
{/if}
|
|
137
|
+
|
|
138
|
+
<style>
|
|
139
|
+
td.text-right :global(input),
|
|
140
|
+
td.text-end :global(input) {
|
|
141
|
+
text-align: right;
|
|
142
|
+
}
|
|
143
|
+
td.text-right :global(select),
|
|
144
|
+
td.text-end :global(select) {
|
|
145
|
+
text-align: right;
|
|
146
|
+
}
|
|
147
|
+
td.text-right :global(textarea),
|
|
148
|
+
td.text-end :global(textarea) {
|
|
149
|
+
text-align: right;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* BS5 uses floats for checkbox alignment, which overrides the text align classes, so undo that here. */
|
|
153
|
+
td:where(.text-start, .text-center, .text-end) :global(.form-check-input) {
|
|
154
|
+
float: unset;
|
|
155
|
+
}
|
|
156
|
+
</style>
|
package/dist/TreeRow.svelte
CHANGED
|
@@ -1,170 +1,170 @@
|
|
|
1
|
-
<script
|
|
2
|
-
lang="ts"
|
|
3
|
-
generics="T"
|
|
4
|
-
>
|
|
5
|
-
import type { Snippet } from 'svelte'
|
|
6
|
-
import type { Writable } from 'svelte/store'
|
|
7
|
-
import type { ClassValue } from 'svelte/elements'
|
|
8
|
-
import type { TreeNode, IdKeyType, ParentIdKeyType } from './tree-utility.js'
|
|
9
|
-
import TreeRow from './TreeRow.svelte'
|
|
10
|
-
|
|
11
|
-
type Key = IdKeyType<T>
|
|
12
|
-
type ParentKey = ParentIdKeyType<T>
|
|
13
|
-
type Node = TreeNode<T, Key, ParentKey>
|
|
14
|
-
|
|
15
|
-
import Button from '@isoftdata/svelte-button'
|
|
16
|
-
import Td from './Td.svelte'
|
|
17
|
-
import { getContext, hasContext, onMount } from 'svelte'
|
|
18
|
-
|
|
19
|
-
// 2 rem nest padding -> ml-2
|
|
20
|
-
// 1.5 rem leaf padding -> ml-3
|
|
21
|
-
const NEST_PADDING = 1.5 // rem
|
|
22
|
-
const LEAF_MARGIN_CLASS = 'ml-3'
|
|
23
|
-
|
|
24
|
-
interface Props {
|
|
25
|
-
property: keyof T
|
|
26
|
-
idProp: Key
|
|
27
|
-
parentIdProp: ParentKey
|
|
28
|
-
node: Node
|
|
29
|
-
depth?: number
|
|
30
|
-
showLabel?: boolean
|
|
31
|
-
class?: ClassValue
|
|
32
|
-
// Snippets
|
|
33
|
-
button?: Snippet<[{ node: Node }]>
|
|
34
|
-
first?: Snippet<[{ node: Node }]>
|
|
35
|
-
children?: Snippet<[{ node: Node }]>
|
|
36
|
-
// Callbacks
|
|
37
|
-
rowClick?: (context: { node: Node }) => void
|
|
38
|
-
toggleFold?: (context: { expanded: boolean }) => void
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let {
|
|
42
|
-
//
|
|
43
|
-
property,
|
|
44
|
-
idProp,
|
|
45
|
-
parentIdProp,
|
|
46
|
-
node,
|
|
47
|
-
depth = 0,
|
|
48
|
-
showLabel = true,
|
|
49
|
-
class: className = '',
|
|
50
|
-
button,
|
|
51
|
-
first,
|
|
52
|
-
children,
|
|
53
|
-
rowClick,
|
|
54
|
-
toggleFold,
|
|
55
|
-
}: Props = $props()
|
|
56
|
-
|
|
57
|
-
function pad(depth: number) {
|
|
58
|
-
return depth * NEST_PADDING + 'rem'
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const selectedRowIds = getContext<Writable<(number | string)[]>>('selectedRowIds')
|
|
62
|
-
if (!hasContext('expandedRows')) {
|
|
63
|
-
throw new Error('TreeRow requires that the `tree` prop is set to true on a parent Table component.')
|
|
64
|
-
}
|
|
65
|
-
const expandedRows = getContext<Writable<Record<number | string, boolean>>>('expandedRows')
|
|
66
|
-
|
|
67
|
-
let expanded = $derived($expandedRows[node[idProp] as number | string] ?? false)
|
|
68
|
-
let icon: 'caret-down' | 'caret-right' = $derived(expanded ? 'caret-down' : 'caret-right')
|
|
69
|
-
let buttonPadding = $derived(pad(depth))
|
|
70
|
-
let leafPadding = $derived(pad(depth + 1))
|
|
71
|
-
let childRows = $derived(node ? node.children : [])
|
|
72
|
-
|
|
73
|
-
let selected = $derived($selectedRowIds ? $selectedRowIds.includes(node[idProp] as number | string) : false)
|
|
74
|
-
let stringProperty = $derived(typeof property === 'string' ? property : property.toString())
|
|
75
|
-
|
|
76
|
-
function setExpanded(expanded: boolean) {
|
|
77
|
-
$expandedRows[node[idProp] as number | string] = expanded
|
|
78
|
-
toggleFold?.({ expanded })
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function onFoldClick(event: MouseEvent) {
|
|
82
|
-
event.preventDefault()
|
|
83
|
-
event.stopPropagation()
|
|
84
|
-
setExpanded(!expanded)
|
|
85
|
-
}
|
|
86
|
-
onMount(() => {
|
|
87
|
-
// Run this on mount so on page load, the selected row is expanded.
|
|
88
|
-
function isExpanded(expandedRows: Record<number | string, boolean>, node: Node): boolean {
|
|
89
|
-
if (expandedRows[node[idProp] as number | string]) {
|
|
90
|
-
return true
|
|
91
|
-
}
|
|
92
|
-
if (node.children) {
|
|
93
|
-
return node.children.some(
|
|
94
|
-
child => isExpanded(expandedRows, child) || $selectedRowIds.includes(child[idProp] as number | string),
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
return false
|
|
98
|
-
}
|
|
99
|
-
if (!expanded && node.children) {
|
|
100
|
-
$expandedRows[node[idProp] as number | string] = isExpanded($expandedRows, node)
|
|
101
|
-
}
|
|
102
|
-
})
|
|
103
|
-
</script>
|
|
104
|
-
|
|
105
|
-
<tr
|
|
106
|
-
class={className}
|
|
107
|
-
class:table-primary={selected}
|
|
108
|
-
data-id={node[idProp]}
|
|
109
|
-
data-parentid={node[parentIdProp]}
|
|
110
|
-
onclick={() => rowClick?.({ node })}
|
|
111
|
-
ondblclick={onFoldClick}
|
|
112
|
-
>
|
|
113
|
-
<Td property={stringProperty}>
|
|
114
|
-
{#if childRows.length}
|
|
115
|
-
<Button
|
|
116
|
-
size="xs"
|
|
117
|
-
color="link"
|
|
118
|
-
style="padding-left: {buttonPadding}; box-shadow: none!important; font-size: 1rem; text-wrap: nowrap;"
|
|
119
|
-
icon={{ icon: icon, class: 'fa-xl', fixedWidth: true, prefix: 'fas' }}
|
|
120
|
-
disabled={!childRows.length}
|
|
121
|
-
colorGreyDisabled={false}
|
|
122
|
-
onclick={onFoldClick}
|
|
123
|
-
onkeydown={e => {
|
|
124
|
-
if (e.key === 'ArrowRight') {
|
|
125
|
-
setExpanded(true)
|
|
126
|
-
} else if (e.key === 'ArrowLeft') {
|
|
127
|
-
setExpanded(false)
|
|
128
|
-
}
|
|
129
|
-
}}
|
|
130
|
-
>{@render button?.({ node })}
|
|
131
|
-
{#if showLabel}
|
|
132
|
-
{node[property]}
|
|
133
|
-
{/if}
|
|
134
|
-
</Button>
|
|
135
|
-
{:else if showLabel}
|
|
136
|
-
<span
|
|
137
|
-
class={LEAF_MARGIN_CLASS}
|
|
138
|
-
style="padding-left: {leafPadding}; text-wrap: nowrap;">{node[property]}</span
|
|
139
|
-
>
|
|
140
|
-
{/if}
|
|
141
|
-
{@render first?.({ node })}
|
|
142
|
-
</Td>
|
|
143
|
-
<!-- The rest of the Row -->
|
|
144
|
-
{@render children?.({ node })}
|
|
145
|
-
</tr>
|
|
146
|
-
|
|
147
|
-
{#if expanded}
|
|
148
|
-
{#each childRows as child}
|
|
149
|
-
<TreeRow
|
|
150
|
-
node={child}
|
|
151
|
-
depth={depth + 1}
|
|
152
|
-
{property}
|
|
153
|
-
{idProp}
|
|
154
|
-
{parentIdProp}
|
|
155
|
-
{showLabel}
|
|
156
|
-
{rowClick}
|
|
157
|
-
class={className}
|
|
158
|
-
>
|
|
159
|
-
{#snippet children(args)}
|
|
160
|
-
{@render children?.(args)}
|
|
161
|
-
{/snippet}
|
|
162
|
-
{#snippet first(args)}
|
|
163
|
-
{@render first?.(args)}
|
|
164
|
-
{/snippet}
|
|
165
|
-
{#snippet button(args)}
|
|
166
|
-
{@render button?.(args)}
|
|
167
|
-
{/snippet}
|
|
168
|
-
</TreeRow>
|
|
169
|
-
{/each}
|
|
170
|
-
{/if}
|
|
1
|
+
<script
|
|
2
|
+
lang="ts"
|
|
3
|
+
generics="T"
|
|
4
|
+
>
|
|
5
|
+
import type { Snippet } from 'svelte'
|
|
6
|
+
import type { Writable } from 'svelte/store'
|
|
7
|
+
import type { ClassValue } from 'svelte/elements'
|
|
8
|
+
import type { TreeNode, IdKeyType, ParentIdKeyType } from './tree-utility.js'
|
|
9
|
+
import TreeRow from './TreeRow.svelte'
|
|
10
|
+
|
|
11
|
+
type Key = IdKeyType<T>
|
|
12
|
+
type ParentKey = ParentIdKeyType<T>
|
|
13
|
+
type Node = TreeNode<T, Key, ParentKey>
|
|
14
|
+
|
|
15
|
+
import Button from '@isoftdata/svelte-button'
|
|
16
|
+
import Td from './Td.svelte'
|
|
17
|
+
import { getContext, hasContext, onMount } from 'svelte'
|
|
18
|
+
|
|
19
|
+
// 2 rem nest padding -> ml-2
|
|
20
|
+
// 1.5 rem leaf padding -> ml-3
|
|
21
|
+
const NEST_PADDING = 1.5 // rem
|
|
22
|
+
const LEAF_MARGIN_CLASS = 'ml-3'
|
|
23
|
+
|
|
24
|
+
interface Props {
|
|
25
|
+
property: keyof T
|
|
26
|
+
idProp: Key
|
|
27
|
+
parentIdProp: ParentKey
|
|
28
|
+
node: Node
|
|
29
|
+
depth?: number
|
|
30
|
+
showLabel?: boolean
|
|
31
|
+
class?: ClassValue
|
|
32
|
+
// Snippets
|
|
33
|
+
button?: Snippet<[{ node: Node }]>
|
|
34
|
+
first?: Snippet<[{ node: Node }]>
|
|
35
|
+
children?: Snippet<[{ node: Node }]>
|
|
36
|
+
// Callbacks
|
|
37
|
+
rowClick?: (context: { node: Node }) => void
|
|
38
|
+
toggleFold?: (context: { expanded: boolean }) => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let {
|
|
42
|
+
//
|
|
43
|
+
property,
|
|
44
|
+
idProp,
|
|
45
|
+
parentIdProp,
|
|
46
|
+
node,
|
|
47
|
+
depth = 0,
|
|
48
|
+
showLabel = true,
|
|
49
|
+
class: className = '',
|
|
50
|
+
button,
|
|
51
|
+
first,
|
|
52
|
+
children,
|
|
53
|
+
rowClick,
|
|
54
|
+
toggleFold,
|
|
55
|
+
}: Props = $props()
|
|
56
|
+
|
|
57
|
+
function pad(depth: number) {
|
|
58
|
+
return depth * NEST_PADDING + 'rem'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const selectedRowIds = getContext<Writable<(number | string)[]>>('selectedRowIds')
|
|
62
|
+
if (!hasContext('expandedRows')) {
|
|
63
|
+
throw new Error('TreeRow requires that the `tree` prop is set to true on a parent Table component.')
|
|
64
|
+
}
|
|
65
|
+
const expandedRows = getContext<Writable<Record<number | string, boolean>>>('expandedRows')
|
|
66
|
+
|
|
67
|
+
let expanded = $derived($expandedRows[node[idProp] as number | string] ?? false)
|
|
68
|
+
let icon: 'caret-down' | 'caret-right' = $derived(expanded ? 'caret-down' : 'caret-right')
|
|
69
|
+
let buttonPadding = $derived(pad(depth))
|
|
70
|
+
let leafPadding = $derived(pad(depth + 1))
|
|
71
|
+
let childRows = $derived(node ? node.children : [])
|
|
72
|
+
|
|
73
|
+
let selected = $derived($selectedRowIds ? $selectedRowIds.includes(node[idProp] as number | string) : false)
|
|
74
|
+
let stringProperty = $derived(typeof property === 'string' ? property : property.toString())
|
|
75
|
+
|
|
76
|
+
function setExpanded(expanded: boolean) {
|
|
77
|
+
$expandedRows[node[idProp] as number | string] = expanded
|
|
78
|
+
toggleFold?.({ expanded })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function onFoldClick(event: MouseEvent) {
|
|
82
|
+
event.preventDefault()
|
|
83
|
+
event.stopPropagation()
|
|
84
|
+
setExpanded(!expanded)
|
|
85
|
+
}
|
|
86
|
+
onMount(() => {
|
|
87
|
+
// Run this on mount so on page load, the selected row is expanded.
|
|
88
|
+
function isExpanded(expandedRows: Record<number | string, boolean>, node: Node): boolean {
|
|
89
|
+
if (expandedRows[node[idProp] as number | string]) {
|
|
90
|
+
return true
|
|
91
|
+
}
|
|
92
|
+
if (node.children) {
|
|
93
|
+
return node.children.some(
|
|
94
|
+
child => isExpanded(expandedRows, child) || $selectedRowIds.includes(child[idProp] as number | string),
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
if (!expanded && node.children) {
|
|
100
|
+
$expandedRows[node[idProp] as number | string] = isExpanded($expandedRows, node)
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
</script>
|
|
104
|
+
|
|
105
|
+
<tr
|
|
106
|
+
class={className}
|
|
107
|
+
class:table-primary={selected}
|
|
108
|
+
data-id={node[idProp]}
|
|
109
|
+
data-parentid={node[parentIdProp]}
|
|
110
|
+
onclick={() => rowClick?.({ node })}
|
|
111
|
+
ondblclick={onFoldClick}
|
|
112
|
+
>
|
|
113
|
+
<Td property={stringProperty}>
|
|
114
|
+
{#if childRows.length}
|
|
115
|
+
<Button
|
|
116
|
+
size="xs"
|
|
117
|
+
color="link"
|
|
118
|
+
style="padding-left: {buttonPadding}; box-shadow: none!important; font-size: 1rem; text-wrap: nowrap;"
|
|
119
|
+
icon={{ icon: icon, class: 'fa-xl', fixedWidth: true, prefix: 'fas' }}
|
|
120
|
+
disabled={!childRows.length}
|
|
121
|
+
colorGreyDisabled={false}
|
|
122
|
+
onclick={onFoldClick}
|
|
123
|
+
onkeydown={e => {
|
|
124
|
+
if (e.key === 'ArrowRight') {
|
|
125
|
+
setExpanded(true)
|
|
126
|
+
} else if (e.key === 'ArrowLeft') {
|
|
127
|
+
setExpanded(false)
|
|
128
|
+
}
|
|
129
|
+
}}
|
|
130
|
+
>{@render button?.({ node })}
|
|
131
|
+
{#if showLabel}
|
|
132
|
+
{node[property]}
|
|
133
|
+
{/if}
|
|
134
|
+
</Button>
|
|
135
|
+
{:else if showLabel}
|
|
136
|
+
<span
|
|
137
|
+
class={LEAF_MARGIN_CLASS}
|
|
138
|
+
style="padding-left: {leafPadding}; text-wrap: nowrap;">{node[property]}</span
|
|
139
|
+
>
|
|
140
|
+
{/if}
|
|
141
|
+
{@render first?.({ node })}
|
|
142
|
+
</Td>
|
|
143
|
+
<!-- The rest of the Row -->
|
|
144
|
+
{@render children?.({ node })}
|
|
145
|
+
</tr>
|
|
146
|
+
|
|
147
|
+
{#if expanded}
|
|
148
|
+
{#each childRows as child}
|
|
149
|
+
<TreeRow
|
|
150
|
+
node={child}
|
|
151
|
+
depth={depth + 1}
|
|
152
|
+
{property}
|
|
153
|
+
{idProp}
|
|
154
|
+
{parentIdProp}
|
|
155
|
+
{showLabel}
|
|
156
|
+
{rowClick}
|
|
157
|
+
class={className}
|
|
158
|
+
>
|
|
159
|
+
{#snippet children(args)}
|
|
160
|
+
{@render children?.(args)}
|
|
161
|
+
{/snippet}
|
|
162
|
+
{#snippet first(args)}
|
|
163
|
+
{@render first?.(args)}
|
|
164
|
+
{/snippet}
|
|
165
|
+
{#snippet button(args)}
|
|
166
|
+
{@render button?.(args)}
|
|
167
|
+
{/snippet}
|
|
168
|
+
</TreeRow>
|
|
169
|
+
{/each}
|
|
170
|
+
{/if}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isoftdata/svelte-table",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.4",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@faker-js/faker": "^8.4.1",
|
|
20
|
-
"@fortawesome/fontawesome-common-types": "^
|
|
20
|
+
"@fortawesome/fontawesome-common-types": "^7.1.0",
|
|
21
21
|
"@isoftdata/prettier-config": "^2.0.4",
|
|
22
22
|
"@isoftdata/svelte-bootstrap-version-switcher": "^1.1.2",
|
|
23
23
|
"@isoftdata/svelte-checkbox": "^2.2.1",
|