Skip to content

Commit c4f2909

Browse files
committed
feat: migrate components in data table to runes
1 parent e53dda1 commit c4f2909

14 files changed

+343
-221
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Svelte 5 Runes mode is being migrated to slowly. This is the todo list of compon
160160
- [x] Banner
161161
- [x] Card
162162
- [x] Common
163-
- [ ] Data Table
163+
- [x] Data Table
164164
- [x] Dialog
165165
- [x] Drawer
166166
- [x] Image List

packages/data-table/src/Body.svelte

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<svelte:options runes={false} />
1+
<svelte:options runes />
22

33
<tbody
44
bind:this={element}
@@ -7,10 +7,11 @@
77
[className]: true,
88
'mdc-data-table__content': true,
99
})}
10-
{...$$restProps}><slot /></tbody
10+
{...restProps}>{@render children?.()}</tbody
1111
>
1212

1313
<script lang="ts">
14+
import type { Snippet } from 'svelte';
1415
import { onMount, setContext, getContext } from 'svelte';
1516
import type { SmuiAttrs } from '@smui/common';
1617
import type { ActionArray } from '@smui/common/internal';
@@ -20,22 +21,29 @@
2021
import type { SMUIDataTableBodyAccessor } from './Body.types.js';
2122
2223
type OwnProps = {
24+
/**
25+
* An array of Action or [Action, ActionProps] to be applied to the element.
26+
*/
2327
use?: ActionArray;
28+
/**
29+
* A space separated list of CSS classes.
30+
*/
2431
class?: string;
25-
};
26-
type $$Props = OwnProps & SmuiAttrs<'tbody', keyof OwnProps>;
2732
28-
// Remember to update $$Props if you add/remove/rename props.
29-
export let use: ActionArray = [];
30-
let className = '';
31-
export { className as class };
33+
children?: Snippet;
34+
};
35+
let {
36+
use = [],
37+
class: className = '',
38+
children,
39+
...restProps
40+
}: OwnProps & SmuiAttrs<'tbody', keyof OwnProps> = $props();
3241
3342
let element: HTMLTableSectionElement;
34-
let rows: SMUIDataTableRowAccessor[] = [];
35-
const rowAccessorMap = new WeakMap<
36-
HTMLTableRowElement,
37-
SMUIDataTableRowAccessor
38-
>();
43+
let rows: SMUIDataTableRowAccessor[] = $state([]);
44+
const rowAccessorMap = $state(
45+
new WeakMap<HTMLTableRowElement, SMUIDataTableRowAccessor>(),
46+
);
3947
4048
setContext('SMUI:data-table:row:header', false);
4149

packages/data-table/src/Cell.svelte

+52-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<svelte:options runes={false} />
1+
<svelte:options runes />
22

33
{#if header}
44
<th
@@ -22,16 +22,16 @@
2222
: 'none'
2323
: undefined}
2424
{...internalAttrs}
25-
{...$$restProps}
25+
{...restProps}
2626
onchange={(e) => {
2727
if (checkbox) {
2828
notifyHeaderChange(e);
2929
}
30-
$$restProps.onchange?.(e);
30+
restProps.onchange?.(e);
3131
}}
3232
>{#if sortable}
3333
<div class="mdc-data-table__header-cell-wrapper">
34-
<slot />
34+
{@render children?.()}
3535
<div
3636
class="mdc-data-table__sort-status-label"
3737
aria-hidden="true"
@@ -44,7 +44,7 @@
4444
: ''}
4545
</div>
4646
</div>
47-
{:else}<slot />{/if}</th
47+
{:else}{@render children?.()}{/if}</th
4848
>
4949
{:else}
5050
<td
@@ -58,13 +58,13 @@
5858
...internalClasses,
5959
})}
6060
{...internalAttrs}
61-
{...$$restProps}
61+
{...restProps}
6262
onchange={(e) => {
6363
if (checkbox) {
6464
notifyBodyChange(e);
6565
}
66-
$$restProps.onchange?.(e);
67-
}}><slot /></td
66+
restProps.onchange?.(e);
67+
}}>{@render children?.()}</td
6868
>
6969
{/if}
7070

@@ -74,6 +74,7 @@
7474

7575
<script lang="ts">
7676
import type { SortValue } from '@material/data-table';
77+
import type { Snippet } from 'svelte';
7778
import { onMount, getContext, setContext } from 'svelte';
7879
import type { Writable } from 'svelte/store';
7980
import type { SmuiAttrs } from '@smui/common';
@@ -82,34 +83,59 @@
8283
8384
import type { SMUIDataTableCellAccessor } from './Cell.types.js';
8485
86+
let header = getContext<boolean>('SMUI:data-table:row:header');
87+
8588
type OwnProps = {
89+
/**
90+
* An array of Action or [Action, ActionProps] to be applied to the element.
91+
*/
8692
use?: ActionArray;
93+
/**
94+
* A space separated list of CSS classes.
95+
*/
8796
class?: string;
97+
/**
98+
* Whether to apply numeric column styling.
99+
*/
88100
numeric?: boolean;
101+
/**
102+
* Whether this cell contains the checkbox to select.
103+
*/
89104
checkbox?: boolean;
105+
/**
106+
* The column ID for the column this cell is a header for.
107+
*
108+
* You only need this on sortable columns and you only need to put it on the
109+
* cell in the header.
110+
*/
90111
columnId?: string;
112+
/**
113+
* Whether sorting is enabled on the column this cell is a header for.
114+
*
115+
* This will default to true if the data table is sortable.
116+
*/
91117
sortable?: boolean;
92-
};
93-
type $$Props = SmuiAttrs<'th', keyof OwnProps> &
94-
SmuiAttrs<'td', keyof OwnProps> &
95-
OwnProps;
96118
97-
let header = getContext<boolean>('SMUI:data-table:row:header');
98-
99-
// Remember to update $$Props if you add/remove/rename props.
100-
export let use: ActionArray = [];
101-
let className = '';
102-
export { className as class };
103-
export let numeric = false;
104-
export let checkbox = false;
105-
export let columnId = header
106-
? 'SMUI-data-table-column-' + counter++
107-
: 'SMUI-data-table-unused';
108-
export let sortable = getContext<boolean>('SMUI:data-table:sortable');
119+
children?: Snippet;
120+
};
121+
let {
122+
use = [],
123+
class: className = '',
124+
numeric = false,
125+
checkbox = false,
126+
columnId = header
127+
? 'SMUI-data-table-column-' + counter++
128+
: 'SMUI-data-table-unused',
129+
sortable = getContext<boolean>('SMUI:data-table:sortable'),
130+
children,
131+
...restProps
132+
}: OwnProps &
133+
SmuiAttrs<'th', keyof OwnProps> &
134+
SmuiAttrs<'td', keyof OwnProps> = $props();
109135
110136
let element: HTMLTableCellElement;
111-
let internalClasses: { [k: string]: boolean } = {};
112-
let internalAttrs: { [k: string]: string | undefined } = {};
137+
let internalClasses: { [k: string]: boolean } = $state({});
138+
let internalAttrs: { [k: string]: string | undefined } = $state({});
113139
let sort = getContext<Writable<string | null>>('SMUI:data-table:sort');
114140
let sortDirection = getContext<Writable<SortValue>>(
115141
'SMUI:data-table:sortDirection',

0 commit comments

Comments
 (0)