Skip to content

Commit 43889e7

Browse files
authored
Merge branch 'main' into demo-page-ally
2 parents b7e7616 + 6d1437c commit 43889e7

File tree

6 files changed

+131
-104
lines changed

6 files changed

+131
-104
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @careswitch/svelte-data-table
22

3+
## 0.5.0
4+
5+
### Minor Changes
6+
7+
- 48cfdcf: Introduce `id` in column definition to allow multiple columns for the same key
8+
39
## 0.4.0
410

511
### Minor Changes

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ _Requires Svelte 5 peer dependency_
3333
{ id: 2, name: 'Jane Doe', status: 'inactive' }
3434
],
3535
columns: [
36-
{ key: 'id', name: 'ID' },
37-
{ key: 'name', name: 'Name' },
38-
{ key: 'status', name: 'Status' }
36+
{ id: 'id', key: 'id', name: 'ID' },
37+
{ id: 'name', key: 'name', name: 'Name' },
38+
{ id: 'status', key: 'status', name: 'Status' }
3939
]
4040
});
4141
</script>
4242
4343
<table>
4444
<thead>
4545
<tr>
46-
{#each table.columns as column (column.key)}
46+
{#each table.columns as column (column.id)}
4747
<th>{column.name}</th>
4848
{/each}
4949
</tr>
5050
</thead>
5151
<tbody>
5252
{#each table.rows as row (row.id)}
5353
<tr>
54-
{#each table.columns as column (column.key)}
54+
{#each table.columns as column (column.id)}
5555
<td>{row[column.key]}</td>
5656
{/each}
5757
</tr>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@careswitch/svelte-data-table",
33
"description": "Small, fast data table library for Svelte 5 with client-side sorting, filtering, and pagination.",
4-
"version": "0.4.0",
4+
"version": "0.5.0",
55
"homepage": "https://careswitch-svelte-data-table.vercel.app",
66
"license": "MIT",
77
"keywords": [

src/index.test.ts

+42-19
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ describe('DataTable', () => {
1111
];
1212

1313
const columns = [
14-
{ key: 'id', name: 'ID', sortable: true },
15-
{ key: 'name', name: 'Name', sortable: true },
16-
{ key: 'age', name: 'Age', sortable: true }
14+
{ id: 'id', key: 'id', name: 'ID', sortable: true },
15+
{ id: 'name', key: 'name', name: 'Name', sortable: true },
16+
{ id: 'age', key: 'age', name: 'Age', sortable: true },
17+
{
18+
id: 'ageGroup',
19+
key: 'age',
20+
name: 'Age Group',
21+
sortable: true,
22+
getValue: (row) => (row.age < 30 ? 'Young' : 'Adult')
23+
}
1724
] satisfies ColumnDef<(typeof sampleData)[0]>[];
1825

1926
describe('Initialization', () => {
@@ -60,12 +67,18 @@ describe('DataTable', () => {
6067
const incompleteData = [
6168
{ id: 1, name: 'Alice' },
6269
{ id: 2, age: 25 }
63-
];
70+
] as any[];
6471
const table = new DataTable({ data: incompleteData, columns });
6572
expect(table.rows).toHaveLength(2);
6673
expect(table.rows[0].age).toBeUndefined();
6774
expect(table.rows[1].name).toBeUndefined();
6875
});
76+
77+
it('should handle multiple columns with the same key', () => {
78+
const table = new DataTable({ data: sampleData, columns });
79+
expect(table.columns).toHaveLength(4);
80+
expect(table.columns.filter((col) => col.key === 'age')).toHaveLength(2);
81+
});
6982
});
7083

7184
describe('Sorting', () => {
@@ -101,7 +114,7 @@ describe('DataTable', () => {
101114

102115
it('should handle sorting with custom sorter function', () => {
103116
const customColumns = columns.map((col) =>
104-
col.key === 'name'
117+
col.id === 'name'
105118
? {
106119
...col,
107120
sorter: (a, b) => b.name.localeCompare(a.name) // Reverse alphabetical order
@@ -136,14 +149,22 @@ describe('DataTable', () => {
136149
{ id: 1, name: 'Alice', age: null },
137150
{ id: 2, name: 'Bob', age: null },
138151
{ id: 3, name: 'Charlie', age: null }
139-
];
152+
] as any[];
140153
const table = new DataTable({ data: nullData, columns });
141154
table.toggleSort('age');
142155
expect(table.rows).toHaveLength(3);
143156
// The order should remain unchanged
144157
expect(table.rows[0].name).toBe('Alice');
145158
expect(table.rows[2].name).toBe('Charlie');
146159
});
160+
161+
it('should sort independently for columns with the same key', () => {
162+
const table = new DataTable({ data: sampleData, columns });
163+
table.toggleSort('age');
164+
const ageSortState = table.getSortState('age');
165+
const ageGroupSortState = table.getSortState('ageGroup');
166+
expect(ageSortState).not.toBe(ageGroupSortState);
167+
});
147168
});
148169

149170
describe('Filtering', () => {
@@ -167,6 +188,13 @@ describe('DataTable', () => {
167188
).toBe(true);
168189
});
169190

191+
it('should filter derived columns', () => {
192+
const table = new DataTable({ data: sampleData, columns });
193+
table.setFilter('ageGroup', ['Young']);
194+
expect(table.rows).toHaveLength(2);
195+
expect(table.rows.every((row) => row.age < 30)).toBe(true);
196+
});
197+
170198
it('should clear filter', () => {
171199
const table = new DataTable({ data: sampleData, columns });
172200
table.setFilter('age', [30]);
@@ -231,6 +259,13 @@ describe('DataTable', () => {
231259
expect(table.rows).toHaveLength(2);
232260
expect(table.rows.every((row) => row.name === 'Alice' || row.name === 'Bob')).toBe(true);
233261
});
262+
263+
it('should filter independently for columns with the same key', () => {
264+
const table = new DataTable({ data: sampleData, columns });
265+
table.setFilter('age', [30, 35]);
266+
table.setFilter('ageGroup', ['Young']);
267+
expect(table.rows).toHaveLength(0); // No rows match both filters
268+
});
234269
});
235270

236271
describe('Pagination', () => {
@@ -276,19 +311,7 @@ describe('DataTable', () => {
276311
});
277312
});
278313

279-
describe('baseRows functionality', () => {
280-
const sampleData = [
281-
{ id: 1, name: 'Alice', age: 30 },
282-
{ id: 2, name: 'Bob', age: 25 },
283-
{ id: 3, name: 'Charlie', age: 35 }
284-
];
285-
286-
const columns = [
287-
{ key: 'id', name: 'ID', sortable: true },
288-
{ key: 'name', name: 'Name', sortable: true },
289-
{ key: 'age', name: 'Age', sortable: true }
290-
] satisfies ColumnDef<(typeof sampleData)[0]>[];
291-
314+
describe('baseRows', () => {
292315
it('should return the original data when getting baseRows', () => {
293316
const table = new DataTable({ data: sampleData, columns });
294317
expect(table.baseRows).toEqual(sampleData);

0 commit comments

Comments
 (0)