Skip to content

Commit

Permalink
Fix: primefaces#2523 DataTable/TreeTable: stricter generically typed …
Browse files Browse the repository at this point in the history
…API (primefaces#3822)

* Fix: primefaces#2523 DataTable/TreeTable: stricter generically typed API

* Fix: primefaces#2523 DataTable/TreeTable: stricter generically typed API

* Fix: DataTableCellClassNameOptions to correct type

* Fix: more strict types on anys
  • Loading branch information
bu3alwa authored Dec 27, 2022
1 parent f76463b commit 06a6721
Showing 1 changed file with 72 additions and 47 deletions.
119 changes: 72 additions & 47 deletions components/lib/datatable/datatable.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as React from 'react';
import { Column, ColumnFilterMatchModeType } from '../column';
import { Column, ColumnFilterMatchModeType, ColumnProps } from '../column';
import { PaginatorTemplate } from '../paginator';
import { VirtualScroller, VirtualScrollerProps } from '../virtualscroller/virtualscroller';

type DataTableHeaderTemplateType = React.ReactNode | ((options: DataTableHeaderTemplateOptions) => React.ReactNode);
type DataTableHeaderTemplateType<TValue extends DataTableValueArray> = React.ReactNode | ((options: DataTableHeaderTemplateOptions<TValue>) => React.ReactNode);

type DataTableFooterTemplateType = React.ReactNode | ((options: DataTableFooterTemplateOptions) => React.ReactNode);
type DataTableFooterTemplateType<TValue extends DataTableValueArray> = React.ReactNode | ((options: DataTableFooterTemplateOptions<TValue>) => React.ReactNode);

type DataTableRowGroupHeaderTemplateType = React.ReactNode | ((data: any, options: DataTableRowGroupHeaderTemplateOptions) => React.ReactNode);
type DataTableRowGroupHeaderTemplateType<TValue extends DataTableValueArray> = React.ReactNode | ((data: any, options: DataTableRowGroupHeaderTemplateOptions<TValue>) => React.ReactNode);

type DataTableRowGroupFooterTemplateType = React.ReactNode | ((data: any, options: DataTableRowGroupFooterTemplateOptions) => React.ReactNode);
type DataTableRowGroupFooterTemplateType<TValue extends DataTableValueArray> = React.ReactNode | ((data: any, options: DataTableRowGroupFooterTemplateOptions<TValue>) => React.ReactNode);

type DataTablePaginatorPositionType = 'top' | 'bottom' | 'both';

Expand Down Expand Up @@ -45,19 +45,19 @@ type DataTableSizeType = 'small' | 'normal' | 'large';

type DataTableScrollDirectionType = 'vertical' | 'horizontal' | 'both';

interface DataTableHeaderTemplateOptions {
props: DataTableProps;
interface DataTableHeaderTemplateOptions<TValue extends DataTableValueArray> {
props: DataTableProps<TValue>;
}

interface DataTableFooterTemplateOptions extends DataTableHeaderTemplateOptions {}
interface DataTableFooterTemplateOptions<TValue extends DataTableValueArray> extends DataTableHeaderTemplateOptions<TValue> {}

interface DataTableRowGroupHeaderTemplateOptions {
interface DataTableRowGroupHeaderTemplateOptions<TValue extends DataTableValueArray> {
index: number;
props: DataTableProps;
props: DataTableProps<TValue>;
customRendering: boolean;
}

interface DataTableRowGroupFooterTemplateOptions extends DataTableRowGroupHeaderTemplateOptions {
interface DataTableRowGroupFooterTemplateOptions<T extends DataTableValueArray> extends DataTableRowGroupHeaderTemplateOptions<T> {
colSpan: number;
}

Expand Down Expand Up @@ -130,9 +130,9 @@ interface DataTableDataSelectableParams {
index: number;
}

interface DataTableSelectionChangeParams {
interface DataTableSelectionChangeParams<TValue extends DataTableValueArray> {
originalEvent: React.SyntheticEvent;
value: any;
value: DataTableSelection<TValue>;
type?: string;
[key: string]: any;
}
Expand All @@ -158,7 +158,7 @@ interface DataTableCellClickEventParams {
originalEvent: React.MouseEvent<HTMLElement>;
value: any;
field: string;
rowData: any;
rowData: DataTableValue;
rowIndex: number;
cellIndex: number;
selected: boolean;
Expand All @@ -173,7 +173,7 @@ interface DataTableRowEditSaveParams extends DataTableRowEditParams {
}

interface DataTableRowEditCompleteParams extends DataTableRowEventParams {
newData: any;
newData: DataTableValue;
field: string;
index: number;
}
Expand All @@ -187,9 +187,9 @@ interface DataTableSelectParams {
interface DataTableUnselectParams extends DataTableSelectParams {}

interface DataTableExportFunctionParams {
data: any;
data: DataTableValueArray;
field: string;
rowData: any;
rowData: DataTableValue;
column: Column;
}

Expand All @@ -212,33 +212,58 @@ interface DataTableRowExpansionTemplate {
customRendering: boolean;
}

interface DataTableRowClassNameOptions {
props: DataTableProps;
interface DataTableRowClassNameOptions<TValue extends DataTableValueArray> {
props: DataTableProps<TValue>;
}

interface DataTableCellClassNameOptions {
props: DataTableProps;
rowData: any;
interface DataTableCellClassNameOptions<TValue extends DataTableValueArray> {
props: DataTableProps<TValue>;
column: Column;
field: string;
frozenRow: boolean;
rowIndex: number;
}

interface DataTableShowSelectionElementOptions {
interface DataTableShowSelectionElementOptions<TValue extends DataTableValueArray> {
rowIndex: number;
props: DataTableProps;
props: DataTableProps<TValue>;
}

interface DataTableShowRowReorderElementOptions {
interface DataTableShowRowReorderElementOptions<TValue extends DataTableValueArray> {
rowIndex: number;
props: DataTableProps;
props: DataTableProps<TValue>;
}

interface DataTableRowEditValidatorOptions {
props: DataTableProps;
interface DataTableRowEditValidatorOptions<TValue extends DataTableValueArray> {
props: DataTableProps<TValue>;
}

export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'size' | 'onContextMenu' | 'ref'> {
interface DataTableValue extends Record<string, any> {}

interface DataTableValueArray extends Array<DataTableValue> {}

type DataTableRowData<TValue extends DataTableValueArray> = {
[K in keyof TValue]: TValue[K];
};

type DataTableRowDataArray<TValue extends DataTableValueArray> = DataTableRowData<TValue>[];

type DataTableCellSelection<TValue extends DataTableValueArray> = {
cellIndex: number;
column: Column;
field: string;
props: ColumnProps;
rowData: DataTableRowData<TValue>;
rowIndex: number;
selected: boolean;
value: TValue[keyof TValue];
};

type DataTableSelection<TValue extends DataTableValueArray> = DataTableRowData<TValue> | DataTableRowDataArray<TValue> | DataTableCellSelection<TValue>;

export interface DataTableProps<TValue extends DataTableValueArray> extends Omit<React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'size' | 'onContextMenu' | 'ref' | 'value'> {
id?: string;
value?: any[];
value?: TValue;
alwaysShowPaginator?: boolean;
autoLayout?: boolean;
breakpoint?: string;
Expand All @@ -254,26 +279,26 @@ export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.Input
defaultSortOrder?: DataTableSortOrderType;
dragSelection?: boolean;
editMode?: string;
editingRows?: any[] | DataTableEditingRows;
editingRows?: DataTableValueArray | DataTableEditingRows;
emptyMessage?: DataTableEmptyMessageType;
expandableRowGroups?: boolean;
expandedRowIcon?: string;
expandedRows?: any[] | DataTableExpandedRows;
expandedRows?: DataTableValueArray | DataTableExpandedRows;
exportFilename?: string;
filterDelay?: number;
filterDisplay?: DataTableFilterDisplayType;
filterLocale?: string;
filters?: DataTableFilterMeta;
first?: number;
footer?: DataTableFooterTemplateType;
footer?: DataTableFooterTemplateType<TValue>;
footerColumnGroup?: React.ReactNode;
frozenValue?: any[];
frozenValue?: DataTableRowData<TValue>[];
frozenWidth?: string;
globalFilter?: DataTableGlobalFilterType;
globalFilterFields?: string[];
globalFilterMatchMode?: ColumnFilterMatchModeType;
groupRowsBy?: string;
header?: DataTableHeaderTemplateType;
header?: DataTableHeaderTemplateType<TValue>;
headerColumnGroup?: React.ReactNode;
lazy?: boolean;
loading?: boolean;
Expand All @@ -293,8 +318,8 @@ export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.Input
reorderableRows?: boolean;
resizableColumns?: boolean;
responsiveLayout?: DataTableResponsiveLayoutType;
rowGroupFooterTemplate?: DataTableRowGroupFooterTemplateType;
rowGroupHeaderTemplate?: DataTableRowGroupHeaderTemplateType;
rowGroupFooterTemplate?: DataTableRowGroupFooterTemplateType<TValue>;
rowGroupHeaderTemplate?: DataTableRowGroupHeaderTemplateType<TValue>;
rowGroupMode?: string;
rowHover?: boolean;
rows?: number;
Expand All @@ -304,7 +329,7 @@ export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.Input
scrollable?: boolean;
selectAll?: boolean;
selectOnEdit?: boolean;
selection?: any | any[];
selection?: DataTableSelection<TValue>;
selectionAutoFocus?: boolean;
selectionAriaLabel?: string;
selectionMode?: DataTableSelectionModeType;
Expand All @@ -324,7 +349,7 @@ export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.Input
tableStyle?: React.CSSProperties;
totalRecords?: number;
virtualScrollerOptions?: VirtualScrollerProps;
cellClassName?(value: any, options: DataTableCellClassNameOptions): object | string;
cellClassName?(value: any, options: DataTableCellClassNameOptions<TValue>): object | string;
customRestoreState?(): object;
customSaveState?(state: object): void;
exportFunction?(e: DataTableExportFunctionParams): any;
Expand All @@ -339,7 +364,7 @@ export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.Input
onColumnResizerClick?(e: DataTableColumnResizerClickParams): void;
onColumnResizerDoubleClick?(e: DataTableColumnResizerClickParams): void;
onContextMenu?(e: DataTableRowEventParams): void;
onContextMenuSelectionChange?(e: DataTableSelectionChangeParams): void;
onContextMenuSelectionChange?(e: DataTableSelectionChangeParams<TValue>): void;
onFilter?(e: DataTablePFSEvent): void;
onPage?(e: DataTablePFSEvent): void;
onRowClick?(e: DataTableRowClickEventParams): void;
Expand All @@ -358,20 +383,20 @@ export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.Input
onRowToggle?(e: DataTableRowToggleParams): void;
onRowUnselect?(e: DataTableUnselectParams): void;
onSelectAllChange?(e: DataTableSelectAllChangeParams): void;
onSelectionChange?(e: DataTableSelectionChangeParams): void;
onSelectionChange?(e: DataTableSelectionChangeParams<TValue>): void;
onSort?(e: DataTablePFSEvent): void;
onStateRestore?(state: object): void;
onStateSave?(state: object): void;
onValueChange?(value: any[]): void;
rowClassName?(data: any, options: DataTableRowClassNameOptions): object | string;
rowEditValidator?(data: any, options: DataTableRowEditValidatorOptions): boolean;
rowExpansionTemplate?(data: any, options: DataTableRowExpansionTemplate): React.ReactNode;
showRowReorderElement?(data: any, options: DataTableShowRowReorderElementOptions): boolean | undefined | null;
showSelectionElement?(data: any, options: DataTableShowSelectionElementOptions): boolean | undefined | null;
rowClassName?(data: DataTableRowData<TValue>, options: DataTableRowClassNameOptions<TValue>): object | string;
rowEditValidator?(data: DataTableRowData<TValue>, options: DataTableRowEditValidatorOptions<TValue>): boolean;
rowExpansionTemplate?(data: DataTableRowData<TValue>, options: DataTableRowExpansionTemplate): React.ReactNode;
showRowReorderElement?(data: DataTableRowData<TValue>, options: DataTableShowRowReorderElementOptions<TValue>): boolean | undefined | null;
showSelectionElement?(data: DataTableRowData<TValue>, options: DataTableShowSelectionElementOptions<TValue>): boolean | undefined | null;
children?: React.ReactNode;
}

export declare class DataTable extends React.Component<DataTableProps, any> {
export declare class DataTable<TValue extends DataTableValueArray> extends React.Component<DataTableProps<TValue>, any> {
public clearState(): void;
public closeEditingCell(): void;
public exportCSV(options?: { selectionOnly: boolean }): void;
Expand Down

0 comments on commit 06a6721

Please sign in to comment.