Skip to content

Commit

Permalink
Merge pull request #118 from risalfajar/optional-id
Browse files Browse the repository at this point in the history
Make `header` as column's alternative id
  • Loading branch information
bryanmylee authored Apr 6, 2023
2 parents bc2e096 + 88b961c commit 2e3f9f1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/src/routes/docs/[...2]api/[...3]create-columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const columns = table.createColumns([

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

_Defaults to the value of `accessor` if a string accessor is passed_. Required if a function accessor is passed.
_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.

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

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "svelte-headless-table",
"description": "Unopinionated and extensible data tables for Svelte",
"version": "0.17.2",
"version": "0.17.3",
"scripts": {
"dev": "vite dev",
"build": "vite build",
Expand Down
12 changes: 6 additions & 6 deletions src/lib/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export type FlatColumnInit<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Id extends string = any
> = Omit<ColumnInit<Item, Plugins>, 'height'> & {
id: Id;
id?: Id;
};

export class FlatColumn<
Expand All @@ -65,7 +65,7 @@ export class FlatColumn<
id: Id;
constructor({ header, footer, plugins, id }: FlatColumnInit<Item, Plugins>) {
super({ header, footer, plugins, height: 1 });
this.id = id;
this.id = id ?? String(header);
}
}

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

export type DataColumnInitIdAndKey<Item, Id extends string, Key extends keyof Item> = {
accessor: Key;
id: Id;
id?: Id;
};

export type DataColumnInitFnAndId<Item, Id extends string, Value> = {
Expand Down Expand Up @@ -133,10 +133,10 @@ export class DataColumn<
} else {
this.accessorKey = accessor;
}
if (id === undefined && this.accessorKey === undefined) {
throw new Error('A column id or string accessor is required');
if (id === undefined && this.accessorKey === undefined && header === undefined) {
throw new Error('A column id, string accessor, or header is required');
}
this.id = (id ?? String(this.accessorKey)) as Id;
this.id = (id ?? this.accessorKey ? String(this.accessorKey) : String(header)) as Id;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
38 changes: 38 additions & 0 deletions src/lib/createTable.createColumns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,41 @@ it('throws if two columns have the same id', () => {
]);
}).toThrowError('Duplicate column ids not allowed: "firstName"');
});

it('using headers as id, passes if no duplicate headers', () => {
expect(() => {
table.createColumns([
table.column({
header: 'First Name',
accessor: (item) => item.firstName
}),
table.column({
header: 'Last Name',
accessor: (item) => item.lastName
}),
table.display({
header: 'Actions',
cell: () => ''
}),
])
}).not.toThrow()
});

it('using headers as id, throws if two columns have the same headers', () => {
expect(() => {
table.createColumns([
table.column({
header: 'First Name',
accessor: (item) => item.firstName
}),
table.column({
header: 'Last Name',
accessor: (item) => item.lastName
}),
table.display({
header: 'First Name',
cell: () => ''
}),
])
}).toThrowError('Duplicate column ids not allowed: "First Name"')
});

0 comments on commit 2e3f9f1

Please sign in to comment.