UNPKG

13.3 kBTypeScriptView Raw
1import type { Key } from 'react';
2import type { ReactElement } from 'react';
3import type { RefAttributes } from 'react';
4
5export declare interface CalculatedColumn<TRow, TSummaryRow = unknown> extends Column<TRow, TSummaryRow> {
6 readonly idx: number;
7 readonly resizable: boolean;
8 readonly sortable: boolean;
9 readonly frozen: boolean;
10 readonly isLastFrozenColumn: boolean;
11 readonly rowGroup: boolean;
12 readonly formatter: React.ComponentType<FormatterProps<TRow, TSummaryRow>>;
13}
14
15export declare type CellNavigationMode = 'NONE' | 'CHANGE_ROW' | 'LOOP_OVER_ROW';
16
17export declare interface CellRendererProps<TRow, TSummaryRow> extends Pick<RowRendererProps<TRow, TSummaryRow>, 'onRowClick' | 'onRowDoubleClick' | 'selectCell'>, Omit_2<React.HTMLAttributes<HTMLDivElement>, 'style' | 'children'> {
18 column: CalculatedColumn<TRow, TSummaryRow>;
19 colSpan: number | undefined;
20 row: TRow;
21 isCopied: boolean;
22 isDraggedOver: boolean;
23 isCellSelected: boolean;
24 dragHandle: ReactElement<React.HTMLAttributes<HTMLDivElement>> | undefined;
25 onRowChange: (newRow: TRow) => void;
26}
27
28export declare type ColSpanArgs<R, SR> = {
29 type: 'HEADER';
30} | {
31 type: 'ROW';
32 row: R;
33} | {
34 type: 'SUMMARY';
35 row: SR;
36};
37
38export declare interface Column<TRow, TSummaryRow = unknown> {
39 /** The name of the column. By default it will be displayed in the header cell */
40 readonly name: string | ReactElement;
41 /** A unique key to distinguish each column */
42 readonly key: string;
43 /** Column width. If not specified, it will be determined automatically based on grid width and specified widths of other columns */
44 readonly width?: Maybe<number | string>;
45 /** Minimum column width in px. */
46 readonly minWidth?: Maybe<number>;
47 /** Maximum column width in px. */
48 readonly maxWidth?: Maybe<number>;
49 readonly cellClass?: Maybe<string | ((row: TRow) => Maybe<string>)>;
50 readonly headerCellClass?: Maybe<string>;
51 readonly summaryCellClass?: Maybe<string | ((row: TSummaryRow) => Maybe<string>)>;
52 /** Formatter to be used to render the cell content */
53 readonly formatter?: Maybe<React.ComponentType<FormatterProps<TRow, TSummaryRow>>>;
54 /** Formatter to be used to render the summary cell content */
55 readonly summaryFormatter?: Maybe<React.ComponentType<SummaryFormatterProps<TSummaryRow, TRow>>>;
56 /** Formatter to be used to render the group cell content */
57 readonly groupFormatter?: Maybe<React.ComponentType<GroupFormatterProps<TRow, TSummaryRow>>>;
58 /** Enables cell editing. If set and no editor property specified, then a textinput will be used as the cell editor */
59 readonly editable?: Maybe<boolean | ((row: TRow) => boolean)>;
60 readonly colSpan?: Maybe<(args: ColSpanArgs<TRow, TSummaryRow>) => Maybe<number>>;
61 /** Determines whether column is frozen or not */
62 readonly frozen?: Maybe<boolean>;
63 /** Enable resizing of a column */
64 readonly resizable?: Maybe<boolean>;
65 /** Enable sorting of a column */
66 readonly sortable?: Maybe<boolean>;
67 /** Sets the column sort order to be descending instead of ascending the first time the column is sorted */
68 readonly sortDescendingFirst?: Maybe<boolean>;
69 /** Editor to be rendered when cell of column is being edited. If set, then the column is automatically set to be editable */
70 readonly editor?: Maybe<React.ComponentType<EditorProps<TRow, TSummaryRow>>>;
71 readonly editorOptions?: Maybe<{
72 /** @default false */
73 readonly renderFormatter?: Maybe<boolean>;
74 /** @default false */
75 readonly editOnClick?: Maybe<boolean>;
76 /** @default true */
77 readonly commitOnOutsideClick?: Maybe<boolean>;
78 /** Prevent default to cancel editing */
79 readonly onCellKeyDown?: Maybe<(event: React.KeyboardEvent<HTMLDivElement>) => void>;
80 /** Control the default cell navigation behavior while the editor is open */
81 readonly onNavigation?: Maybe<(event: React.KeyboardEvent<HTMLDivElement>) => boolean>;
82 }>;
83 /** Header renderer for each header cell */
84 readonly headerRenderer?: Maybe<React.ComponentType<HeaderRendererProps<TRow, TSummaryRow>>>;
85}
86
87export declare interface DataGridHandle {
88 element: HTMLDivElement | null;
89 scrollToColumn: (colIdx: number) => void;
90 scrollToRow: (rowIdx: number) => void;
91 selectCell: (position: Position, enableEditor?: Maybe<boolean>) => void;
92}
93
94export declare interface DataGridProps<R, SR = unknown, K extends Key = Key> extends SharedDivProps {
95 /**
96 * Grid and data Props
97 */
98 /** An array of objects representing each column on the grid */
99 columns: readonly Column<R, SR>[];
100 /** A function called for each rendered row that should return a plain key/value pair object */
101 rows: readonly R[];
102 /**
103 * Rows to be pinned at the bottom of the rows view for summary, the vertical scroll bar will not scroll these rows.
104 * Bottom horizontal scroll bar can move the row left / right. Or a customized row renderer can be used to disabled the scrolling support.
105 */
106 summaryRows?: Maybe<readonly SR[]>;
107 /** The getter should return a unique key for each row */
108 rowKeyGetter?: Maybe<(row: R) => K>;
109 onRowsChange?: Maybe<(rows: R[], data: RowsChangeData<R, SR>) => void>;
110 /**
111 * Dimensions props
112 */
113 /**
114 * The height of each row in pixels
115 * @default 35
116 */
117 rowHeight?: Maybe<number | ((args: RowHeightArgs<R>) => number)>;
118 /**
119 * The height of the header row in pixels
120 * @default 35
121 */
122 headerRowHeight?: Maybe<number>;
123 /**
124 * The height of each summary row in pixels
125 * @default 35
126 */
127 summaryRowHeight?: Maybe<number>;
128 /**
129 * Feature props
130 */
131 /** Set of selected row keys */
132 selectedRows?: Maybe<ReadonlySet<K>>;
133 /** Function called whenever row selection is changed */
134 onSelectedRowsChange?: Maybe<(selectedRows: Set<K>) => void>;
135 /** Used for multi column sorting */
136 sortColumns?: Maybe<readonly SortColumn[]>;
137 onSortColumnsChange?: Maybe<(sortColumns: SortColumn[]) => void>;
138 defaultColumnOptions?: Maybe<DefaultColumnOptions<R, SR>>;
139 groupBy?: Maybe<readonly string[]>;
140 rowGrouper?: Maybe<(rows: readonly R[], columnKey: string) => Record<string, readonly R[]>>;
141 expandedGroupIds?: Maybe<ReadonlySet<unknown>>;
142 onExpandedGroupIdsChange?: Maybe<(expandedGroupIds: Set<unknown>) => void>;
143 onFill?: Maybe<(event: FillEvent<R>) => R>;
144 onPaste?: Maybe<(event: PasteEvent<R>) => R>;
145 /**
146 * Event props
147 */
148 /** Function called whenever a row is clicked */
149 onRowClick?: Maybe<(row: R, column: CalculatedColumn<R, SR>) => void>;
150 /** Function called whenever a row is double clicked */
151 onRowDoubleClick?: Maybe<(row: R, column: CalculatedColumn<R, SR>) => void>;
152 /** Called when the grid is scrolled */
153 onScroll?: Maybe<(event: React.UIEvent<HTMLDivElement>) => void>;
154 /** Called when a column is resized */
155 onColumnResize?: Maybe<(idx: number, width: number) => void>;
156 /**
157 * Toggles and modes
158 */
159 /** @default 'NONE' */
160 cellNavigationMode?: Maybe<CellNavigationMode>;
161 /** @default true */
162 enableVirtualization?: Maybe<boolean>;
163 /**
164 * Miscellaneous
165 */
166 rowRenderer?: Maybe<React.ComponentType<RowRendererProps<R, SR>>>;
167 noRowsFallback?: React.ReactNode;
168 rowClass?: Maybe<(row: R) => Maybe<string>>;
169 'data-testid'?: Maybe<string>;
170}
171
172declare const _default: <R, SR = unknown, K extends Key = Key>(props: DataGridProps<R, SR, K> & RefAttributes<DataGridHandle>) => JSX.Element;
173export default _default;
174
175declare type DefaultColumnOptions<R, SR> = Pick<Column<R, SR>, 'formatter' | 'minWidth' | 'resizable' | 'sortable'>;
176
177export declare interface EditorProps<TRow, TSummaryRow = unknown> {
178 column: CalculatedColumn<TRow, TSummaryRow>;
179 row: TRow;
180 onRowChange: (row: TRow, commitChanges?: boolean) => void;
181 onClose: (commitChanges?: boolean) => void;
182}
183
184export declare interface FillEvent<TRow> {
185 columnKey: string;
186 sourceRow: TRow;
187 targetRow: TRow;
188}
189
190export declare interface FormatterProps<TRow, TSummaryRow = unknown> {
191 column: CalculatedColumn<TRow, TSummaryRow>;
192 row: TRow;
193 isCellSelected: boolean;
194 onRowChange: (row: TRow) => void;
195}
196
197export declare interface GroupFormatterProps<TRow, TSummaryRow = unknown> {
198 groupKey: unknown;
199 column: CalculatedColumn<TRow, TSummaryRow>;
200 row: GroupRow<TRow>;
201 childRows: readonly TRow[];
202 isExpanded: boolean;
203 isCellSelected: boolean;
204 toggleGroup: () => void;
205}
206
207declare interface GroupRow<TRow> {
208 readonly childRows: readonly TRow[];
209 readonly id: string;
210 readonly parentId: unknown;
211 readonly groupKey: unknown;
212 readonly isExpanded: boolean;
213 readonly level: number;
214 readonly posInSet: number;
215 readonly setSize: number;
216 readonly startRowIndex: number;
217}
218
219export declare interface HeaderRendererProps<TRow, TSummaryRow = unknown> {
220 column: CalculatedColumn<TRow, TSummaryRow>;
221 sortDirection: SortDirection | undefined;
222 priority: number | undefined;
223 onSort: (ctrlClick: boolean) => void;
224 allRowsSelected: boolean;
225 onAllRowsSelectionChange: (checked: boolean) => void;
226 isCellSelected: boolean;
227}
228
229declare type Maybe<T> = T | undefined | null;
230
231declare type Omit_2<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
232
233export declare interface PasteEvent<TRow> {
234 sourceColumnKey: string;
235 sourceRow: TRow;
236 targetColumnKey: string;
237 targetRow: TRow;
238}
239
240declare interface Position {
241 readonly idx: number;
242 readonly rowIdx: number;
243}
244
245declare interface Props<R, SR> extends SharedHeaderCellProps<R, SR> {
246 children: React.ReactNode;
247}
248
249export declare const Row: <R, SR>(props: RowRendererProps<R, SR> & RefAttributes<HTMLDivElement>) => JSX.Element;
250
251export declare type RowHeightArgs<R> = {
252 type: 'ROW';
253 row: R;
254} | {
255 type: 'GROUP';
256 row: GroupRow<R>;
257};
258
259export declare interface RowRendererProps<TRow, TSummaryRow = unknown> extends Omit_2<React.HTMLAttributes<HTMLDivElement>, 'style' | 'children'> {
260 viewportColumns: readonly CalculatedColumn<TRow, TSummaryRow>[];
261 row: TRow;
262 rowIdx: number;
263 selectedCellIdx: number | undefined;
264 copiedCellIdx: number | undefined;
265 draggedOverCellIdx: number | undefined;
266 lastFrozenColumnIndex: number;
267 isRowSelected: boolean;
268 top: number;
269 height: number;
270 selectedCellEditor: ReactElement<EditorProps<TRow>> | undefined;
271 selectedCellDragHandle: ReactElement<React.HTMLAttributes<HTMLDivElement>> | undefined;
272 onRowChange: (rowIdx: number, newRow: TRow) => void;
273 onRowClick: Maybe<(row: TRow, column: CalculatedColumn<TRow, TSummaryRow>) => void>;
274 onRowDoubleClick: Maybe<(row: TRow, column: CalculatedColumn<TRow, TSummaryRow>) => void>;
275 rowClass: Maybe<(row: TRow) => Maybe<string>>;
276 setDraggedOverRowIdx: ((overRowIdx: number) => void) | undefined;
277 selectCell: (row: TRow, column: CalculatedColumn<TRow, TSummaryRow>, enableEditor?: Maybe<boolean>) => void;
278}
279
280export declare interface RowsChangeData<R, SR = unknown> {
281 indexes: number[];
282 column: CalculatedColumn<R, SR>;
283}
284
285export declare const SELECT_COLUMN_KEY = "select-row";
286
287export declare function SelectCellFormatter({ value, isCellSelected, disabled, onClick, onChange, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy }: SelectCellFormatterProps): JSX.Element;
288
289declare interface SelectCellFormatterProps extends SharedInputProps {
290 isCellSelected: boolean;
291 value: boolean;
292 onChange: (value: boolean, isShiftClick: boolean) => void;
293}
294
295export declare const SelectColumn: Column<any, any>;
296
297export declare interface SelectRowEvent<TRow> {
298 row: TRow;
299 checked: boolean;
300 isShiftClick: boolean;
301}
302
303declare type SharedDivProps = Pick<React.HTMLAttributes<HTMLDivElement>, 'aria-label' | 'aria-labelledby' | 'aria-describedby' | 'className' | 'style'>;
304
305declare type SharedHeaderCellProps<R, SR> = Pick<HeaderRendererProps<R, SR>, 'sortDirection' | 'onSort' | 'priority' | 'isCellSelected'>;
306
307declare type SharedInputProps = Pick<React.InputHTMLAttributes<HTMLInputElement>, 'disabled' | 'onClick' | 'aria-label' | 'aria-labelledby'>;
308
309export declare function SortableHeaderCell<R, SR>({ onSort, sortDirection, priority, children, isCellSelected }: Props<R, SR>): JSX.Element;
310
311export declare interface SortColumn {
312 readonly columnKey: string;
313 readonly direction: SortDirection;
314}
315
316export declare type SortDirection = 'ASC' | 'DESC';
317
318export declare interface SummaryFormatterProps<TSummaryRow, TRow = unknown> {
319 column: CalculatedColumn<TRow, TSummaryRow>;
320 row: TSummaryRow;
321 isCellSelected: boolean;
322}
323
324export declare function TextEditor<TRow, TSummaryRow>({ row, column, onRowChange, onClose }: EditorProps<TRow, TSummaryRow>): JSX.Element;
325
326export declare function ToggleGroupFormatter<R, SR>({ groupKey, isExpanded, isCellSelected, toggleGroup }: GroupFormatterProps<R, SR>): JSX.Element;
327
328export declare function useRowSelection<R>(): [boolean, (selectRowEvent: SelectRowEvent<R>) => void];
329
330export declare function ValueFormatter<R, SR>(props: FormatterProps<R, SR>): JSX.Element | null;
331
332export { }