Skip to content

Commit

Permalink
Fix: primefaces#2523 DataTable/TreeTable: stricter generically typed API
Browse files Browse the repository at this point in the history
  • Loading branch information
bu3alwa committed Dec 24, 2022
1 parent 0d313c0 commit 4d52937
Showing 1 changed file with 63 additions and 40 deletions.
103 changes: 63 additions & 40 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 Down Expand Up @@ -212,33 +212,56 @@ 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>;
rowData: DataTableRowSelection<TValue>;
column: Column;
}

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 DataTableRowSelection<TValue extends DataTableValueArray> = {
[K in keyof TValue]: TValue[K];
};

type DataTableRowSelectionArray<TValue extends DataTableValueArray> = DataTableRowSelection<TValue>[];

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

type DataTableSelection<TValue extends DataTableValueArray> = DataTableRowSelection<TValue> | DataTableRowSelectionArray<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 @@ -265,15 +288,15 @@ export interface DataTableProps extends Omit<React.DetailedHTMLProps<React.Input
filterLocale?: string;
filters?: DataTableFilterMeta;
first?: number;
footer?: DataTableFooterTemplateType;
footer?: DataTableFooterTemplateType<TValue>;
footerColumnGroup?: React.ReactNode;
frozenValue?: any[];
frozenValue?: DataTableRowSelection<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 +316,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 +327,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 +347,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 +362,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 +381,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;
rowClassName?(data: any, options: DataTableRowClassNameOptions<TValue>): object | string;
rowEditValidator?(data: any, options: DataTableRowEditValidatorOptions<TValue>): boolean;
rowExpansionTemplate?(data: any, options: DataTableRowExpansionTemplate): React.ReactNode;
showRowReorderElement?(data: any, options: DataTableShowRowReorderElementOptions): boolean | undefined | null;
showSelectionElement?(data: any, options: DataTableShowSelectionElementOptions): boolean | undefined | null;
showRowReorderElement?(data: any, options: DataTableShowRowReorderElementOptions<TValue>): boolean | undefined | null;
showSelectionElement?(data: any, 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 4d52937

Please sign in to comment.