sveltacular 1.0.15 → 1.0.17

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.
@@ -12,6 +12,7 @@
12
12
  name = undefined,
13
13
  onChange = undefined,
14
14
  label,
15
+ ariaLabel,
15
16
  children,
16
17
  size = 'full' as FormFieldSizeOptions,
17
18
  helperText = undefined,
@@ -25,6 +26,7 @@
25
26
  name?: string | undefined;
26
27
  onChange?: ((data: { isChecked: boolean; value: string }) => void) | undefined;
27
28
  label?: string;
29
+ ariaLabel?: string;
28
30
  children?: Snippet;
29
31
  size?: FormFieldSizeOptions;
30
32
  helperText?: string;
@@ -33,6 +35,9 @@
33
35
  inline?: boolean;
34
36
  } = $props();
35
37
 
38
+ // Use ariaLabel if provided, otherwise fall back to label for accessibility
39
+ let inputAriaLabel = $derived(ariaLabel ?? label);
40
+
36
41
  const id = uniqueId();
37
42
 
38
43
  const onChecked = (event: Event) => {
@@ -56,18 +61,21 @@
56
61
  bind:checked={isChecked}
57
62
  onchange={onChecked}
58
63
  {required}
64
+ aria-label={inputAriaLabel}
59
65
  />
60
66
  <span class="checkbox">
61
67
  <span class="checkmark"><Icon type="check" size="sm" fill="#fff" mask /></span>
62
68
  </span>
63
- {#if children}
64
- <div class="text">
65
- {@render children()}
66
- </div>
67
- {:else if label}
68
- <div class="text">
69
- {label}
70
- </div>
69
+ {#if !ariaLabel}
70
+ {#if children}
71
+ <div class="text">
72
+ {@render children()}
73
+ </div>
74
+ {:else if label}
75
+ <div class="text">
76
+ {label}
77
+ </div>
78
+ {/if}
71
79
  {/if}
72
80
  </label>
73
81
  {:else}
@@ -82,6 +90,7 @@
82
90
  bind:checked={isChecked}
83
91
  onchange={onChecked}
84
92
  {required}
93
+ aria-label={inputAriaLabel}
85
94
  />
86
95
  <span class="checkbox">
87
96
  <span class="checkmark"> <Icon type="check" size="sm" fill="#fff" mask /></span>
@@ -123,7 +132,7 @@
123
132
  user-select: none;
124
133
  }
125
134
  .checkbox-label .checkbox .checkmark {
126
- display: block;
135
+ display: none;
127
136
  position: absolute;
128
137
  top: 0;
129
138
  left: 0;
@@ -145,6 +154,7 @@
145
154
  border-color: var(--form-input-border);
146
155
  }
147
156
  .checkbox-label input:checked + .checkbox .checkmark {
157
+ display: block;
148
158
  width: 100%;
149
159
  height: 100%;
150
160
  }</style>
@@ -11,6 +11,7 @@ type $$ComponentProps = {
11
11
  value: string;
12
12
  }) => void) | undefined;
13
13
  label?: string;
14
+ ariaLabel?: string;
14
15
  children?: Snippet;
15
16
  size?: FormFieldSizeOptions;
16
17
  helperText?: string;
@@ -9,12 +9,14 @@
9
9
  value = undefined as RadioValue,
10
10
  group = $bindable(undefined as string | undefined),
11
11
  disabled = false,
12
+ ariaLabel,
12
13
  children = undefined,
13
14
  onChange = undefined
14
15
  }: {
15
16
  value?: RadioValue;
16
17
  group?: string | undefined;
17
18
  disabled?: boolean;
19
+ ariaLabel?: string;
18
20
  children?: Snippet;
19
21
  onChange?: ((value: string) => void) | undefined;
20
22
  } = $props();
@@ -29,12 +31,13 @@
29
31
  {value}
30
32
  {disabled}
31
33
  {id}
34
+ aria-label={ariaLabel}
32
35
  onchange={() => onChange?.(String(value || ''))}
33
36
  />
34
- <span class="checkbox">
35
- <span class="checkmark"><Icon type="check" size="sm" /></span>
37
+ <span class="radio-circle">
38
+ <span class="radio-dot"></span>
36
39
  </span>
37
- {#if children}
40
+ {#if !ariaLabel && children}
38
41
  <div class="text">
39
42
  {@render children?.()}
40
43
  </div>
@@ -49,43 +52,35 @@
49
52
  font-size: 1rem;
50
53
  cursor: pointer;
51
54
  }
52
- label .checkbox {
55
+ label .radio-circle {
53
56
  position: relative;
54
57
  width: 1.2rem;
55
58
  height: 1.2rem;
56
- border-radius: 0.6rem;
59
+ border-radius: 50%;
57
60
  border: 1px solid var(--form-input-border, black);
58
61
  background-color: var(--form-input-bg, white);
59
- color: var(--form-input-fg, black);
60
- font-size: 0.875rem;
61
- font-weight: 500;
62
- line-height: 1.25rem;
63
- transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out, color 0.2s ease-in-out, fill 0.2s ease-in-out, stroke 0.2s ease-in-out;
62
+ transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out;
64
63
  user-select: none;
65
64
  display: flex;
66
65
  align-items: center;
67
66
  justify-content: center;
68
- padding-top: 0.1rem;
69
67
  }
70
- label .checkbox .checkmark {
71
- display: block;
72
- width: 0;
73
- height: 0;
74
- line-height: 100%;
75
- color: var(--form-input-selected-fg, white);
76
- fill: var(--form-input-selected-bg, #3182ce);
77
- stroke: var(--form-input-selected-fg, white);
78
- transition: width 0.2s ease-in-out, height 0.2s ease-in-out;
68
+ label .radio-circle .radio-dot {
69
+ display: none;
70
+ width: 0.5rem;
71
+ height: 0.5rem;
72
+ border-radius: 50%;
73
+ background-color: var(--form-input-selected-bg, #3182ce);
74
+ transition: opacity 0.2s ease-in-out;
79
75
  }
80
76
  label input {
81
77
  width: 0;
82
78
  height: 0;
83
79
  position: absolute;
84
80
  }
85
- label input:checked + .checkbox {
86
- background-color: var(--form-input-selected-bg, #3182ce);
81
+ label input:checked + .radio-circle {
82
+ border-color: var(--form-input-selected-bg, #3182ce);
87
83
  }
88
- label input:checked + .checkbox .checkmark {
89
- width: 100%;
90
- height: 100%;
84
+ label input:checked + .radio-circle .radio-dot {
85
+ display: block;
91
86
  }</style>
@@ -4,6 +4,7 @@ type $$ComponentProps = {
4
4
  value?: RadioValue;
5
5
  group?: string | undefined;
6
6
  disabled?: boolean;
7
+ ariaLabel?: string;
7
8
  children?: Snippet;
8
9
  onChange?: ((value: string) => void) | undefined;
9
10
  };
@@ -4,6 +4,8 @@
4
4
  import TableHeader from './table-header.svelte';
5
5
  import TableRow from './table-row.svelte';
6
6
  import Table from './table.svelte';
7
+ import TableSelectionCell from './table-selection-cell.svelte';
8
+ import TableSelectionHeaderCell from './table-selection-header-cell.svelte';
7
9
  import type { ColumnDef, JsonObject, PaginationProperties } from '../types/data.js';
8
10
  import Button from '../forms/button/button.svelte';
9
11
  import DropdownItem from '../generic/dropdown-item/dropdown-item.svelte';
@@ -21,21 +23,25 @@
21
23
  getCellTypeClass,
22
24
  sortRows
23
25
  } from './cell-renderers.js';
24
- import { getTableContext } from './table-context.svelte.js';
25
26
  import type { Snippet } from 'svelte';
26
27
  import { useVirtualList } from '../helpers/use-virtual-list.svelte.js';
27
- import { onMount } from 'svelte';
28
+ import type { ButtonVariant, FormFieldSizeOptions } from '../types/form.js';
28
29
 
29
30
  type PaginationEvent = (pagination: PaginationProperties) => void;
30
31
 
31
32
  interface Action {
32
33
  text: string;
33
- onClick: (row: JsonObject) => unknown;
34
+ variant?: ButtonVariant;
35
+ href?: (row: JsonObject) => string;
36
+ onClick?: (row: JsonObject) => unknown;
34
37
  }
35
38
 
36
39
  interface Actions {
37
40
  text?: string;
38
- type?: string;
41
+ type?: 'buttons' | 'dropdown';
42
+ variant?: ButtonVariant | 'default';
43
+ size?: FormFieldSizeOptions;
44
+ align?: 'left' | 'center' | 'right';
39
45
  items: Action[];
40
46
  }
41
47
 
@@ -48,12 +54,12 @@
48
54
  actions = undefined,
49
55
  stickyHeader = false,
50
56
  enableSorting = true,
51
- enableSelection = false,
52
- selectionMode = 'multi',
57
+ selectionMode = 'none',
53
58
  rowIdKey = 'id',
54
59
  onPageChange = null,
55
60
  onSort = undefined,
56
61
  onSelectionChange = undefined,
62
+ selectedCount = $bindable(0),
57
63
  children = undefined,
58
64
  virtualScroll = false,
59
65
  rowHeight = 48,
@@ -67,12 +73,12 @@
67
73
  actions?: Actions;
68
74
  stickyHeader?: boolean;
69
75
  enableSorting?: boolean;
70
- enableSelection?: boolean;
71
- selectionMode?: 'single' | 'multi';
76
+ selectionMode?: 'none' | 'single' | 'multi';
72
77
  rowIdKey?: string;
73
78
  onPageChange?: PaginationEvent | null;
74
79
  onSort?: (column: string, direction: 'asc' | 'desc') => void;
75
- onSelectionChange?: (selectedIds: Set<string | number>) => void;
80
+ onSelectionChange?: (selectedRows: JsonObject[]) => void;
81
+ selectedCount?: number;
76
82
  children?: Snippet;
77
83
  virtualScroll?: boolean;
78
84
  rowHeight?: number;
@@ -108,8 +114,24 @@
108
114
 
109
115
  // Computed values
110
116
  let hasActionCol = $derived(actions?.items && actions.items.length > 0);
117
+ let hasSelectionCol = $derived(selectionMode !== 'none');
111
118
  let visibleCols = $derived(cols.filter((col) => !col.hidden));
112
- let colCount = $derived(Math.max(1, visibleCols.length) + (hasActionCol ? 1 : 0));
119
+ let colCount = $derived(
120
+ Math.max(1, visibleCols.length) + (hasActionCol ? 1 : 0) + (hasSelectionCol ? 1 : 0)
121
+ );
122
+ let actionButtonVariant = $derived.by(() => {
123
+ return !actions?.variant || actions.variant === 'default' ? 'outline' : actions.variant;
124
+ });
125
+ let actionButtonSize = $derived(actions?.size ?? 'sm');
126
+ let actionAlign = $derived(actions?.align ?? 'center');
127
+
128
+ // Track selected count from selection change callbacks
129
+ let internalSelectedCount = $state(0);
130
+
131
+ // Sync selectedCount with internal tracking
132
+ $effect(() => {
133
+ selectedCount = internalSelectedCount;
134
+ });
113
135
 
114
136
  // Manage sort state directly in DataGrid (not via context)
115
137
  let currentSortColumn = $state<string | null>(null);
@@ -181,13 +203,23 @@
181
203
  </script>
182
204
 
183
205
  <Table
206
+ rows={filteredRows ?? []}
184
207
  {stickyHeader}
185
208
  enableSorting={false}
186
- {enableSelection}
187
209
  {selectionMode}
188
210
  {rowIdKey}
189
211
  onSort={handleSortChange}
190
- {onSelectionChange}
212
+ onSelectionChange={(selectedRowIds) => {
213
+ // Convert selected IDs to actual row objects
214
+ // Use sortedRows (before pagination) to include all selected rows, not just current page
215
+ const selectedRowObjects = (sortedRows ?? []).filter((row) => {
216
+ const id = row[rowIdKey] as string | number;
217
+ return id !== undefined && selectedRowIds.includes(id);
218
+ });
219
+ onSelectionChange?.(selectedRowObjects);
220
+ // Track selected count from the callback
221
+ internalSelectedCount = selectedRowIds.length;
222
+ }}
191
223
  >
192
224
  {#if children}
193
225
  <TableCaption side={captionSide} align={captionAlign}>{@render children?.()}</TableCaption>
@@ -195,6 +227,9 @@
195
227
 
196
228
  <TableHeader sticky={stickyHeader}>
197
229
  <tr>
230
+ {#if hasSelectionCol}
231
+ <TableSelectionHeaderCell />
232
+ {/if}
198
233
  {#each visibleCols as col}
199
234
  <TableHeaderCell
200
235
  type={col.type}
@@ -210,7 +245,7 @@
210
245
  </TableHeaderCell>
211
246
  {/each}
212
247
  {#if hasActionCol}
213
- <TableHeaderCell type="actions">Actions</TableHeaderCell>
248
+ <TableHeaderCell type="actions" align={actionAlign}>Actions</TableHeaderCell>
214
249
  {/if}
215
250
  </tr>
216
251
  </TableHeader>
@@ -245,7 +280,10 @@
245
280
  <div
246
281
  style="position: absolute; top: {vItem.offsetTop}px; height: {vItem.height}px; width: 100%; display: table; table-layout: fixed;"
247
282
  >
248
- <TableRow {row} rowIndex={index} selectable={enableSelection}>
283
+ <TableRow {row} rowIndex={index} selectable={hasSelectionCol}>
284
+ {#if hasSelectionCol}
285
+ <TableSelectionCell {row} rowIndex={index} />
286
+ {/if}
249
287
  {#each visibleCols as col}
250
288
  {@const cellValue = formatCell(row, col)}
251
289
  {@const cellLink = getCellLink(row, col)}
@@ -263,26 +301,32 @@
263
301
  </TableCell>
264
302
  {/each}
265
303
  {#if hasActionCol && actions}
266
- <TableCell type="actions">
304
+ <TableCell type="actions" align={actionAlign}>
267
305
  {#if actions.type === 'dropdown'}
268
306
  <DropdownButton text={actions.text ?? ''} variant="ghost">
269
307
  {#each actions.items as action}
270
- <DropdownItem onClick={() => action.onClick(row)}
308
+ <DropdownItem
309
+ href={action.href ? action.href(row) : undefined}
310
+ onClick={action.onClick ? () => action.onClick?.(row) : undefined}
271
311
  >{action.text}</DropdownItem
272
312
  >
273
313
  {/each}
274
314
  </DropdownButton>
275
315
  {:else}
276
- {#each actions.items as action}
277
- <Button
278
- collapse={true}
279
- size="sm"
280
- type="button"
281
- variant={actions.type === 'outline' ? 'outline' : 'secondary'}
282
- onClick={() => action.onClick(row)}
283
- label={action.text}
284
- />
285
- {/each}
316
+ <div class="actions">
317
+ {#each actions.items as action}
318
+ {@const buttonVariant = action.variant ?? actionButtonVariant}
319
+ <Button
320
+ collapse={true}
321
+ type="button"
322
+ variant={buttonVariant}
323
+ size={actionButtonSize}
324
+ href={action.href ? action.href(row) : undefined}
325
+ onClick={action.onClick ? () => action.onClick?.(row) : undefined}
326
+ label={action.text}
327
+ />
328
+ {/each}
329
+ </div>
286
330
  {/if}
287
331
  </TableCell>
288
332
  {/if}
@@ -294,7 +338,10 @@
294
338
  {:else}
295
339
  <!-- Regular rendering mode -->
296
340
  {#each filteredRows as row, index}
297
- <TableRow {row} rowIndex={index} selectable={enableSelection}>
341
+ <TableRow {row} rowIndex={index} selectable={hasSelectionCol}>
342
+ {#if hasSelectionCol}
343
+ <TableSelectionCell {row} rowIndex={index} />
344
+ {/if}
298
345
  {#each visibleCols as col}
299
346
  {@const cellValue = formatCell(row, col)}
300
347
  {@const cellLink = getCellLink(row, col)}
@@ -312,24 +359,31 @@
312
359
  </TableCell>
313
360
  {/each}
314
361
  {#if hasActionCol && actions}
315
- <TableCell type="actions">
362
+ <TableCell type="actions" align={actionAlign}>
316
363
  {#if actions.type === 'dropdown'}
317
364
  <DropdownButton text={actions.text ?? ''} variant="ghost">
318
365
  {#each actions.items as action}
319
- <DropdownItem onClick={() => action.onClick(row)}>{action.text}</DropdownItem>
366
+ <DropdownItem
367
+ href={action.href ? action.href(row) : undefined}
368
+ onClick={action.onClick ? () => action.onClick?.(row) : undefined}
369
+ >{action.text}</DropdownItem
370
+ >
320
371
  {/each}
321
372
  </DropdownButton>
322
373
  {:else}
323
- {#each actions.items as action}
324
- <Button
325
- collapse={true}
326
- size="sm"
327
- type="button"
328
- variant={actions.type === 'outline' ? 'outline' : 'secondary'}
329
- onClick={() => action.onClick(row)}
330
- label={action.text}
331
- />
332
- {/each}
374
+ <div class="actions">
375
+ {#each actions.items as action}
376
+ {@const buttonVariant = action.variant ?? actionButtonVariant}
377
+ <Button
378
+ type="button"
379
+ variant={buttonVariant}
380
+ size={actionButtonSize}
381
+ href={action.href ? action.href(row) : undefined}
382
+ onClick={action.onClick ? () => action.onClick?.(row) : undefined}
383
+ label={action.text}
384
+ />
385
+ {/each}
386
+ </div>
333
387
  {/if}
334
388
  </TableCell>
335
389
  {/if}
@@ -388,4 +442,9 @@ td.footer-cell :global(.pagination) {
388
442
  display: flex;
389
443
  justify-content: center;
390
444
  align-items: center;
445
+ }
446
+
447
+ .actions {
448
+ display: flex;
449
+ gap: 0.5rem;
391
450
  }</style>
@@ -1,13 +1,19 @@
1
1
  import type { ColumnDef, JsonObject, PaginationProperties } from '../types/data.js';
2
2
  import type { Snippet } from 'svelte';
3
+ import type { ButtonVariant, FormFieldSizeOptions } from '../types/form.js';
3
4
  type PaginationEvent = (pagination: PaginationProperties) => void;
4
5
  interface Action {
5
6
  text: string;
6
- onClick: (row: JsonObject) => unknown;
7
+ variant?: ButtonVariant;
8
+ href?: (row: JsonObject) => string;
9
+ onClick?: (row: JsonObject) => unknown;
7
10
  }
8
11
  interface Actions {
9
12
  text?: string;
10
- type?: string;
13
+ type?: 'buttons' | 'dropdown';
14
+ variant?: ButtonVariant | 'default';
15
+ size?: FormFieldSizeOptions;
16
+ align?: 'left' | 'center' | 'right';
11
17
  items: Action[];
12
18
  }
13
19
  type $$ComponentProps = {
@@ -19,17 +25,17 @@ type $$ComponentProps = {
19
25
  actions?: Actions;
20
26
  stickyHeader?: boolean;
21
27
  enableSorting?: boolean;
22
- enableSelection?: boolean;
23
- selectionMode?: 'single' | 'multi';
28
+ selectionMode?: 'none' | 'single' | 'multi';
24
29
  rowIdKey?: string;
25
30
  onPageChange?: PaginationEvent | null;
26
31
  onSort?: (column: string, direction: 'asc' | 'desc') => void;
27
- onSelectionChange?: (selectedIds: Set<string | number>) => void;
32
+ onSelectionChange?: (selectedRows: JsonObject[]) => void;
33
+ selectedCount?: number;
28
34
  children?: Snippet;
29
35
  virtualScroll?: boolean;
30
36
  rowHeight?: number;
31
37
  maxHeight?: string;
32
38
  };
33
- declare const DataGrid: import("svelte").Component<$$ComponentProps, {}, "">;
39
+ declare const DataGrid: import("svelte").Component<$$ComponentProps, {}, "selectedCount">;
34
40
  type DataGrid = ReturnType<typeof DataGrid>;
35
41
  export default DataGrid;
@@ -1,27 +1,31 @@
1
1
  import type { JsonObject, SortDirection, SortState } from '../types/data.js';
2
2
  export interface TableContextConfig<T extends JsonObject = JsonObject> {
3
3
  enableSorting?: boolean;
4
- enableSelection?: boolean;
5
- selectionMode?: 'single' | 'multi';
4
+ selectionMode?: 'none' | 'single' | 'multi';
6
5
  rowIdKey?: keyof T & string;
7
6
  onSort?: (column: string, direction: SortDirection) => void;
8
- onSelectionChange?: (selectedIds: Set<string | number>) => void;
7
+ onSelectionChange?: (selectedRowIds: (string | number)[]) => void;
8
+ rows?: T[];
9
9
  }
10
10
  export declare class TableContext<T extends JsonObject = JsonObject> {
11
11
  sortColumn: string | null;
12
12
  sortDirection: SortDirection;
13
13
  selectedIds: Set<string | number>;
14
14
  lastSelectedIndex: number | null;
15
+ radioGroup: string | undefined;
16
+ selectedCount: number;
15
17
  config: TableContextConfig<T>;
16
18
  constructor(config?: TableContextConfig<T>);
17
19
  toggleSort(columnKey: string): void;
18
20
  setSortColumn(columnKey: string | null, direction?: SortDirection): void;
19
21
  clearSort(): void;
20
22
  getSortState(): SortState;
21
- toggleRow(id: string | number, index: number, shiftKey?: boolean): void;
23
+ toggleRow(id: string | number, index: number, shiftKey?: boolean, rows?: T[]): void;
24
+ private notifySelectionChange;
22
25
  selectRange(startIndex: number, endIndex: number, rows?: T[]): void;
23
26
  selectAll(rows: T[]): void;
24
- deselectAll(): void;
27
+ deselectAll(rows?: T[]): void;
28
+ setRadioSelection(value: string | undefined, rows?: T[]): void;
25
29
  isRowSelected(id: string | number): boolean;
26
30
  isAllSelected(rows: T[]): boolean;
27
31
  isSomeSelected(rows: T[]): boolean;
@@ -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
- enableSelection: false,
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 (!this.config.enableSelection)
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.config.onSelectionChange?.(new Set(this.selectedIds));
101
+ this.notifySelectionChange(rows);
102
+ }
103
+ notifySelectionChange(rows) {
104
+ if (this.config.onSelectionChange) {
105
+ const selectedRowIds = Array.from(this.selectedIds);
106
+ this.config.onSelectionChange(selectedRowIds);
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
- this.config.onSelectionChange?.(new Set(this.selectedIds));
123
+ // Trigger reactivity
124
+ this.selectedIds = new Set(this.selectedIds);
125
+ this.notifySelectionChange(rows);
107
126
  }
108
127
  selectAll(rows) {
109
- if (!this.config.enableSelection)
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
- this.config.onSelectionChange?.(new Set(this.selectedIds));
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.config.onSelectionChange?.(new Set(this.selectedIds));
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
  }
@@ -22,36 +22,11 @@
22
22
  row && context?.config.rowIdKey ? (row[context.config.rowIdKey] as string | number) : undefined
23
23
  );
24
24
 
25
- let isSelected = $derived(rowId !== undefined && context ? context.isRowSelected(rowId) : false);
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,149 @@
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
+ const currentTarget = event.currentTarget as HTMLElement;
82
+
83
+ // If the click is on the label or any of its children (input, span, etc.),
84
+ // let the component's native handlers take care of it
85
+ const label = currentTarget.querySelector('label');
86
+ if (label && (target === label || label.contains(target))) {
87
+ // For single selection, we need to handle deselection ourselves
88
+ // since native radio buttons don't allow unchecking
89
+ if (selectionMode === 'single' && rowId !== undefined) {
90
+ // Check if already selected and toggle off if so
91
+ if (context?.radioGroup === String(rowId)) {
92
+ event.preventDefault();
93
+ handleRadioChange(String(rowId));
94
+ }
95
+ // If not selected, let the native handler select it
96
+ }
97
+ // For multi-selection, always let the native checkbox handler work
98
+ return;
99
+ }
100
+
101
+ // Only handle cell click if clicking directly on the cell (not on the label/input)
102
+ if (target === currentTarget) {
103
+ if (selectionMode === 'multi') {
104
+ handleCheckboxChange({ isChecked: !localChecked, value: '' });
105
+ } else if (selectionMode === 'single' && rowId !== undefined) {
106
+ handleRadioChange(String(rowId));
107
+ }
108
+ }
109
+ }
110
+ </script>
111
+
112
+ {#if selectionMode === 'multi'}
113
+ <td class="selection-cell" role="gridcell" onclick={handleCellClick}>
114
+ <CheckBox
115
+ bind:isChecked={localChecked}
116
+ onChange={handleCheckboxChange}
117
+ inline={true}
118
+ ariaLabel="Select row"
119
+ />
120
+ </td>
121
+ {:else if selectionMode === 'single'}
122
+ <td class="selection-cell" role="gridcell" onclick={handleCellClick}>
123
+ <RadioBox
124
+ value={String(rowId ?? '')}
125
+ bind:group={radioGroupLocal}
126
+ onChange={handleRadioChange}
127
+ ariaLabel="Select row"
128
+ />
129
+ </td>
130
+ {/if}
131
+
132
+ <style>.selection-cell {
133
+ text-align: center;
134
+ padding: 0.5rem 0.25rem;
135
+ width: 48px;
136
+ min-width: 48px;
137
+ vertical-align: middle;
138
+ cursor: pointer;
139
+ display: table-cell;
140
+ }
141
+
142
+ :global(.selection-cell .checkbox-label),
143
+ :global(.selection-cell label) {
144
+ margin: 0 auto;
145
+ cursor: pointer;
146
+ display: flex;
147
+ align-items: center;
148
+ justify-content: center;
149
+ }</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,113 @@
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
+ const target = event.target as HTMLElement;
56
+ const currentTarget = event.currentTarget as HTMLElement;
57
+
58
+ // If the click is on the label or any of its children (input, span, etc.),
59
+ // let the component's native handlers take care of it
60
+ const label = currentTarget.querySelector('label');
61
+ if (label && (target === label || label.contains(target))) {
62
+ // Let the native checkbox handler work
63
+ return;
64
+ }
65
+
66
+ // Only handle cell click if clicking directly on the cell (not on the label/input)
67
+ if (target === currentTarget) {
68
+ handleSelectAllChange({ isChecked: !localChecked, value: '' });
69
+ }
70
+ }
71
+ </script>
72
+
73
+ {#if selectionMode === 'multi'}
74
+ <th
75
+ class="selection-header"
76
+ style="width: 48px; min-width: 48px; text-align: center;"
77
+ onclick={handleCellClick}
78
+ >
79
+ <div class="header-content">
80
+ <CheckBox
81
+ bind:isChecked={localChecked}
82
+ onChange={handleSelectAllChange}
83
+ inline={true}
84
+ ariaLabel="Select all rows"
85
+ />
86
+ </div>
87
+ </th>
88
+ {:else if selectionMode === 'single'}
89
+ <th class="selection-header" style="width: 48px; min-width: 48px; text-align: center;"></th>
90
+ {/if}
91
+
92
+ <style>.selection-header {
93
+ padding: 0.5rem 0.25rem !important;
94
+ vertical-align: middle;
95
+ text-align: center;
96
+ cursor: pointer;
97
+ }
98
+
99
+ .header-content {
100
+ display: flex;
101
+ align-items: center;
102
+ justify-content: center;
103
+ width: 100%;
104
+ height: 100%;
105
+ }
106
+
107
+ :global(.header-content .checkbox-label) {
108
+ margin: 0;
109
+ cursor: pointer;
110
+ display: flex;
111
+ align-items: center;
112
+ justify-content: center;
113
+ }</style>
@@ -0,0 +1,3 @@
1
+ declare const TableSelectionHeaderCell: import("svelte").Component<Record<string, never>, {}, "">;
2
+ type TableSelectionHeaderCell = ReturnType<typeof TableSelectionHeaderCell>;
3
+ export default TableSelectionHeaderCell;
@@ -6,42 +6,48 @@
6
6
 
7
7
  let {
8
8
  children,
9
+ rows = [],
9
10
  enableSorting = true,
10
- enableSelection = false,
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
- enableSelection?: boolean;
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?: (selectedIds: Set<string | number>) => void;
24
+ onSelectionChange?: (selectedRowIds: (string | number)[]) => 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={enableSelection ? undefined : undefined}
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
- enableSelection?: boolean;
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?: (selectedIds: Set<string | number>) => void;
11
+ onSelectionChange?: (selectedRowIds: (string | number)[]) => void;
11
12
  };
12
13
  declare const Table: import("svelte").Component<$$ComponentProps, {}, "">;
13
14
  type Table = ReturnType<typeof Table>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltacular",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "A Svelte component library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",