Skip to content

Commit 2e3f9f1

Browse files
authored
Merge pull request #118 from risalfajar/optional-id
Make `header` as column's alternative id
2 parents bc2e096 + 88b961c commit 2e3f9f1

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

docs/src/routes/docs/[...2]api/[...3]create-columns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const columns = table.createColumns([
8383

8484
Defines the id of the data column. **Duplicate ids are not allowed** on a single table.
8585

86-
_Defaults to the value of `accessor` if a string accessor is passed_. Required if a function accessor is passed.
86+
_Defaults to the value of `accessor` if a string accessor is passed_. If a function accessor is passed, defaults to the value of `header` instead.
8787

8888
#### `columnDef.header: RenderConfig | ((headerCell, state) => RenderConfig)`
8989

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "svelte-headless-table",
33
"description": "Unopinionated and extensible data tables for Svelte",
4-
"version": "0.17.2",
4+
"version": "0.17.3",
55
"scripts": {
66
"dev": "vite dev",
77
"build": "vite build",

src/lib/columns.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export type FlatColumnInit<
5050
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5151
Id extends string = any
5252
> = Omit<ColumnInit<Item, Plugins>, 'height'> & {
53-
id: Id;
53+
id?: Id;
5454
};
5555

5656
export class FlatColumn<
@@ -65,7 +65,7 @@ export class FlatColumn<
6565
id: Id;
6666
constructor({ header, footer, plugins, id }: FlatColumnInit<Item, Plugins>) {
6767
super({ header, footer, plugins, height: 1 });
68-
this.id = id;
68+
this.id = id ?? String(header);
6969
}
7070
}
7171

@@ -96,7 +96,7 @@ export type DataColumnInitKey<Item, Id extends keyof Item> = {
9696

9797
export type DataColumnInitIdAndKey<Item, Id extends string, Key extends keyof Item> = {
9898
accessor: Key;
99-
id: Id;
99+
id?: Id;
100100
};
101101

102102
export type DataColumnInitFnAndId<Item, Id extends string, Value> = {
@@ -133,10 +133,10 @@ export class DataColumn<
133133
} else {
134134
this.accessorKey = accessor;
135135
}
136-
if (id === undefined && this.accessorKey === undefined) {
137-
throw new Error('A column id or string accessor is required');
136+
if (id === undefined && this.accessorKey === undefined && header === undefined) {
137+
throw new Error('A column id, string accessor, or header is required');
138138
}
139-
this.id = (id ?? String(this.accessorKey)) as Id;
139+
this.id = (id ?? this.accessorKey ? String(this.accessorKey) : String(header)) as Id;
140140
}
141141

142142
// eslint-disable-next-line @typescript-eslint/no-explicit-any

src/lib/createTable.createColumns.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,41 @@ it('throws if two columns have the same id', () => {
4343
]);
4444
}).toThrowError('Duplicate column ids not allowed: "firstName"');
4545
});
46+
47+
it('using headers as id, passes if no duplicate headers', () => {
48+
expect(() => {
49+
table.createColumns([
50+
table.column({
51+
header: 'First Name',
52+
accessor: (item) => item.firstName
53+
}),
54+
table.column({
55+
header: 'Last Name',
56+
accessor: (item) => item.lastName
57+
}),
58+
table.display({
59+
header: 'Actions',
60+
cell: () => ''
61+
}),
62+
])
63+
}).not.toThrow()
64+
});
65+
66+
it('using headers as id, throws if two columns have the same headers', () => {
67+
expect(() => {
68+
table.createColumns([
69+
table.column({
70+
header: 'First Name',
71+
accessor: (item) => item.firstName
72+
}),
73+
table.column({
74+
header: 'Last Name',
75+
accessor: (item) => item.lastName
76+
}),
77+
table.display({
78+
header: 'First Name',
79+
cell: () => ''
80+
}),
81+
])
82+
}).toThrowError('Duplicate column ids not allowed: "First Name"')
83+
});

0 commit comments

Comments
 (0)