@isoftdata/svelte-table 2.7.0 → 2.8.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,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}
@@ -1,4 +1,4 @@
1
- import type { ColumnInfoObject, Column, UuidRowProps, ColumnInfo as GenericColumnInfo, RowProperties } from './types';
1
+ import type { ColumnInfoObject, Column, UuidRowProps, ColumnInfo as GenericColumnInfo, RowProperties, ArbitraryProperty } from './types';
2
2
  type StoreOptions = {
3
3
  /** The key to use in localstorage */
4
4
  key?: string;
@@ -44,9 +44,9 @@ export declare class ColumnInfoRunicStore<R extends UuidRowProps> {
44
44
  iconLeft?: boolean | undefined;
45
45
  iconPrefix?: import("@fortawesome/fontawesome-common-types").IconPrefix | undefined;
46
46
  minWidth?: string | undefined;
47
- name: string;
47
+ name?: string | undefined;
48
48
  numeric?: boolean | undefined;
49
- property: RowProperties<R>;
49
+ property: `_${string}` | RowProperties<R>;
50
50
  sortType?: "ALPHA_NUM" | "STANDARD" | false | undefined;
51
51
  title?: string | undefined;
52
52
  unhidable?: boolean | undefined;
@@ -56,9 +56,9 @@ export declare class ColumnInfoRunicStore<R extends UuidRowProps> {
56
56
  visible: boolean;
57
57
  }[];
58
58
  get current(): ColumnInfoObject<R>;
59
- updateColumn(property: RowProperties<R>, value: Partial<GenericColumnInfo<R>>): void;
59
+ updateColumn(property: RowProperties<R> | ArbitraryProperty, value: Partial<GenericColumnInfo<R>>): void;
60
60
  updateColumns(newColumnInfos: Array<Partial<GenericColumnInfo<R>> & {
61
- property: RowProperties<R>;
61
+ property: RowProperties<R> | ArbitraryProperty;
62
62
  }>): void;
63
63
  private getStoreValFromColumns;
64
64
  /** Takes values from the given column, removes non-serializable values, and fills in with default serializable values */
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export { default as TreeRow } from './TreeRow.svelte';
6
6
  export { default as DraggableRows } from './DraggableRows.svelte';
7
7
  export * from './column-info-store.svelte';
8
8
  export * from './tree-utility.js';
9
+ export * from './types.js';
9
10
  export type * from './Pagination.svelte';
10
11
  export type * from './types';
11
12
  export type * from './bracket-paths';
package/dist/index.js CHANGED
@@ -6,3 +6,4 @@ export { default as TreeRow } from './TreeRow.svelte';
6
6
  export { default as DraggableRows } from './DraggableRows.svelte';
7
7
  export * from './column-info-store.svelte';
8
8
  export * from './tree-utility.js';
9
+ export * from './types.js';
package/dist/types.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import type { IconName, IconPrefix } from '@fortawesome/fontawesome-common-types';
2
2
  import type { BracketPaths } from './bracket-paths';
3
3
  import type { Get, Simplify } from 'type-fest';
4
+ export declare const ARBITRARY_PROPERTY_PREFIX = "_";
5
+ export type ArbitraryProperty = `${typeof ARBITRARY_PROPERTY_PREFIX}${string}`;
4
6
  export type SortDirection = 'ASC' | 'DESC';
5
7
  /** String untion of all properties of the row object, or just `string` as a fallback.
6
8
  *
@@ -49,9 +51,9 @@ export type Column<R extends UuidRowProps = any> = {
49
51
  iconLeft?: boolean;
50
52
  iconPrefix?: IconPrefix;
51
53
  minWidth?: string;
52
- name: string;
54
+ name?: string;
53
55
  numeric?: boolean;
54
- property: RowProperties<R>;
56
+ property: RowProperties<R> | ArbitraryProperty;
55
57
  sortType?: 'ALPHA_NUM' | 'STANDARD' | false;
56
58
  title?: string;
57
59
  unhidable?: boolean;
@@ -60,7 +62,7 @@ export type Column<R extends UuidRowProps = any> = {
60
62
  width?: string;
61
63
  wrap?: boolean;
62
64
  };
63
- export type FooterReducerValue<R extends UuidRowProps> = string | number | Get<R, RowProperties<R>>;
65
+ export type FooterReducerValue<R extends UuidRowProps> = string | number | Get<R, RowProperties<R> | ArbitraryProperty>;
64
66
  export type ColumnInfo<R extends UuidRowProps> = Simplify<Column<R> & {
65
67
  visible: boolean;
66
68
  }>;
package/dist/types.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export const ARBITRARY_PROPERTY_PREFIX = '_';
package/package.json CHANGED
@@ -1,17 +1,6 @@
1
1
  {
2
2
  "name": "@isoftdata/svelte-table",
3
- "version": "2.7.0",
4
- "scripts": {
5
- "dev": "vite dev",
6
- "build": "vite build && npm run package",
7
- "preview": "vite preview",
8
- "package": "svelte-kit sync && svelte-package && publint",
9
- "prepublishOnly": "npm run package",
10
- "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
11
- "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12
- "lint": "prettier --plugin-search-dir . --check . && eslint .",
13
- "format": "prettier --plugin-search-dir . --write ."
14
- },
3
+ "version": "2.8.0",
15
4
  "exports": {
16
5
  ".": {
17
6
  "types": "./dist/index.d.ts",
@@ -75,5 +64,15 @@
75
64
  },
76
65
  "engines": {
77
66
  "pnpm": "10.x"
67
+ },
68
+ "scripts": {
69
+ "dev": "vite dev",
70
+ "build": "vite build && npm run package",
71
+ "preview": "vite preview",
72
+ "package": "svelte-kit sync && svelte-package && publint",
73
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
74
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
75
+ "lint": "prettier --plugin-search-dir . --check . && eslint .",
76
+ "format": "prettier --plugin-search-dir . --write ."
78
77
  }
79
- }
78
+ }