@isoftdata/svelte-table 2.10.5 → 2.11.1

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.
@@ -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.10.5",
3
+ "version": "2.11.1",
4
4
  "exports": {
5
5
  ".": {
6
6
  "types": "./dist/index.d.ts",