@isoftdata/svelte-table 2.11.1 → 2.12.0

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,171 @@
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<Array<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, 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
+ <!-- eslint-disable-next-line svelte/require-each-key -->
149
+ {#each childRows as child}
150
+ <TreeRow
151
+ node={child}
152
+ depth={depth + 1}
153
+ {property}
154
+ {idProp}
155
+ {parentIdProp}
156
+ {showLabel}
157
+ {rowClick}
158
+ class={className}
159
+ >
160
+ {#snippet children(args)}
161
+ {@render children?.(args)}
162
+ {/snippet}
163
+ {#snippet first(args)}
164
+ {@render first?.(args)}
165
+ {/snippet}
166
+ {#snippet button(args)}
167
+ {@render button?.(args)}
168
+ {/snippet}
169
+ </TreeRow>
170
+ {/each}
171
+ {/if}
@@ -1,10 +1,12 @@
1
- import type { PathsOptions, Paths } from 'type-fest';
1
+ import type { Paths } from 'type-fest';
2
2
  /**
3
3
  Generate a union of all possible paths to properties in the given object, denoting the object structure with brackets.
4
4
 
5
5
  This is the `Paths` utility type from the `type-fest` package, but with brackets.
6
6
  */
7
- export type BracketPaths<Row, Options extends PathsOptions = {}> = DotToBracket<Paths<Row, Options>>;
8
- export type DotToBracket<S extends string | unknown> = S extends `${infer Head}.${infer Tail}` ? `${Head}${_DotToBracketTail<Tail>}` : S;
7
+ export type BracketPaths<Row> = DotToBracket<Paths<Row, {
8
+ maxRecursionDepth: 3;
9
+ }>>;
10
+ export type DotToBracket<S> = S extends string ? S extends `${infer Head}.${infer Tail}` ? `${Head}${_DotToBracketTail<Tail>}` : S : never;
9
11
  type _DotToBracketTail<S extends string> = S extends `${infer Part}.${infer Rest}` ? `[${Part}]${_DotToBracketTail<Rest>}` : `[${S}]`;
10
12
  export {};
@@ -16,7 +16,7 @@ export type TreeNode<T, IdKeypath extends IdKeyType<T>, ParentIdKeypath extends
16
16
  * @param idKey The name of a unique id property on the objects in the array
17
17
  * @param parentIdKey The name of a property on the objects in the array that references the id of the parent object
18
18
  */
19
- export declare function treeify<T extends object, K extends IdKeyType<T>, P extends ParentIdKeyType<T>>(array: T[], idKey: K, parentIdKey: P): TreeNode<T, K, P>[];
19
+ export declare function treeify<T extends object, K extends IdKeyType<T>, P extends ParentIdKeyType<T>>(array: Array<T>, idKey: K, parentIdKey: P): TreeNode<T, K, P>[];
20
20
  /**
21
21
  * Gets the path to a node in the tree with the given id
22
22
  * @param tree The tree
@@ -121,9 +121,7 @@ export function upsertIntoTree(tree, node, key, parentKey) {
121
121
  * @param deleteChildren If true, the node's children will be deleted. If false, the node's children will be moved to the node's parent
122
122
  * @returns The tree, with the node(s) removed
123
123
  */
124
- export function removeFromTree(tree, node, key, parentKey,
125
- // eslint-disable-next-line comma-dangle
126
- deleteChildren = false) {
124
+ export function removeFromTree(tree, node, key, parentKey, deleteChildren = false) {
127
125
  const foundNode = findNodeById(tree, key, node[key]);
128
126
  if (foundNode) {
129
127
  const parent = foundNode.parent;
package/dist/types.d.ts CHANGED
@@ -8,11 +8,7 @@ export type SortDirection = 'ASC' | 'DESC';
8
8
  *
9
9
  * Can include bracket notation for nested properties, e.g. `foo[bar]` for `{ foo: { bar: 'baz' } }`. Max recursion depth is 3.
10
10
  */
11
- export type RowProperties<R> = BracketPaths<R, {
12
- maxRecursionDepth: 3;
13
- }> extends never ? string : BracketPaths<R, {
14
- maxRecursionDepth: 3;
15
- }>;
11
+ export type RowProperties<R> = BracketPaths<R> extends never ? string : BracketPaths<R>;
16
12
  /** A column in your table.
17
13
  *
18
14
  * If you specify a type for your row object, `property` must be a valid path to a property of that object.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoftdata/svelte-table",
3
- "version": "2.11.1",
3
+ "version": "2.12.0",
4
4
  "exports": {
5
5
  ".": {
6
6
  "types": "./dist/index.d.ts",
@@ -13,35 +13,32 @@
13
13
  "!dist/**/*.spec.*"
14
14
  ],
15
15
  "peerDependencies": {
16
- "svelte": "^5.22.1"
16
+ "svelte": "5.55.5"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@faker-js/faker": "^8.4.1",
20
20
  "@fortawesome/fontawesome-common-types": "^7.1.0",
21
+ "@isoftdata/eslint-config-svelte": "^1.1.0",
21
22
  "@isoftdata/prettier-config": "^2.0.4",
22
23
  "@isoftdata/svelte-bootstrap-version-switcher": "^1.1.2",
23
24
  "@isoftdata/svelte-checkbox": "^2.2.1",
24
25
  "@isoftdata/svelte-select": "^2.0.1",
25
26
  "@isoftdata/svelte-textarea": "^2.0.0",
26
27
  "@sveltejs/adapter-auto": "^3.3.1",
27
- "@sveltejs/kit": "^2.53.2",
28
- "@sveltejs/package": "^2.3.10",
28
+ "@sveltejs/kit": "^2.59.1",
29
+ "@sveltejs/package": "^2.5.7",
29
30
  "@sveltejs/vite-plugin-svelte": "^5.0.3",
30
31
  "@types/natural-compare-lite": "^1.4.2",
31
- "@typescript-eslint/eslint-plugin": "^6.21.0",
32
- "@typescript-eslint/parser": "^6.21.0",
33
- "eslint": "^8.57.1",
34
- "eslint-config-prettier": "^8.10.0",
35
- "eslint-plugin-svelte": "^2.46.1",
32
+ "eslint": "^10.3.0",
36
33
  "i18next": "^24.2.2",
37
34
  "prettier": "^3.5.3",
38
35
  "prettier-plugin-svelte": "^3.3.3",
39
36
  "publint": "^0.1.16",
40
- "svelte": "5.53.5",
37
+ "svelte": "5.55.5",
41
38
  "svelte-check": "^4.1.4",
42
39
  "tslib": "^2.8.1",
43
40
  "type-fest": "^4.41.0",
44
- "typescript": "^5.8.2",
41
+ "typescript": "^6.0.3",
45
42
  "vite": "^6.2.0"
46
43
  },
47
44
  "svelte": "./dist/index.js",
@@ -51,7 +48,7 @@
51
48
  "dependencies": {
52
49
  "@isoftdata/browser-event": "^2.3.2",
53
50
  "@isoftdata/svelte-button": "^2.1.0",
54
- "@isoftdata/svelte-context-menu": "^2.0.4",
51
+ "@isoftdata/svelte-context-menu": "^2.1.0",
55
52
  "@isoftdata/svelte-icon": "^2.1.0",
56
53
  "@isoftdata/svelte-input": "^2.1.1",
57
54
  "@isoftdata/svelte-store-user-local-writable": "^2.0.0",
@@ -59,12 +56,13 @@
59
56
  "@isoftdata/utility-currency": "^3.0.0",
60
57
  "@isoftdata/utility-string": "^2.1.0",
61
58
  "@lukeed/uuid": "^2.0.1",
59
+ "@types/node": "^22.19.18",
62
60
  "klona": "^2.0.6",
63
61
  "natural-compare-lite": "^1.4.0",
64
62
  "runed": "^0.23.4"
65
63
  },
66
64
  "engines": {
67
- "pnpm": "10.x"
65
+ "pnpm": "11.x"
68
66
  },
69
67
  "scripts": {
70
68
  "dev": "vite dev",