From c3c5d2f5b9dc1cef796f20568ac3b0845e38f396 Mon Sep 17 00:00:00 2001 From: risalfajar Date: Wed, 22 Mar 2023 21:56:26 +0700 Subject: [PATCH 1/2] feat: make `header` as column's alternative id --- .../docs/[...2]api/[...3]create-columns.md | 2 +- package-lock.json | 4 +- src/lib/columns.ts | 12 +++--- src/lib/createTable.createColumns.test.ts | 38 +++++++++++++++++++ 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/docs/src/routes/docs/[...2]api/[...3]create-columns.md b/docs/src/routes/docs/[...2]api/[...3]create-columns.md index c67c830..5fcc068 100644 --- a/docs/src/routes/docs/[...2]api/[...3]create-columns.md +++ b/docs/src/routes/docs/[...2]api/[...3]create-columns.md @@ -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)` diff --git a/package-lock.json b/package-lock.json index 5d6e398..36c388e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "svelte-headless-table", - "version": "0.17.1", + "version": "0.17.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "svelte-headless-table", - "version": "0.17.1", + "version": "0.17.2", "license": "MIT", "dependencies": { "svelte-keyed": "^1.1.5", diff --git a/src/lib/columns.ts b/src/lib/columns.ts index 955db59..0d10795 100644 --- a/src/lib/columns.ts +++ b/src/lib/columns.ts @@ -50,7 +50,7 @@ export type FlatColumnInit< // eslint-disable-next-line @typescript-eslint/no-explicit-any Id extends string = any > = Omit, 'height'> & { - id: Id; + id?: Id; }; export class FlatColumn< @@ -65,7 +65,7 @@ export class FlatColumn< id: Id; constructor({ header, footer, plugins, id }: FlatColumnInit) { super({ header, footer, plugins, height: 1 }); - this.id = id; + this.id = id ?? String(header); } } @@ -96,7 +96,7 @@ export type DataColumnInitKey = { export type DataColumnInitIdAndKey = { accessor: Key; - id: Id; + id?: Id; }; export type DataColumnInitFnAndId = { @@ -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 diff --git a/src/lib/createTable.createColumns.test.ts b/src/lib/createTable.createColumns.test.ts index 4fd613c..abcc595 100644 --- a/src/lib/createTable.createColumns.test.ts +++ b/src/lib/createTable.createColumns.test.ts @@ -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"') +}); From 88b961cc0d042061ebc5068c06bd184a47bd136d Mon Sep 17 00:00:00 2001 From: risalfajar Date: Thu, 6 Apr 2023 00:08:08 +0700 Subject: [PATCH 2/2] bump version 0.17.3 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 36c388e..0ffe031 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "svelte-headless-table", - "version": "0.17.2", + "version": "0.17.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "svelte-headless-table", - "version": "0.17.2", + "version": "0.17.3", "license": "MIT", "dependencies": { "svelte-keyed": "^1.1.5", diff --git a/package.json b/package.json index b2c38a5..9327c1e 100644 --- a/package.json +++ b/package.json @@ -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",