-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f14298b
commit f7e0c5e
Showing
18 changed files
with
1,007 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/design-system/src/elements/toniq-list-table/list-table-inputs.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {assertTypeOf} from 'run-time-assertions'; | ||
import {createListTableTable} from './list-table-inputs'; | ||
|
||
describe(createListTableTable.name, () => { | ||
it('should have proper type enforcement', () => { | ||
createListTableTable({ | ||
entries: [], | ||
columns: [ | ||
{key: 'first', title: 'First'}, | ||
{key: 'second', title: 'Second'}, | ||
], | ||
// missing keys | ||
// @ts-expect-error | ||
createRowObject(entry) { | ||
return { | ||
cells: { | ||
first: 'yo', | ||
}, | ||
}; | ||
}, | ||
}); | ||
createListTableTable({ | ||
entries: [ | ||
{ | ||
key1: 'string', | ||
key2: 42, | ||
}, | ||
], | ||
columns: [ | ||
{key: 'first', title: 'First'}, | ||
{key: 'second', title: 'Second'}, | ||
], | ||
createRowObject(entry) { | ||
assertTypeOf(entry).toEqualTypeOf<Readonly<{key1: string; key2: number}>>(); | ||
return { | ||
cells: { | ||
first: 'yo', | ||
second: 'hi', | ||
}, | ||
}; | ||
}, | ||
}); | ||
createListTableTable({ | ||
entries: [ | ||
{ | ||
key1: 'string', | ||
key2: 42, | ||
}, | ||
], | ||
columns: [], | ||
// can't do anything if columNames is empty | ||
// @ts-expect-error | ||
createRowObject(entry) { | ||
return {cells: {}}; | ||
}, | ||
}); | ||
}); | ||
}); |
92 changes: 92 additions & 0 deletions
92
packages/design-system/src/elements/toniq-list-table/list-table-inputs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import {ArrayElement, PartialAndUndefined, areJsonEqual} from '@augment-vir/common'; | ||
import {HtmlInterpolation} from 'element-vir'; | ||
|
||
export type ColumnsBase = ReadonlyArray< | ||
Readonly<{ | ||
key: PropertyKey; | ||
title: string; | ||
disabled?: boolean; | ||
mobile?: { | ||
sticky: boolean | undefined; | ||
}; | ||
}> | ||
>; | ||
|
||
export type HeaderItem = { | ||
title: string; | ||
key: string; | ||
left: number; | ||
mobile?: { | ||
sticky: boolean; | ||
}; | ||
}; | ||
|
||
export type ListTableRow<Columns extends ColumnsBase> = { | ||
cells: Readonly< | ||
ArrayElement<Columns> extends never | ||
? never | ||
: Record<ArrayElement<Columns>['key'], HtmlInterpolation> | ||
>; | ||
rowActions?: | ||
| PartialAndUndefined<{ | ||
click: (params: { | ||
clickEvent: MouseEvent; | ||
dispatch: (newEvent: Event) => void; | ||
}) => void; | ||
}> | ||
| undefined; | ||
}; | ||
|
||
export enum ListTableHeaderStyleEnum { | ||
Default = 'default', | ||
Transparent = 'transparent', | ||
} | ||
|
||
export type ListTableInputs = { | ||
columns: Readonly<ColumnsBase>; | ||
rows: ReadonlyArray<Readonly<ListTableRow<any>>>; | ||
/** Defaults to ListTableHeaderStyleEnum.Default. */ | ||
headerStyle?: ListTableHeaderStyleEnum | undefined; | ||
pagination?: | ||
| Readonly<{ | ||
currentPage: number; | ||
pageCount: number; | ||
}> | ||
| undefined; | ||
showLoading?: boolean | undefined; | ||
}; | ||
|
||
export type CreateRowObjectCallback<EntryType, Columns extends ColumnsBase> = ( | ||
entry: Readonly<EntryType>, | ||
index: number, | ||
) => Readonly<ListTableRow<Columns>>; | ||
|
||
export function createListTableTable<EntryType, const Columns extends ColumnsBase>({ | ||
entries, | ||
columns, | ||
createRowObject, | ||
}: { | ||
entries: ReadonlyArray<Readonly<EntryType>>; | ||
columns: Readonly<Columns>; | ||
createRowObject: CreateRowObjectCallback<EntryType, Columns>; | ||
}): Pick<ListTableInputs, 'rows' | 'columns'> { | ||
const columnsObject = Object.fromEntries( | ||
columns.map((column) => [ | ||
column.key, | ||
'', | ||
]), | ||
); | ||
|
||
const rows = entries.map((entry, index) => { | ||
const row = createRowObject(entry, index); | ||
|
||
if (!areJsonEqual(Object.keys(columnsObject).sort(), Object.keys(row.cells).sort())) { | ||
console.error('broken list table row', {cells: row.cells, columns: columnsObject}); | ||
throw new Error('List table row keys does not match expect column keys.'); | ||
} | ||
|
||
return row; | ||
}); | ||
|
||
return {rows, columns}; | ||
} |
Oops, something went wrong.