From bc087243c5589db750002943506572df4053ab2e Mon Sep 17 00:00:00 2001 From: "K.Kiyokawa" Date: Tue, 14 Sep 2021 01:33:17 +0900 Subject: [PATCH] feat: typing for DataTable row and cell --- types/DataTable/DataTable.svelte.d.ts | 48 +++++++++++++++------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/types/DataTable/DataTable.svelte.d.ts b/types/DataTable/DataTable.svelte.d.ts index 0f072f9c7d..9090abdcaa 100644 --- a/types/DataTable/DataTable.svelte.d.ts +++ b/types/DataTable/DataTable.svelte.d.ts @@ -5,8 +5,8 @@ export type DataTableKey = string; export type DataTableValue = any; -export interface DataTableEmptyHeader { - key: DataTableKey; +export interface DataTableEmptyHeader { + key: keyof Row; empty: boolean; display?: (item: Value, row: DataTableRow) => DataTableValue; sort?: false | ((a: DataTableValue, b: DataTableValue) => number); @@ -15,8 +15,10 @@ export interface DataTableEmptyHeader { minWidth?: string; } -export interface DataTableNonEmptyHeader { - key: DataTableKey; +export interface DataTableNonEmptyHeader< + Row extends DataTableRow = DataTableRow +> { + key: keyof Row; value: DataTableValue; display?: (item: Value, row: DataTableRow) => DataTableValue; sort?: false | ((a: DataTableValue, b: DataTableValue) => number); @@ -25,7 +27,9 @@ export interface DataTableNonEmptyHeader { minWidth?: string; } -export type DataTableHeader = DataTableNonEmptyHeader | DataTableEmptyHeader; +export type DataTableHeader = + | DataTableNonEmptyHeader + | DataTableEmptyHeader; export interface DataTableRow { id: any; @@ -34,8 +38,8 @@ export interface DataTableRow { export type DataTableRowId = any; -export interface DataTableCell { - key: DataTableKey; +export interface DataTableCell { + key: keyof Row; value: DataTableValue; display?: (item: Value, row: DataTableRow) => DataTableValue; } @@ -47,14 +51,14 @@ export interface DataTableProps extends RestProps { * Specify the data table headers * @default [] */ - headers?: ReadonlyArray; + headers?: ReadonlyArray>; /** * Specify the rows the data table should render * keys defined in `headers` are used for the row ids * @default [] */ - rows?: ReadonlyArray; + rows?: ReadonlyArray; /** * Set the size of the data table @@ -181,33 +185,35 @@ export interface DataTableProps extends RestProps { [key: `data-${string}`]: any; } -export default class DataTable extends SvelteComponentTyped< - DataTableProps, +export default class DataTable< + Row extends DataTableRow = DataTableRow +> extends SvelteComponentTyped< + DataTableProps, { click: CustomEvent<{ - header?: DataTableHeader; - row?: DataTableRow; - cell?: DataTableCell; + header?: DataTableHeader; + row?: Row; + cell?: DataTableCell; }>; ["click:header--expand"]: CustomEvent<{ expanded: boolean }>; ["click:header"]: CustomEvent<{ - header: DataTableHeader; + header: DataTableHeader; sortDirection?: "ascending" | "descending" | "none"; }>; ["click:header--select"]: CustomEvent<{ indeterminate: boolean; selected: boolean; }>; - ["click:row"]: CustomEvent; - ["mouseenter:row"]: CustomEvent; - ["mouseleave:row"]: CustomEvent; + ["click:row"]: CustomEvent; + ["mouseenter:row"]: CustomEvent; + ["mouseleave:row"]: CustomEvent; ["click:row--expand"]: CustomEvent<{ expanded: boolean; - row: DataTableRow; + row: Row; }>; ["click:row--select"]: CustomEvent<{ selected: boolean; - row: DataTableRow; + row: Row; }>; ["click:cell"]: CustomEvent; }, @@ -221,7 +227,7 @@ export default class DataTable extends SvelteComponentTyped< }; ["cell-header"]: { header: DataTableNonEmptyHeader }; description: {}; - ["expanded-row"]: { row: DataTableRow }; + ["expanded-row"]: { row: Row }; title: {}; } > {}