@pablovsouza/react-datagrid 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +267 -2
- package/dist/DataGrid.d.ts +11 -0
- package/dist/DataGrid.d.ts.map +1 -0
- package/dist/DataGrid.js +782 -0
- package/dist/DefaultEditor.d.ts +13 -0
- package/dist/DefaultEditor.d.ts.map +1 -0
- package/dist/DefaultEditor.js +59 -0
- package/dist/clipboard.d.ts +5 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.js +51 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/keyboard.d.ts +30 -0
- package/dist/keyboard.d.ts.map +1 -0
- package/dist/keyboard.js +53 -0
- package/dist/selection.d.ts +23 -0
- package/dist/selection.d.ts.map +1 -0
- package/dist/selection.js +43 -0
- package/dist/styles.css +256 -0
- package/dist/types.d.ts +198 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +73 -4
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EditorProps } from './types';
|
|
2
|
+
interface DefaultEditorProps<T> extends EditorProps<T> {
|
|
3
|
+
text: string;
|
|
4
|
+
parse: (text: string) => unknown;
|
|
5
|
+
align: 'start' | 'center' | 'end';
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The text editor: Enter / Shift+Enter commit and move down / up, Tab / Shift+Tab right / left, Esc cancels,
|
|
9
|
+
* leaving the cell commits. A text `parse` rejects (undefined) keeps the editor open, marked invalid.
|
|
10
|
+
*/
|
|
11
|
+
export declare function DefaultEditor<T>({ text, initialText, parse, commit, cancel, align, column }: DefaultEditorProps<T>): import("react").JSX.Element;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=DefaultEditor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DefaultEditor.d.ts","sourceRoot":"","sources":["../src/DefaultEditor.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C,UAAU,kBAAkB,CAAC,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IACpD,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAA;IAChC,KAAK,EAAE,OAAO,GAAG,QAAQ,GAAG,KAAK,CAAA;CAClC;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,EAC/B,IAAI,EACJ,WAAW,EACX,KAAK,EACL,MAAM,EACN,MAAM,EACN,KAAK,EACL,MAAM,EACP,EAAE,kBAAkB,CAAC,CAAC,CAAC,+BA4DvB"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* The text editor: Enter / Shift+Enter commit and move down / up, Tab / Shift+Tab right / left, Esc cancels,
|
|
5
|
+
* leaving the cell commits. A text `parse` rejects (undefined) keeps the editor open, marked invalid.
|
|
6
|
+
*/
|
|
7
|
+
export function DefaultEditor({ text, initialText, parse, commit, cancel, align, column }) {
|
|
8
|
+
const [draft, setDraft] = useState(initialText ?? text);
|
|
9
|
+
const [invalid, setInvalid] = useState(false);
|
|
10
|
+
const done = useRef(false);
|
|
11
|
+
const input = useRef(null);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const element = input.current;
|
|
14
|
+
if (!element)
|
|
15
|
+
return;
|
|
16
|
+
element.focus();
|
|
17
|
+
if (initialText === undefined)
|
|
18
|
+
element.select();
|
|
19
|
+
else
|
|
20
|
+
element.setSelectionRange(element.value.length, element.value.length);
|
|
21
|
+
}, [initialText]);
|
|
22
|
+
const finish = (move) => {
|
|
23
|
+
const value = parse(draft);
|
|
24
|
+
if (value === undefined) {
|
|
25
|
+
setInvalid(true);
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
done.current = true;
|
|
29
|
+
commit(value, move);
|
|
30
|
+
return true;
|
|
31
|
+
};
|
|
32
|
+
return (_jsx("input", { ref: input, className: "rdg-editor", "aria-label": typeof column.header === 'string' ? column.header : column.id, "aria-invalid": invalid || undefined, value: draft, style: { textAlign: align === 'start' ? 'left' : align === 'end' ? 'right' : 'center' }, onChange: (event) => {
|
|
33
|
+
setDraft(event.target.value);
|
|
34
|
+
setInvalid(false);
|
|
35
|
+
}, onKeyDown: (event) => {
|
|
36
|
+
if (event.key === 'Enter') {
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
finish(event.shiftKey ? 'up' : 'down');
|
|
39
|
+
}
|
|
40
|
+
else if (event.key === 'Tab') {
|
|
41
|
+
event.preventDefault();
|
|
42
|
+
finish(event.shiftKey ? 'left' : 'right');
|
|
43
|
+
}
|
|
44
|
+
else if (event.key === 'Escape') {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
done.current = true;
|
|
47
|
+
cancel();
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
return;
|
|
51
|
+
event.stopPropagation();
|
|
52
|
+
}, onBlur: () => {
|
|
53
|
+
if (!done.current && !finish('none')) {
|
|
54
|
+
// An invalid value on focus loss: drop the edit rather than trap the focus.
|
|
55
|
+
done.current = true;
|
|
56
|
+
cancel();
|
|
57
|
+
}
|
|
58
|
+
} }));
|
|
59
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const toTsvField: (text: string) => string;
|
|
2
|
+
export declare const toTsv: (matrix: readonly (readonly string[])[]) => string;
|
|
3
|
+
/** Parses tab-separated text (quoted fields included); a trailing line break adds no empty row. */
|
|
4
|
+
export declare const parseTsv: (text: string) => string[][];
|
|
5
|
+
//# sourceMappingURL=clipboard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.d.ts","sourceRoot":"","sources":["../src/clipboard.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,UAAU,SAAU,MAAM,WAC4B,CAAA;AAEnE,eAAO,MAAM,KAAK,WAAY,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,WACI,CAAA;AAEhE,mGAAmG;AACnG,eAAO,MAAM,QAAQ,SAAU,MAAM,KAAG,MAAM,EAAE,EAiC/C,CAAA"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tab-separated text, as spreadsheets (Excel, LibreOffice, Google Sheets) copy and paste it: rows end with
|
|
3
|
+
* a line break; a field holding a tab, a line break or a quote is quoted, with quotes doubled.
|
|
4
|
+
*/
|
|
5
|
+
const needsQuotes = /[\t\n\r"]/;
|
|
6
|
+
export const toTsvField = (text) => needsQuotes.test(text) ? `"${text.replaceAll('"', '""')}"` : text;
|
|
7
|
+
export const toTsv = (matrix) => matrix.map((row) => row.map(toTsvField).join('\t')).join('\n');
|
|
8
|
+
/** Parses tab-separated text (quoted fields included); a trailing line break adds no empty row. */
|
|
9
|
+
export const parseTsv = (text) => {
|
|
10
|
+
const rows = [];
|
|
11
|
+
let row = [];
|
|
12
|
+
let field = '';
|
|
13
|
+
let quoted = false;
|
|
14
|
+
let index = 0;
|
|
15
|
+
const input = text.replace(/\r\n?/g, '\n');
|
|
16
|
+
while (index < input.length) {
|
|
17
|
+
const char = input[index];
|
|
18
|
+
if (quoted) {
|
|
19
|
+
if (char === '"') {
|
|
20
|
+
if (input[index + 1] === '"') {
|
|
21
|
+
field += '"';
|
|
22
|
+
index++;
|
|
23
|
+
}
|
|
24
|
+
else
|
|
25
|
+
quoted = false;
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
field += char;
|
|
29
|
+
}
|
|
30
|
+
else if (char === '"' && field === '')
|
|
31
|
+
quoted = true;
|
|
32
|
+
else if (char === '\t') {
|
|
33
|
+
row.push(field);
|
|
34
|
+
field = '';
|
|
35
|
+
}
|
|
36
|
+
else if (char === '\n') {
|
|
37
|
+
row.push(field);
|
|
38
|
+
rows.push(row);
|
|
39
|
+
row = [];
|
|
40
|
+
field = '';
|
|
41
|
+
}
|
|
42
|
+
else
|
|
43
|
+
field += char;
|
|
44
|
+
index++;
|
|
45
|
+
}
|
|
46
|
+
if (field !== '' || row.length > 0) {
|
|
47
|
+
row.push(field);
|
|
48
|
+
rows.push(row);
|
|
49
|
+
}
|
|
50
|
+
return rows;
|
|
51
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import './styles.css';
|
|
2
|
+
export { DataGrid, defaultCompare } from './DataGrid';
|
|
3
|
+
export { parseTsv, toTsv } from './clipboard';
|
|
4
|
+
export { navigate, tabTarget } from './keyboard';
|
|
5
|
+
export { boundsOf, selectedRowIndexes } from './selection';
|
|
6
|
+
export type { Bounds } from './selection';
|
|
7
|
+
export type { CellChange, CellContext, CellPosition, CellRange, ColumnAlign, ColumnState, DataGridColumn, DataGridHandle, DataGridProps, EditMove, EditorProps, GridKeyContext, SelectionMode, SortDirection, SortItem } from './types';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAA;AAErB,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAChD,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAC1D,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,YAAY,EACV,UAAU,EACV,WAAW,EACX,YAAY,EACZ,SAAS,EACT,WAAW,EACX,WAAW,EACX,cAAc,EACd,cAAc,EACd,aAAa,EACb,QAAQ,EACR,WAAW,EACX,cAAc,EACd,aAAa,EACb,aAAa,EACb,QAAQ,EACT,MAAM,SAAS,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CellPosition, SelectionMode } from './types';
|
|
2
|
+
export interface NavigationInput {
|
|
3
|
+
key: string;
|
|
4
|
+
ctrl: boolean;
|
|
5
|
+
cell: CellPosition;
|
|
6
|
+
rowCount: number;
|
|
7
|
+
colCount: number;
|
|
8
|
+
pageSize: number;
|
|
9
|
+
mode: SelectionMode;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Where a navigation key takes the current cell (desktop grid model, like WinForms DataGridView and WPF
|
|
13
|
+
* DataGrid); `null` when the key does not navigate.
|
|
14
|
+
*
|
|
15
|
+
* - arrows: one cell; Ctrl+arrow: to the edge of the grid in that direction
|
|
16
|
+
* - Home / End: first / last column of the row (cell mode); first / last row (row mode, like a list)
|
|
17
|
+
* - Ctrl+Home / Ctrl+End: first / last cell of the grid
|
|
18
|
+
* - PageUp / PageDown: one page of rows
|
|
19
|
+
*/
|
|
20
|
+
export declare const navigate: ({ key, ctrl, cell, rowCount, colCount, pageSize, mode }: NavigationInput) => CellPosition | null;
|
|
21
|
+
/** Tab / Shift+Tab: next / previous cell, wrapping to the next / previous row; `null` past the ends. */
|
|
22
|
+
export declare const tabTarget: (cell: CellPosition, backwards: boolean, rowCount: number, colCount: number) => CellPosition | null;
|
|
23
|
+
/** A key that starts editing by typing (a printable character without Ctrl / Meta / Alt). */
|
|
24
|
+
export declare const isTypingKey: (event: {
|
|
25
|
+
key: string;
|
|
26
|
+
ctrlKey: boolean;
|
|
27
|
+
metaKey: boolean;
|
|
28
|
+
altKey: boolean;
|
|
29
|
+
}) => boolean;
|
|
30
|
+
//# sourceMappingURL=keyboard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyboard.d.ts","sourceRoot":"","sources":["../src/keyboard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAE1D,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,OAAO,CAAA;IACb,IAAI,EAAE,YAAY,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,aAAa,CAAA;CACpB;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,QAAQ,4DAQlB,eAAe,KAAG,YAAY,GAAG,IA8BnC,CAAA;AAED,wGAAwG;AACxG,eAAO,MAAM,SAAS,SACd,YAAY,aACP,OAAO,YACR,MAAM,YACN,MAAM,KACf,YAAY,GAAG,IAIjB,CAAA;AAED,6FAA6F;AAC7F,eAAO,MAAM,WAAW,UAAW;IACjC,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,OAAO,CAAA;CAChB,YAAgF,CAAA"}
|
package/dist/keyboard.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { clamp } from './selection';
|
|
2
|
+
/**
|
|
3
|
+
* Where a navigation key takes the current cell (desktop grid model, like WinForms DataGridView and WPF
|
|
4
|
+
* DataGrid); `null` when the key does not navigate.
|
|
5
|
+
*
|
|
6
|
+
* - arrows: one cell; Ctrl+arrow: to the edge of the grid in that direction
|
|
7
|
+
* - Home / End: first / last column of the row (cell mode); first / last row (row mode, like a list)
|
|
8
|
+
* - Ctrl+Home / Ctrl+End: first / last cell of the grid
|
|
9
|
+
* - PageUp / PageDown: one page of rows
|
|
10
|
+
*/
|
|
11
|
+
export const navigate = ({ key, ctrl, cell, rowCount, colCount, pageSize, mode }) => {
|
|
12
|
+
if (rowCount === 0 || colCount === 0)
|
|
13
|
+
return null;
|
|
14
|
+
const lastRow = rowCount - 1;
|
|
15
|
+
const lastCol = colCount - 1;
|
|
16
|
+
const to = (row, col) => ({
|
|
17
|
+
row: clamp(row, 0, lastRow),
|
|
18
|
+
col: clamp(col, 0, lastCol)
|
|
19
|
+
});
|
|
20
|
+
switch (key) {
|
|
21
|
+
case 'ArrowUp':
|
|
22
|
+
return to(ctrl ? 0 : cell.row - 1, cell.col);
|
|
23
|
+
case 'ArrowDown':
|
|
24
|
+
return to(ctrl ? lastRow : cell.row + 1, cell.col);
|
|
25
|
+
case 'ArrowLeft':
|
|
26
|
+
return to(cell.row, ctrl ? 0 : cell.col - 1);
|
|
27
|
+
case 'ArrowRight':
|
|
28
|
+
return to(cell.row, ctrl ? lastCol : cell.col + 1);
|
|
29
|
+
case 'Home':
|
|
30
|
+
if (ctrl)
|
|
31
|
+
return to(0, 0);
|
|
32
|
+
return mode === 'row' ? to(0, cell.col) : to(cell.row, 0);
|
|
33
|
+
case 'End':
|
|
34
|
+
if (ctrl)
|
|
35
|
+
return to(lastRow, lastCol);
|
|
36
|
+
return mode === 'row' ? to(lastRow, cell.col) : to(cell.row, lastCol);
|
|
37
|
+
case 'PageUp':
|
|
38
|
+
return to(cell.row - Math.max(1, pageSize), cell.col);
|
|
39
|
+
case 'PageDown':
|
|
40
|
+
return to(cell.row + Math.max(1, pageSize), cell.col);
|
|
41
|
+
default:
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/** Tab / Shift+Tab: next / previous cell, wrapping to the next / previous row; `null` past the ends. */
|
|
46
|
+
export const tabTarget = (cell, backwards, rowCount, colCount) => {
|
|
47
|
+
const index = cell.row * colCount + cell.col + (backwards ? -1 : 1);
|
|
48
|
+
if (index < 0 || index >= rowCount * colCount)
|
|
49
|
+
return null;
|
|
50
|
+
return { row: Math.floor(index / colCount), col: index % colCount };
|
|
51
|
+
};
|
|
52
|
+
/** A key that starts editing by typing (a printable character without Ctrl / Meta / Alt). */
|
|
53
|
+
export const isTypingKey = (event) => event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CellPosition, CellRange, SelectionMode } from './types';
|
|
2
|
+
export interface Bounds {
|
|
3
|
+
top: number;
|
|
4
|
+
bottom: number;
|
|
5
|
+
left: number;
|
|
6
|
+
right: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const clamp: (value: number, min: number, max: number) => number;
|
|
9
|
+
export declare const boundsOf: (range: CellRange) => Bounds;
|
|
10
|
+
export declare const single: (cell: CellPosition) => CellRange;
|
|
11
|
+
/** Row mode: a range always covers every column. */
|
|
12
|
+
export declare const fullRows: (range: CellRange, colCount: number) => CellRange;
|
|
13
|
+
export declare const normalizeRanges: (ranges: readonly CellRange[], mode: SelectionMode, colCount: number) => CellRange[];
|
|
14
|
+
export declare const inBounds: (bounds: Bounds, row: number, col: number) => boolean;
|
|
15
|
+
export declare const cellSelected: (ranges: readonly CellRange[], row: number, col: number) => boolean;
|
|
16
|
+
export declare const rowSelected: (ranges: readonly CellRange[], row: number) => boolean;
|
|
17
|
+
/** Ranges that still fit after the rows or columns changed (removed rows, hidden columns). */
|
|
18
|
+
export declare const clampRanges: (ranges: readonly CellRange[], rowCount: number, colCount: number) => CellRange[];
|
|
19
|
+
export declare const sameCell: (a: CellPosition | null | undefined, b: CellPosition | null | undefined) => boolean;
|
|
20
|
+
export declare const sameRanges: (a: readonly CellRange[], b: readonly CellRange[]) => boolean;
|
|
21
|
+
/** Rows covered by the ranges, ascending, without repeats. */
|
|
22
|
+
export declare const selectedRowIndexes: (ranges: readonly CellRange[]) => number[];
|
|
23
|
+
//# sourceMappingURL=selection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.d.ts","sourceRoot":"","sources":["../src/selection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAErE,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;CACd;AAED,eAAO,MAAM,KAAK,UAAW,MAAM,OAAO,MAAM,OAAO,MAAM,WACxB,CAAA;AAErC,eAAO,MAAM,QAAQ,UAAW,SAAS,KAAG,MAK1C,CAAA;AAEF,eAAO,MAAM,MAAM,SAAU,YAAY,KAAG,SAA4C,CAAA;AAExF,oDAAoD;AACpD,eAAO,MAAM,QAAQ,UAAW,SAAS,YAAY,MAAM,KAAG,SAG5D,CAAA;AAEF,eAAO,MAAM,eAAe,WAClB,SAAS,SAAS,EAAE,QACtB,aAAa,YACT,MAAM,KACf,SAAS,EAAuF,CAAA;AAEnG,eAAO,MAAM,QAAQ,WAAY,MAAM,OAAO,MAAM,OAAO,MAAM,YACuB,CAAA;AAExF,eAAO,MAAM,YAAY,WAAY,SAAS,SAAS,EAAE,OAAO,MAAM,OAAO,MAAM,YACtB,CAAA;AAE7D,eAAO,MAAM,WAAW,WAAY,SAAS,SAAS,EAAE,OAAO,MAAM,YAIjE,CAAA;AAEJ,8FAA8F;AAC9F,eAAO,MAAM,WAAW,WACd,SAAS,SAAS,EAAE,YAClB,MAAM,YACN,MAAM,KACf,SAAS,EAOX,CAAA;AAED,eAAO,MAAM,QAAQ,MAAO,YAAY,GAAG,IAAI,GAAG,SAAS,KAAK,YAAY,GAAG,IAAI,GAAG,SAAS,YAChC,CAAA;AAE/D,eAAO,MAAM,UAAU,MAAO,SAAS,SAAS,EAAE,KAAK,SAAS,SAAS,EAAE,YAKxE,CAAA;AAEH,8DAA8D;AAC9D,eAAO,MAAM,kBAAkB,WAAY,SAAS,SAAS,EAAE,KAAG,MAAM,EAOvE,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
|
|
2
|
+
export const boundsOf = (range) => ({
|
|
3
|
+
top: Math.min(range.anchor.row, range.focus.row),
|
|
4
|
+
bottom: Math.max(range.anchor.row, range.focus.row),
|
|
5
|
+
left: Math.min(range.anchor.col, range.focus.col),
|
|
6
|
+
right: Math.max(range.anchor.col, range.focus.col)
|
|
7
|
+
});
|
|
8
|
+
export const single = (cell) => ({ anchor: cell, focus: cell });
|
|
9
|
+
/** Row mode: a range always covers every column. */
|
|
10
|
+
export const fullRows = (range, colCount) => ({
|
|
11
|
+
anchor: { row: range.anchor.row, col: 0 },
|
|
12
|
+
focus: { row: range.focus.row, col: Math.max(0, colCount - 1) }
|
|
13
|
+
});
|
|
14
|
+
export const normalizeRanges = (ranges, mode, colCount) => (mode === 'row' ? ranges.map((range) => fullRows(range, colCount)) : [...ranges]);
|
|
15
|
+
export const inBounds = (bounds, row, col) => row >= bounds.top && row <= bounds.bottom && col >= bounds.left && col <= bounds.right;
|
|
16
|
+
export const cellSelected = (ranges, row, col) => ranges.some((range) => inBounds(boundsOf(range), row, col));
|
|
17
|
+
export const rowSelected = (ranges, row) => ranges.some((range) => {
|
|
18
|
+
const bounds = boundsOf(range);
|
|
19
|
+
return row >= bounds.top && row <= bounds.bottom;
|
|
20
|
+
});
|
|
21
|
+
/** Ranges that still fit after the rows or columns changed (removed rows, hidden columns). */
|
|
22
|
+
export const clampRanges = (ranges, rowCount, colCount) => {
|
|
23
|
+
if (rowCount === 0 || colCount === 0)
|
|
24
|
+
return [];
|
|
25
|
+
const fit = (cell) => ({
|
|
26
|
+
row: clamp(cell.row, 0, rowCount - 1),
|
|
27
|
+
col: clamp(cell.col, 0, colCount - 1)
|
|
28
|
+
});
|
|
29
|
+
return ranges.map((range) => ({ anchor: fit(range.anchor), focus: fit(range.focus) }));
|
|
30
|
+
};
|
|
31
|
+
export const sameCell = (a, b) => a === b || (!!a && !!b && a.row === b.row && a.col === b.col);
|
|
32
|
+
export const sameRanges = (a, b) => a.length === b.length &&
|
|
33
|
+
a.every((range, index) => sameCell(range.anchor, b[index]?.anchor) && sameCell(range.focus, b[index]?.focus));
|
|
34
|
+
/** Rows covered by the ranges, ascending, without repeats. */
|
|
35
|
+
export const selectedRowIndexes = (ranges) => {
|
|
36
|
+
const rows = new Set();
|
|
37
|
+
for (const range of ranges) {
|
|
38
|
+
const bounds = boundsOf(range);
|
|
39
|
+
for (let row = bounds.top; row <= bounds.bottom; row++)
|
|
40
|
+
rows.add(row);
|
|
41
|
+
}
|
|
42
|
+
return [...rows].sort((a, b) => a - b);
|
|
43
|
+
};
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @pablovsouza/react-datagrid: theme by CSS variables (set them on `.rdg`, a parent or :root).
|
|
3
|
+
*
|
|
4
|
+
* The base styles sit in the `rdg` layer. Layers declared first lose: an app with its own layers (Tailwind)
|
|
5
|
+
* declares `@layer rdg;` before them, so its utilities override the grid's base styles. The
|
|
6
|
+
* selection colours are the exception: unlayered on purpose, so a colour a cell sets for itself (status
|
|
7
|
+
* colours, utility classes) never makes selected text unreadable. Change them through the variables.
|
|
8
|
+
*/
|
|
9
|
+
@layer rdg {
|
|
10
|
+
.rdg {
|
|
11
|
+
--rdg-bg: Canvas;
|
|
12
|
+
--rdg-fg: CanvasText;
|
|
13
|
+
--rdg-border: color-mix(in srgb, CanvasText 14%, transparent);
|
|
14
|
+
--rdg-header-bg: color-mix(in srgb, CanvasText 5%, Canvas);
|
|
15
|
+
--rdg-header-fg: CanvasText;
|
|
16
|
+
--rdg-row-odd-bg: transparent;
|
|
17
|
+
--rdg-row-hover-bg: color-mix(in srgb, CanvasText 6%, transparent);
|
|
18
|
+
--rdg-selected-bg: Highlight;
|
|
19
|
+
--rdg-selected-fg: HighlightText;
|
|
20
|
+
/* Selection while the grid does not have the focus (desktop grids grey it out; default: the same). */
|
|
21
|
+
--rdg-selected-bg-inactive: var(--rdg-selected-bg);
|
|
22
|
+
--rdg-selected-fg-inactive: var(--rdg-selected-fg);
|
|
23
|
+
/* Cell mode: the cells of a range, and the current cell's outline. */
|
|
24
|
+
--rdg-range-bg: color-mix(in srgb, var(--rdg-selected-bg) 22%, transparent);
|
|
25
|
+
--rdg-range-fg: inherit;
|
|
26
|
+
--rdg-active-outline: var(--rdg-selected-bg);
|
|
27
|
+
--rdg-editor-bg: Canvas;
|
|
28
|
+
--rdg-editor-fg: CanvasText;
|
|
29
|
+
--rdg-invalid: #dc2626;
|
|
30
|
+
--rdg-font-size: inherit;
|
|
31
|
+
--rdg-cell-padding: 0 8px;
|
|
32
|
+
|
|
33
|
+
position: relative;
|
|
34
|
+
display: flex;
|
|
35
|
+
flex-direction: column;
|
|
36
|
+
min-height: 0;
|
|
37
|
+
min-width: 0;
|
|
38
|
+
background: var(--rdg-bg);
|
|
39
|
+
color: var(--rdg-fg);
|
|
40
|
+
font-size: var(--rdg-font-size);
|
|
41
|
+
outline: none;
|
|
42
|
+
user-select: none;
|
|
43
|
+
-webkit-user-select: none;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.rdg-scroller {
|
|
47
|
+
position: relative;
|
|
48
|
+
flex: 1;
|
|
49
|
+
min-height: 0;
|
|
50
|
+
overflow: auto;
|
|
51
|
+
overscroll-behavior: contain;
|
|
52
|
+
touch-action: pan-x pan-y;
|
|
53
|
+
-webkit-overflow-scrolling: touch;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* The header colour may be translucent or a gradient: the grid colour underneath keeps it opaque. */
|
|
57
|
+
.rdg-header {
|
|
58
|
+
position: sticky;
|
|
59
|
+
top: 0;
|
|
60
|
+
z-index: 2;
|
|
61
|
+
width: 100%;
|
|
62
|
+
background: var(--rdg-bg);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.rdg-header-row {
|
|
66
|
+
background: var(--rdg-header-bg);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.rdg-header-row,
|
|
70
|
+
.rdg-row {
|
|
71
|
+
display: flex;
|
|
72
|
+
width: 100%;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.rdg-header-cell {
|
|
76
|
+
position: relative;
|
|
77
|
+
display: flex;
|
|
78
|
+
align-items: center;
|
|
79
|
+
gap: 4px;
|
|
80
|
+
box-sizing: border-box;
|
|
81
|
+
padding: var(--rdg-cell-padding);
|
|
82
|
+
overflow: hidden;
|
|
83
|
+
color: var(--rdg-header-fg);
|
|
84
|
+
border-bottom: 1px solid var(--rdg-border);
|
|
85
|
+
font-weight: 600;
|
|
86
|
+
white-space: nowrap;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.rdg-header-label {
|
|
90
|
+
flex: 1;
|
|
91
|
+
min-width: 0;
|
|
92
|
+
overflow: hidden;
|
|
93
|
+
text-overflow: ellipsis;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.rdg-sortable {
|
|
97
|
+
cursor: pointer;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.rdg-sort-indicator {
|
|
101
|
+
flex: none;
|
|
102
|
+
font-size: 0.7em;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
.rdg-resize-handle {
|
|
106
|
+
position: absolute;
|
|
107
|
+
top: 0;
|
|
108
|
+
right: 0;
|
|
109
|
+
bottom: 0;
|
|
110
|
+
width: 6px;
|
|
111
|
+
cursor: col-resize;
|
|
112
|
+
touch-action: none;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
.rdg-resize-handle:hover {
|
|
116
|
+
background: var(--rdg-border);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
.rdg-body {
|
|
120
|
+
position: relative;
|
|
121
|
+
width: 100%;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.rdg-row {
|
|
125
|
+
position: absolute;
|
|
126
|
+
top: 0;
|
|
127
|
+
left: 0;
|
|
128
|
+
box-sizing: border-box;
|
|
129
|
+
border-bottom: 1px solid var(--rdg-border);
|
|
130
|
+
background: var(--rdg-bg);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.rdg-row-odd {
|
|
134
|
+
background: color-mix(in srgb, var(--rdg-row-odd-bg), var(--rdg-bg));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
.rdg-row:hover {
|
|
138
|
+
background: var(--rdg-row-hover-bg);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.rdg-cell {
|
|
142
|
+
display: flex;
|
|
143
|
+
align-items: center;
|
|
144
|
+
box-sizing: border-box;
|
|
145
|
+
padding: var(--rdg-cell-padding);
|
|
146
|
+
overflow: hidden;
|
|
147
|
+
white-space: nowrap;
|
|
148
|
+
background: inherit;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.rdg-cell[data-pinned],
|
|
152
|
+
.rdg-header-cell[data-pinned] {
|
|
153
|
+
background-color: var(--rdg-bg);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.rdg-header-cell[data-pinned] {
|
|
157
|
+
background: var(--rdg-header-bg-pinned, var(--rdg-bg));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.rdg-cell-text {
|
|
161
|
+
flex: 1;
|
|
162
|
+
min-width: 0;
|
|
163
|
+
overflow: hidden;
|
|
164
|
+
text-overflow: ellipsis;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.rdg-align-start {
|
|
168
|
+
justify-content: flex-start;
|
|
169
|
+
text-align: left;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.rdg-align-center {
|
|
173
|
+
justify-content: center;
|
|
174
|
+
text-align: center;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.rdg-align-end {
|
|
178
|
+
justify-content: flex-end;
|
|
179
|
+
text-align: right;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.rdg-align-end .rdg-cell-text,
|
|
183
|
+
.rdg-align-end .rdg-header-label {
|
|
184
|
+
text-align: right;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
.rdg-align-center .rdg-cell-text,
|
|
188
|
+
.rdg-align-center .rdg-header-label {
|
|
189
|
+
text-align: center;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
.rdg[data-selection-mode='cell'] .rdg-cell[data-active] {
|
|
193
|
+
outline: 2px solid var(--rdg-active-outline);
|
|
194
|
+
outline-offset: -2px;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
.rdg-editor,
|
|
198
|
+
.rdg-editor-slot {
|
|
199
|
+
width: 100%;
|
|
200
|
+
min-width: 0;
|
|
201
|
+
height: 100%;
|
|
202
|
+
box-sizing: border-box;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
.rdg-editor {
|
|
206
|
+
margin: 0;
|
|
207
|
+
border: 0;
|
|
208
|
+
padding: 0;
|
|
209
|
+
background: var(--rdg-editor-bg);
|
|
210
|
+
color: var(--rdg-editor-fg);
|
|
211
|
+
font: inherit;
|
|
212
|
+
outline: none;
|
|
213
|
+
user-select: text;
|
|
214
|
+
-webkit-user-select: text;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
.rdg-editor[aria-invalid='true'] {
|
|
218
|
+
box-shadow: inset 0 0 0 2px var(--rdg-invalid);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
.rdg-cell[data-editing] {
|
|
222
|
+
background: var(--rdg-editor-bg);
|
|
223
|
+
padding: 0 4px;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
.rdg-empty {
|
|
227
|
+
padding: 24px 8px;
|
|
228
|
+
text-align: center;
|
|
229
|
+
opacity: 0.7;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/* Selection colours: unlayered (see the top). Selected rows (row mode) and range cells (cell mode). */
|
|
234
|
+
.rdg-row[aria-selected='true'],
|
|
235
|
+
.rdg-row[aria-selected='true'] .rdg-cell {
|
|
236
|
+
background: var(--rdg-selected-bg-inactive);
|
|
237
|
+
}
|
|
238
|
+
.rdg-row[aria-selected='true'] .rdg-cell,
|
|
239
|
+
.rdg-row[aria-selected='true'] .rdg-cell * {
|
|
240
|
+
color: var(--rdg-selected-fg-inactive);
|
|
241
|
+
}
|
|
242
|
+
.rdg:focus-within .rdg-row[aria-selected='true'],
|
|
243
|
+
.rdg:focus-within .rdg-row[aria-selected='true'] .rdg-cell {
|
|
244
|
+
background: var(--rdg-selected-bg);
|
|
245
|
+
}
|
|
246
|
+
.rdg:focus-within .rdg-row[aria-selected='true'] .rdg-cell,
|
|
247
|
+
.rdg:focus-within .rdg-row[aria-selected='true'] .rdg-cell * {
|
|
248
|
+
color: var(--rdg-selected-fg);
|
|
249
|
+
}
|
|
250
|
+
.rdg-cell[aria-selected='true']:not([data-editing]) {
|
|
251
|
+
background: var(--rdg-range-bg);
|
|
252
|
+
}
|
|
253
|
+
.rdg-cell[aria-selected='true']:not([data-editing]),
|
|
254
|
+
.rdg-cell[aria-selected='true']:not([data-editing]) * {
|
|
255
|
+
color: var(--rdg-range-fg);
|
|
256
|
+
}
|