sveltacular 1.0.14 → 1.0.16
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 +2 -3
- package/dist/forms/check-box/check-box.svelte +22 -12
- package/dist/forms/check-box/check-box.svelte.d.ts +1 -0
- package/dist/forms/dimension-box/dimension-box.svelte +18 -3
- package/dist/forms/dimension-box/dimension-box.svelte.d.ts +4 -0
- package/dist/forms/file-area/file-area.svelte +2 -2
- package/dist/forms/info-box/info-box.svelte +2 -2
- package/dist/forms/list-box/list-box.svelte +2 -2
- package/dist/forms/money-box/money-box.svelte +23 -3
- package/dist/forms/money-box/money-box.svelte.d.ts +5 -0
- package/dist/forms/number-range-box/number-range-box.svelte +20 -4
- package/dist/forms/number-range-box/number-range-box.svelte.d.ts +4 -0
- package/dist/forms/radio-group/radio-box.svelte +21 -26
- package/dist/forms/radio-group/radio-box.svelte.d.ts +1 -0
- package/dist/forms/text-box/text-box.svelte +2 -2
- package/dist/generic/email/email.svelte +2 -2
- package/dist/generic/menu/menu.svelte +3 -3
- package/dist/generic/phone/phone.svelte +3 -4
- package/dist/icons/icon-data.d.ts +28 -0
- package/dist/icons/icon-data.js +621 -0
- package/dist/icons/icon.svelte +164 -0
- package/dist/icons/icon.svelte.d.ts +13 -0
- package/dist/icons/index.d.ts +2 -13
- package/dist/icons/index.js +1 -13
- package/dist/icons/types.d.ts +4 -0
- package/dist/icons/types.js +1 -0
- package/dist/images/index.d.ts +0 -1
- package/dist/images/index.js +0 -1
- package/dist/layout/main/main.svelte +2 -2
- package/dist/navigation/accordion/accordion.svelte +2 -2
- package/dist/navigation/app-bar/app-bar.svelte +1 -0
- package/dist/navigation/app-bar/app-branding.svelte +4 -3
- package/dist/navigation/app-bar/app-logo.svelte +8 -5
- package/dist/navigation/app-bar/app-nav-item.svelte +48 -1
- package/dist/navigation/app-bar/app-nav.svelte +2 -2
- package/dist/navigation/breadcrumbs/breadcrumbs.svelte +2 -2
- package/dist/navigation/context-menu/context-menu-item.svelte +2 -2
- package/dist/navigation/dropdown-button/dropdown-button.svelte +2 -2
- package/dist/tables/data-grid.svelte +38 -14
- package/dist/tables/data-grid.svelte.d.ts +4 -4
- package/dist/tables/table-context.svelte.d.ts +9 -5
- package/dist/tables/table-context.svelte.js +59 -12
- package/dist/tables/table-header-cell.svelte +21 -12
- package/dist/tables/table-row.svelte +3 -39
- package/dist/tables/table-selection-cell.svelte +134 -0
- package/dist/tables/table-selection-cell.svelte.d.ts +8 -0
- package/dist/tables/table-selection-header-cell.svelte +103 -0
- package/dist/tables/table-selection-header-cell.svelte.d.ts +3 -0
- package/dist/tables/table.svelte +15 -9
- package/dist/tables/table.svelte.d.ts +4 -3
- package/package.json +1 -1
|
@@ -7,13 +7,16 @@ export class TableContext {
|
|
|
7
7
|
// Selection state
|
|
8
8
|
selectedIds = $state(new Set());
|
|
9
9
|
lastSelectedIndex = $state(null);
|
|
10
|
+
// Radio button group state (for single selection mode)
|
|
11
|
+
radioGroup = $state(undefined);
|
|
12
|
+
// Reactive selected count
|
|
13
|
+
selectedCount = $derived(this.selectedIds.size);
|
|
10
14
|
// Configuration
|
|
11
15
|
config;
|
|
12
16
|
constructor(config = {}) {
|
|
13
17
|
this.config = {
|
|
14
18
|
enableSorting: true,
|
|
15
|
-
|
|
16
|
-
selectionMode: 'multi',
|
|
19
|
+
selectionMode: 'none',
|
|
17
20
|
rowIdKey: 'id',
|
|
18
21
|
...config
|
|
19
22
|
};
|
|
@@ -56,25 +59,31 @@ export class TableContext {
|
|
|
56
59
|
};
|
|
57
60
|
}
|
|
58
61
|
// Selection methods
|
|
59
|
-
toggleRow(id, index, shiftKey = false) {
|
|
60
|
-
if (
|
|
62
|
+
toggleRow(id, index, shiftKey = false, rows) {
|
|
63
|
+
if (this.config.selectionMode === 'none')
|
|
61
64
|
return;
|
|
62
65
|
if (this.config.selectionMode === 'single') {
|
|
63
66
|
// Single selection mode
|
|
64
67
|
if (this.selectedIds.has(id)) {
|
|
65
68
|
this.selectedIds.delete(id);
|
|
69
|
+
this.radioGroup = undefined;
|
|
70
|
+
// Trigger reactivity
|
|
71
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
66
72
|
}
|
|
67
73
|
else {
|
|
68
74
|
this.selectedIds.clear();
|
|
69
75
|
this.selectedIds.add(id);
|
|
76
|
+
this.radioGroup = String(id);
|
|
77
|
+
// Trigger reactivity
|
|
78
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
70
79
|
}
|
|
71
80
|
this.lastSelectedIndex = index;
|
|
72
81
|
}
|
|
73
82
|
else {
|
|
74
83
|
// Multi selection mode
|
|
75
|
-
if (shiftKey && this.lastSelectedIndex !== null) {
|
|
84
|
+
if (shiftKey && this.lastSelectedIndex !== null && rows) {
|
|
76
85
|
// Range selection
|
|
77
|
-
this.selectRange(this.lastSelectedIndex, index);
|
|
86
|
+
this.selectRange(this.lastSelectedIndex, index, rows);
|
|
78
87
|
}
|
|
79
88
|
else {
|
|
80
89
|
// Toggle single row
|
|
@@ -84,10 +93,18 @@ export class TableContext {
|
|
|
84
93
|
else {
|
|
85
94
|
this.selectedIds.add(id);
|
|
86
95
|
}
|
|
96
|
+
// Trigger reactivity
|
|
97
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
87
98
|
this.lastSelectedIndex = index;
|
|
88
99
|
}
|
|
89
100
|
}
|
|
90
|
-
this.
|
|
101
|
+
this.notifySelectionChange(rows);
|
|
102
|
+
}
|
|
103
|
+
notifySelectionChange(rows) {
|
|
104
|
+
if (this.config.onSelectionChange && rows) {
|
|
105
|
+
const selectedRows = this.getSelectedRows(rows);
|
|
106
|
+
this.config.onSelectionChange(selectedRows);
|
|
107
|
+
}
|
|
91
108
|
}
|
|
92
109
|
selectRange(startIndex, endIndex, rows) {
|
|
93
110
|
if (!rows)
|
|
@@ -103,23 +120,52 @@ export class TableContext {
|
|
|
103
120
|
}
|
|
104
121
|
}
|
|
105
122
|
}
|
|
106
|
-
|
|
123
|
+
// Trigger reactivity
|
|
124
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
125
|
+
this.notifySelectionChange(rows);
|
|
107
126
|
}
|
|
108
127
|
selectAll(rows) {
|
|
109
|
-
if (
|
|
128
|
+
if (this.config.selectionMode === 'none')
|
|
110
129
|
return;
|
|
130
|
+
// Clear first, then add all to ensure proper state update
|
|
131
|
+
this.selectedIds.clear();
|
|
111
132
|
rows.forEach((row) => {
|
|
112
133
|
const id = row[this.config.rowIdKey];
|
|
113
134
|
if (id !== undefined) {
|
|
114
135
|
this.selectedIds.add(id);
|
|
115
136
|
}
|
|
116
137
|
});
|
|
117
|
-
|
|
138
|
+
// Trigger reactivity by reassigning (Svelte 5 tracks Set mutations, but ensure it's reactive)
|
|
139
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
140
|
+
this.notifySelectionChange(rows);
|
|
118
141
|
}
|
|
119
|
-
deselectAll() {
|
|
142
|
+
deselectAll(rows) {
|
|
120
143
|
this.selectedIds.clear();
|
|
144
|
+
// Trigger reactivity by reassigning
|
|
145
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
121
146
|
this.lastSelectedIndex = null;
|
|
122
|
-
this.
|
|
147
|
+
this.radioGroup = undefined;
|
|
148
|
+
this.notifySelectionChange(rows);
|
|
149
|
+
}
|
|
150
|
+
// Method to handle radio group changes (from radio button bindings)
|
|
151
|
+
setRadioSelection(value, rows) {
|
|
152
|
+
if (this.config.selectionMode !== 'single')
|
|
153
|
+
return;
|
|
154
|
+
if (value) {
|
|
155
|
+
this.selectedIds.clear();
|
|
156
|
+
this.selectedIds.add(value);
|
|
157
|
+
// Trigger reactivity
|
|
158
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
159
|
+
this.radioGroup = value;
|
|
160
|
+
this.notifySelectionChange(rows);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.selectedIds.clear();
|
|
164
|
+
// Trigger reactivity
|
|
165
|
+
this.selectedIds = new Set(this.selectedIds);
|
|
166
|
+
this.radioGroup = undefined;
|
|
167
|
+
this.notifySelectionChange(rows);
|
|
168
|
+
}
|
|
123
169
|
}
|
|
124
170
|
isRowSelected(id) {
|
|
125
171
|
return this.selectedIds.has(id);
|
|
@@ -140,6 +186,7 @@ export class TableContext {
|
|
|
140
186
|
return id !== undefined && this.selectedIds.has(id);
|
|
141
187
|
});
|
|
142
188
|
}
|
|
189
|
+
// selectedCount is now a reactive derived state, but keep this method for backwards compatibility
|
|
143
190
|
getSelectedCount() {
|
|
144
191
|
return this.selectedIds.size;
|
|
145
192
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
|
+
import Icon from '../icons/icon.svelte';
|
|
4
|
+
import type { IconType } from '../icons/types.js';
|
|
3
5
|
|
|
4
6
|
let {
|
|
5
7
|
colspan = 1,
|
|
@@ -73,6 +75,17 @@
|
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
}
|
|
78
|
+
|
|
79
|
+
// Determine which icon to show based on sort state
|
|
80
|
+
let sortIconType = $derived<IconType | undefined>(
|
|
81
|
+
canSort
|
|
82
|
+
? isSorted
|
|
83
|
+
? sortDirection === 'asc'
|
|
84
|
+
? 'triangle-up'
|
|
85
|
+
: 'triangle-down'
|
|
86
|
+
: 'triangle-up-down'
|
|
87
|
+
: undefined
|
|
88
|
+
);
|
|
76
89
|
</script>
|
|
77
90
|
|
|
78
91
|
<th
|
|
@@ -91,17 +104,14 @@
|
|
|
91
104
|
<span class="header-text">
|
|
92
105
|
{@render children?.()}
|
|
93
106
|
</span>
|
|
94
|
-
{#if canSort}
|
|
107
|
+
{#if canSort && sortIconType}
|
|
95
108
|
<span class="sort-indicator" aria-hidden="true">
|
|
96
|
-
|
|
97
|
-
{
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
{:else}
|
|
103
|
-
<span class="sort-arrow unsorted">⇅</span>
|
|
104
|
-
{/if}
|
|
109
|
+
<Icon
|
|
110
|
+
type={sortIconType}
|
|
111
|
+
size="sm"
|
|
112
|
+
variant="secondary"
|
|
113
|
+
class="sort-arrow {isSorted ? '' : 'unsorted'}"
|
|
114
|
+
/>
|
|
105
115
|
</span>
|
|
106
116
|
{/if}
|
|
107
117
|
</div>
|
|
@@ -158,7 +168,6 @@ th.sorted {
|
|
|
158
168
|
flex-shrink: 0;
|
|
159
169
|
display: inline-flex;
|
|
160
170
|
align-items: center;
|
|
161
|
-
font-size: 0.75rem;
|
|
162
171
|
opacity: 0.7;
|
|
163
172
|
}
|
|
164
173
|
|
|
@@ -170,6 +179,6 @@ th.sorted {
|
|
|
170
179
|
opacity: 0.3;
|
|
171
180
|
}
|
|
172
181
|
|
|
173
|
-
th.sortable:hover .sort-arrow.unsorted {
|
|
182
|
+
th.sortable:hover :global(.sort-arrow.unsorted) {
|
|
174
183
|
opacity: 0.6;
|
|
175
184
|
}</style>
|
|
@@ -22,36 +22,11 @@
|
|
|
22
22
|
row && context?.config.rowIdKey ? (row[context.config.rowIdKey] as string | number) : undefined
|
|
23
23
|
);
|
|
24
24
|
|
|
25
|
-
let
|
|
26
|
-
|
|
27
|
-
let canSelect = $derived(selectable && context?.config.enableSelection && rowId !== undefined);
|
|
28
|
-
|
|
29
|
-
function handleClick(event: MouseEvent) {
|
|
30
|
-
if (canSelect && rowId !== undefined && rowIndex !== undefined) {
|
|
31
|
-
const shiftKey = event.shiftKey;
|
|
32
|
-
context?.toggleRow(rowId, rowIndex, shiftKey);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function handleKeyDown(event: KeyboardEvent) {
|
|
37
|
-
if (canSelect && (event.key === 'Enter' || event.key === ' ')) {
|
|
38
|
-
event.preventDefault();
|
|
39
|
-
if (rowId !== undefined && rowIndex !== undefined) {
|
|
40
|
-
context?.toggleRow(rowId, rowIndex, event.shiftKey);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
25
|
+
let selectionMode = $derived(context?.config.selectionMode ?? 'none');
|
|
26
|
+
let canSelect = $derived(selectable && selectionMode !== 'none' && rowId !== undefined);
|
|
44
27
|
</script>
|
|
45
28
|
|
|
46
|
-
<tr
|
|
47
|
-
class:selectable={canSelect}
|
|
48
|
-
class:selected={isSelected}
|
|
49
|
-
aria-selected={canSelect ? isSelected : undefined}
|
|
50
|
-
tabindex={canSelect ? 0 : undefined}
|
|
51
|
-
onclick={handleClick}
|
|
52
|
-
onkeydown={handleKeyDown}
|
|
53
|
-
role={canSelect ? 'row' : undefined}
|
|
54
|
-
>
|
|
29
|
+
<tr class:selectable={canSelect}>
|
|
55
30
|
{@render children?.()}
|
|
56
31
|
</tr>
|
|
57
32
|
|
|
@@ -69,8 +44,6 @@
|
|
|
69
44
|
}
|
|
70
45
|
|
|
71
46
|
tr.selectable {
|
|
72
|
-
cursor: pointer;
|
|
73
|
-
|
|
74
47
|
&:hover {
|
|
75
48
|
background-color: var(--table-row-hover-bg, rgba(0, 0, 0, 0.05));
|
|
76
49
|
}
|
|
@@ -80,13 +53,4 @@
|
|
|
80
53
|
outline-offset: -2px;
|
|
81
54
|
}
|
|
82
55
|
}
|
|
83
|
-
|
|
84
|
-
tr.selected {
|
|
85
|
-
background-color: var(--table-row-selected-bg, rgba(0, 102, 204, 0.1)) !important;
|
|
86
|
-
border-color: var(--table-row-selected-border, #0066cc);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
tr.selected.selectable:hover {
|
|
90
|
-
background-color: var(--table-row-selected-hover-bg, rgba(0, 102, 204, 0.15)) !important;
|
|
91
|
-
}
|
|
92
56
|
</style>
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { JsonObject } from '../types/data.js';
|
|
3
|
+
import { getTableContext } from './table-context.svelte.js';
|
|
4
|
+
import CheckBox from '../forms/check-box/check-box.svelte';
|
|
5
|
+
import RadioBox from '../forms/radio-group/radio-box.svelte';
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
row = undefined,
|
|
9
|
+
rowIndex = undefined
|
|
10
|
+
}: {
|
|
11
|
+
row?: JsonObject;
|
|
12
|
+
rowIndex?: number;
|
|
13
|
+
} = $props();
|
|
14
|
+
|
|
15
|
+
const context = getTableContext();
|
|
16
|
+
|
|
17
|
+
if (!context) {
|
|
18
|
+
throw new Error('TableSelectionCell must be used within a Table component');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Get row ID for selection
|
|
22
|
+
let rowId = $derived(
|
|
23
|
+
row && context?.config.rowIdKey ? (row[context.config.rowIdKey] as string | number) : undefined
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// Derive selection state from context - this is the source of truth
|
|
27
|
+
// Access selectedIds directly to ensure reactivity is tracked properly
|
|
28
|
+
let isSelectedInContext = $derived.by(() => {
|
|
29
|
+
if (rowId === undefined || !context) return false;
|
|
30
|
+
// Explicitly access selectedIds to set up reactivity tracking
|
|
31
|
+
const selectedIds = context.selectedIds;
|
|
32
|
+
return selectedIds.has(rowId);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
let selectionMode = $derived(context?.config.selectionMode ?? 'none');
|
|
36
|
+
|
|
37
|
+
// Local state for checkbox binding - synced from context
|
|
38
|
+
let localChecked = $state(false);
|
|
39
|
+
|
|
40
|
+
// Sync local state FROM context whenever context selection changes
|
|
41
|
+
// This ensures external changes (like "select all") update the checkbox
|
|
42
|
+
$effect(() => {
|
|
43
|
+
localChecked = isSelectedInContext;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// For radio buttons in single selection mode
|
|
47
|
+
let radioGroupLocal = $state<string | undefined>(undefined);
|
|
48
|
+
|
|
49
|
+
// Sync radio state FROM context
|
|
50
|
+
$effect(() => {
|
|
51
|
+
if (selectionMode === 'single' && context) {
|
|
52
|
+
radioGroupLocal = context.radioGroup;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Handle checkbox change - update context (source of truth)
|
|
57
|
+
function handleCheckboxChange(data: { isChecked: boolean; value: string }) {
|
|
58
|
+
if (rowId !== undefined && rowIndex !== undefined && context) {
|
|
59
|
+
const rows = context.config.rows ?? [];
|
|
60
|
+
// Toggle the row in context - context is the source of truth
|
|
61
|
+
context.toggleRow(rowId, rowIndex, false, rows);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Handle radio button change
|
|
66
|
+
function handleRadioChange(value: string) {
|
|
67
|
+
if (context && selectionMode === 'single') {
|
|
68
|
+
const rows = context.config.rows ?? [];
|
|
69
|
+
// Toggle: if already selected, deselect; otherwise select
|
|
70
|
+
if (context.radioGroup === value) {
|
|
71
|
+
context.setRadioSelection(undefined, rows);
|
|
72
|
+
} else {
|
|
73
|
+
context.setRadioSelection(value, rows);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Handle cell click - toggle selection when clicking anywhere in the cell
|
|
79
|
+
function handleCellClick(event: MouseEvent) {
|
|
80
|
+
const target = event.target as HTMLElement;
|
|
81
|
+
|
|
82
|
+
if (selectionMode === 'multi') {
|
|
83
|
+
// For checkboxes: skip if clicking on input to avoid double-toggle
|
|
84
|
+
if (target.tagName === 'INPUT') return;
|
|
85
|
+
handleCheckboxChange({ isChecked: !localChecked, value: '' });
|
|
86
|
+
} else if (selectionMode === 'single' && rowId !== undefined) {
|
|
87
|
+
// For radios: always handle the click ourselves to enable deselection
|
|
88
|
+
// Native radio buttons don't allow unchecking, so we take control
|
|
89
|
+
if (target.tagName === 'INPUT') {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
}
|
|
92
|
+
handleRadioChange(String(rowId));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
</script>
|
|
96
|
+
|
|
97
|
+
{#if selectionMode === 'multi'}
|
|
98
|
+
<td class="selection-cell" role="gridcell" onclick={handleCellClick}>
|
|
99
|
+
<CheckBox
|
|
100
|
+
bind:isChecked={localChecked}
|
|
101
|
+
onChange={handleCheckboxChange}
|
|
102
|
+
inline={true}
|
|
103
|
+
ariaLabel="Select row"
|
|
104
|
+
/>
|
|
105
|
+
</td>
|
|
106
|
+
{:else if selectionMode === 'single'}
|
|
107
|
+
<td class="selection-cell" role="gridcell" onclick={handleCellClick}>
|
|
108
|
+
<RadioBox
|
|
109
|
+
value={String(rowId ?? '')}
|
|
110
|
+
bind:group={radioGroupLocal}
|
|
111
|
+
onChange={handleRadioChange}
|
|
112
|
+
ariaLabel="Select row"
|
|
113
|
+
/>
|
|
114
|
+
</td>
|
|
115
|
+
{/if}
|
|
116
|
+
|
|
117
|
+
<style>.selection-cell {
|
|
118
|
+
text-align: center;
|
|
119
|
+
padding: 0.5rem 0.25rem;
|
|
120
|
+
width: 48px;
|
|
121
|
+
min-width: 48px;
|
|
122
|
+
vertical-align: middle;
|
|
123
|
+
cursor: pointer;
|
|
124
|
+
display: table-cell;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
:global(.selection-cell .checkbox-label),
|
|
128
|
+
:global(.selection-cell label) {
|
|
129
|
+
margin: 0 auto;
|
|
130
|
+
cursor: pointer;
|
|
131
|
+
display: flex;
|
|
132
|
+
align-items: center;
|
|
133
|
+
justify-content: center;
|
|
134
|
+
}</style>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { JsonObject } from '../types/data.js';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
row?: JsonObject;
|
|
4
|
+
rowIndex?: number;
|
|
5
|
+
};
|
|
6
|
+
declare const TableSelectionCell: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
7
|
+
type TableSelectionCell = ReturnType<typeof TableSelectionCell>;
|
|
8
|
+
export default TableSelectionCell;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getTableContext } from './table-context.svelte.js';
|
|
3
|
+
import CheckBox from '../forms/check-box/check-box.svelte';
|
|
4
|
+
|
|
5
|
+
const context = getTableContext();
|
|
6
|
+
|
|
7
|
+
if (!context) {
|
|
8
|
+
throw new Error('TableSelectionHeaderCell must be used within a Table component');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let selectionMode = $derived(context?.config.selectionMode ?? 'none');
|
|
12
|
+
let rows = $derived(context?.config.rows ?? []);
|
|
13
|
+
|
|
14
|
+
// Compute whether all rows are selected - source of truth from context
|
|
15
|
+
let isAllSelectedInContext = $derived.by(() => {
|
|
16
|
+
if (!context || !rows.length) return false;
|
|
17
|
+
// Access selectedIds directly to ensure reactivity is tracked
|
|
18
|
+
const selectedIds = context.selectedIds;
|
|
19
|
+
const rowIdKey = context.config.rowIdKey!;
|
|
20
|
+
// Check if every row's ID is in the selected set
|
|
21
|
+
return rows.every((row) => {
|
|
22
|
+
const id = row[rowIdKey] as string | number;
|
|
23
|
+
return id !== undefined && selectedIds.has(id);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Local state for checkbox binding - synced from context
|
|
28
|
+
let localChecked = $state(false);
|
|
29
|
+
|
|
30
|
+
// Sync local state FROM context whenever selection changes
|
|
31
|
+
// This ensures the header checkbox reflects the actual selection state
|
|
32
|
+
$effect(() => {
|
|
33
|
+
localChecked = isAllSelectedInContext;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Handle "select all" checkbox change
|
|
37
|
+
function handleSelectAllChange(data: { isChecked: boolean; value: string }) {
|
|
38
|
+
if (!context) return;
|
|
39
|
+
const currentRows = rows;
|
|
40
|
+
if (!currentRows || currentRows.length === 0) return;
|
|
41
|
+
|
|
42
|
+
// data.isChecked is the NEW desired state from user interaction
|
|
43
|
+
if (data.isChecked) {
|
|
44
|
+
// User wants to select all
|
|
45
|
+
context.selectAll(currentRows);
|
|
46
|
+
} else {
|
|
47
|
+
// User wants to deselect all
|
|
48
|
+
context.deselectAll(currentRows);
|
|
49
|
+
}
|
|
50
|
+
// The effect will sync localChecked from context after this
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Handle header cell click - toggle select all when clicking anywhere in the cell
|
|
54
|
+
function handleCellClick(event: MouseEvent) {
|
|
55
|
+
// Don't double-toggle if the click was on the actual input element
|
|
56
|
+
const target = event.target as HTMLElement;
|
|
57
|
+
if (target.tagName === 'INPUT') return;
|
|
58
|
+
|
|
59
|
+
handleSelectAllChange({ isChecked: !localChecked, value: '' });
|
|
60
|
+
}
|
|
61
|
+
</script>
|
|
62
|
+
|
|
63
|
+
{#if selectionMode === 'multi'}
|
|
64
|
+
<th
|
|
65
|
+
class="selection-header"
|
|
66
|
+
style="width: 48px; min-width: 48px; text-align: center;"
|
|
67
|
+
onclick={handleCellClick}
|
|
68
|
+
>
|
|
69
|
+
<div class="header-content">
|
|
70
|
+
<CheckBox
|
|
71
|
+
bind:isChecked={localChecked}
|
|
72
|
+
onChange={handleSelectAllChange}
|
|
73
|
+
inline={true}
|
|
74
|
+
ariaLabel="Select all rows"
|
|
75
|
+
/>
|
|
76
|
+
</div>
|
|
77
|
+
</th>
|
|
78
|
+
{:else if selectionMode === 'single'}
|
|
79
|
+
<th class="selection-header" style="width: 48px; min-width: 48px; text-align: center;"></th>
|
|
80
|
+
{/if}
|
|
81
|
+
|
|
82
|
+
<style>.selection-header {
|
|
83
|
+
padding: 0.5rem 0.25rem !important;
|
|
84
|
+
vertical-align: middle;
|
|
85
|
+
text-align: center;
|
|
86
|
+
cursor: pointer;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.header-content {
|
|
90
|
+
display: flex;
|
|
91
|
+
align-items: center;
|
|
92
|
+
justify-content: center;
|
|
93
|
+
width: 100%;
|
|
94
|
+
height: 100%;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
:global(.header-content .checkbox-label) {
|
|
98
|
+
margin: 0;
|
|
99
|
+
cursor: pointer;
|
|
100
|
+
display: flex;
|
|
101
|
+
align-items: center;
|
|
102
|
+
justify-content: center;
|
|
103
|
+
}</style>
|
package/dist/tables/table.svelte
CHANGED
|
@@ -6,42 +6,48 @@
|
|
|
6
6
|
|
|
7
7
|
let {
|
|
8
8
|
children,
|
|
9
|
+
rows = [],
|
|
9
10
|
enableSorting = true,
|
|
10
|
-
|
|
11
|
-
selectionMode = 'multi',
|
|
11
|
+
selectionMode = 'none',
|
|
12
12
|
rowIdKey = 'id',
|
|
13
13
|
stickyHeader = false,
|
|
14
14
|
onSort = undefined,
|
|
15
15
|
onSelectionChange = undefined
|
|
16
16
|
}: {
|
|
17
17
|
children?: Snippet;
|
|
18
|
+
rows?: JsonObject[];
|
|
18
19
|
enableSorting?: boolean;
|
|
19
|
-
|
|
20
|
-
selectionMode?: 'single' | 'multi';
|
|
20
|
+
selectionMode?: 'none' | 'single' | 'multi';
|
|
21
21
|
rowIdKey?: string;
|
|
22
22
|
stickyHeader?: boolean;
|
|
23
23
|
onSort?: (column: string, direction: 'asc' | 'desc') => void;
|
|
24
|
-
onSelectionChange?: (
|
|
24
|
+
onSelectionChange?: (selectedRows: JsonObject[]) => void;
|
|
25
25
|
} = $props();
|
|
26
26
|
|
|
27
27
|
// Create table context for child components
|
|
28
28
|
// Using untrack() to indicate we intentionally want non-reactive initial values
|
|
29
29
|
const config: TableContextConfig<JsonObject> = {
|
|
30
30
|
enableSorting: untrack(() => enableSorting),
|
|
31
|
-
enableSelection: untrack(() => enableSelection),
|
|
32
31
|
selectionMode: untrack(() => selectionMode),
|
|
33
32
|
rowIdKey: untrack(() => rowIdKey),
|
|
34
33
|
onSort: untrack(() => onSort),
|
|
35
|
-
onSelectionChange: untrack(() => onSelectionChange)
|
|
34
|
+
onSelectionChange: untrack(() => onSelectionChange),
|
|
35
|
+
rows: untrack(() => rows)
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
createTableContext(config);
|
|
38
|
+
const context = createTableContext(config);
|
|
39
|
+
|
|
40
|
+
// Keep context.config.rows updated reactively when rows prop changes
|
|
41
|
+
// This is essential for selection features to work correctly
|
|
42
|
+
$effect(() => {
|
|
43
|
+
context.config.rows = rows;
|
|
44
|
+
});
|
|
39
45
|
</script>
|
|
40
46
|
|
|
41
47
|
<table
|
|
42
48
|
class:sticky-header={stickyHeader}
|
|
43
49
|
role="grid"
|
|
44
|
-
aria-rowcount={
|
|
50
|
+
aria-rowcount={undefined}
|
|
45
51
|
aria-colcount={undefined}
|
|
46
52
|
>
|
|
47
53
|
{@render children?.()}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { JsonObject } from '../types/data.js';
|
|
2
3
|
type $$ComponentProps = {
|
|
3
4
|
children?: Snippet;
|
|
5
|
+
rows?: JsonObject[];
|
|
4
6
|
enableSorting?: boolean;
|
|
5
|
-
|
|
6
|
-
selectionMode?: 'single' | 'multi';
|
|
7
|
+
selectionMode?: 'none' | 'single' | 'multi';
|
|
7
8
|
rowIdKey?: string;
|
|
8
9
|
stickyHeader?: boolean;
|
|
9
10
|
onSort?: (column: string, direction: 'asc' | 'desc') => void;
|
|
10
|
-
onSelectionChange?: (
|
|
11
|
+
onSelectionChange?: (selectedRows: JsonObject[]) => void;
|
|
11
12
|
};
|
|
12
13
|
declare const Table: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
13
14
|
type Table = ReturnType<typeof Table>;
|