@omnigrid/react 0.1.17 → 0.1.19
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/dist/OmniGrid.js +23 -6
- package/dist/content.d.ts +1 -0
- package/dist/content.js +4 -0
- package/dist/controls.d.ts +19 -0
- package/dist/controls.js +381 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/popup.d.ts +11 -0
- package/dist/popup.js +77 -0
- package/dist/useGrid.js +29 -0
- package/package.json +1 -1
package/dist/OmniGrid.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { createElement, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { flushSync } from "react-dom";
|
|
4
|
-
import { DomPool } from "@omnigrid/core";
|
|
4
|
+
import { DomPool, isActiveCondition } from "@omnigrid/core";
|
|
5
5
|
import { contentToReactNode, hasRenderableContent } from "./content";
|
|
6
6
|
import { computeGroupSegments } from "./groupHeader";
|
|
7
7
|
import { PooledRowPane } from "./pooledRow";
|
|
8
8
|
import { createCellRoot } from "./reactRoot";
|
|
9
9
|
import { renderDefaultOverlay } from "./overlays";
|
|
10
|
+
import { PopupHost } from "./popup";
|
|
10
11
|
import { resolveCellClasses, resolveCellStyle, resolveColumnPresentation, resolveGroupHeaderClasses, resolveGroupHeaderStyle, resolveHeaderClasses, resolveHeaderStyle, } from "./styling";
|
|
11
12
|
import { useGrid } from "./useGrid";
|
|
12
13
|
function getCellValue(row, column) {
|
|
@@ -60,6 +61,7 @@ function createRowLayerState() {
|
|
|
60
61
|
export function OmniGrid({ className, style, slotComponents, ...options }) {
|
|
61
62
|
const { grid, state, viewportData } = useGrid(options);
|
|
62
63
|
const viewportRef = useRef(null);
|
|
64
|
+
const layoutRef = useRef(null);
|
|
63
65
|
const rowsLayerRef = useRef(null);
|
|
64
66
|
const pinnedLeftRowsLayerRef = useRef(null);
|
|
65
67
|
const pinnedRightRowsLayerRef = useRef(null);
|
|
@@ -130,6 +132,11 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
|
|
|
130
132
|
const suppressRowHoverHighlight = options.suppressRowHoverHighlight ?? false;
|
|
131
133
|
const viewportClassName = ["omnigrid", className, suppressRowHoverHighlight ? "omnigrid-no-row-hover" : null].filter(Boolean).join(" ");
|
|
132
134
|
const overlayState = state.overlay;
|
|
135
|
+
const filterModelSnapshot = grid.getFilterModel();
|
|
136
|
+
const isColumnFiltered = (column) => {
|
|
137
|
+
const entry = filterModelSnapshot[column.id] ?? (column.field ? filterModelSnapshot[column.field] : undefined);
|
|
138
|
+
return entry !== undefined && entry.conditions.some(isActiveCondition);
|
|
139
|
+
};
|
|
133
140
|
const overlayScope = overlayState.type
|
|
134
141
|
? (overlayState.scope ?? (overlayState.type === "error" ? "full" : "bodyOnly"))
|
|
135
142
|
: undefined;
|
|
@@ -713,9 +720,19 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
|
|
|
713
720
|
}, tabIndex: 0, style: headerCellLayout, children: item.column.headerRenderer ? (contentToReactNode(item.column.headerRenderer(item.column), {
|
|
714
721
|
registry: slotComponentsRef.current,
|
|
715
722
|
context: buildRenderContext,
|
|
716
|
-
})) : (_jsxs(_Fragment, { children: [_jsx("span", { className: "omnigrid-header-label", children: item.column.header ?? item.column.id }),
|
|
717
|
-
|
|
718
|
-
|
|
723
|
+
})) : (_jsxs(_Fragment, { children: [_jsx("span", { className: "omnigrid-header-label", children: item.column.header ?? item.column.id }), _jsxs("span", { className: "omnigrid-header-tools", children: [item.column.sortState && (_jsxs("span", { className: "omnigrid-sort-indicator", "aria-hidden": "true", children: [contentToReactNode(item.column.sortState === "asc"
|
|
724
|
+
? grid.icons.get("arrow-up")
|
|
725
|
+
: grid.icons.get("arrow-down"), { registry: slotComponentsRef.current, context: buildRenderContext }), item.column.sortIndex !== undefined && (_jsx("span", { className: "omnigrid-sort-priority", children: item.column.sortIndex }))] })), grid.hasFilterMenu() && item.column.filterable !== false && (_jsx("button", { type: "button", className: "omnigrid-filter-trigger", "data-omnigrid-filter-trigger": "", "data-active": isColumnFiltered(item.column) ? "true" : undefined, "aria-label": `Filter ${item.column.header ?? item.column.id}`, "aria-haspopup": "dialog", tabIndex: -1, onClick: (event) => {
|
|
726
|
+
event.stopPropagation();
|
|
727
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
728
|
+
const layoutRect = layoutRef.current?.getBoundingClientRect() ?? { left: 0, top: 0, width: 0, height: 0 };
|
|
729
|
+
grid.requestFilterMenu(item.column.id, {
|
|
730
|
+
x: rect.left - layoutRect.left,
|
|
731
|
+
y: rect.top - layoutRect.top,
|
|
732
|
+
width: rect.width,
|
|
733
|
+
height: rect.height,
|
|
734
|
+
});
|
|
735
|
+
}, onKeyDown: (event) => event.stopPropagation(), children: _jsx("svg", { viewBox: "0 0 24 24", width: "12", height: "12", fill: "currentColor", "aria-hidden": "true", children: _jsx("path", { d: "M3 4h18l-7 8.5V20l-4-2v-5.5z" }) }) }))] })] })) }, item.column.id));
|
|
719
736
|
});
|
|
720
737
|
const renderGroupCells = (depth, columns, originX) => computeGroupSegments(viewportData.columnGroups, columns, depth, originX).map((segment, index) => {
|
|
721
738
|
const groupParams = {
|
|
@@ -781,7 +798,7 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
|
|
|
781
798
|
top: headerHeight,
|
|
782
799
|
width,
|
|
783
800
|
} }));
|
|
784
|
-
return (_jsxs("div", { className: "omnigrid-layout", style: { ...style, position: "relative", ...(isFullOverlay ? { overflow: "hidden" } : {}) }, children: [renderSlot("top"), _jsxs("div", { className: "omnigrid-layout-body", children: [renderSlot("left"), _jsxs("div", { className: "omnigrid-viewport-container", style: {
|
|
801
|
+
return (_jsxs("div", { className: "omnigrid-layout", ref: layoutRef, style: { ...style, position: "relative", ...(isFullOverlay ? { overflow: "hidden" } : {}) }, children: [renderSlot("top"), _jsxs("div", { className: "omnigrid-layout-body", children: [renderSlot("left"), _jsxs("div", { className: "omnigrid-viewport-container", style: {
|
|
785
802
|
flex: "1 1 0",
|
|
786
803
|
minHeight: 0,
|
|
787
804
|
minWidth: 0,
|
|
@@ -832,5 +849,5 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
|
|
|
832
849
|
top: headerHeight,
|
|
833
850
|
width: "100%",
|
|
834
851
|
zIndex: 4,
|
|
835
|
-
}, children: renderOverlayContent(overlayState) }))] }), renderSlot("right")] }), renderSlot("bottom"), overlayState.type && isFullOverlay && (_jsx("div", { className: "omnigrid-overlay omnigrid-overlay-full", role: "status", children: renderOverlayContent(overlayState) }))] }));
|
|
852
|
+
}, children: renderOverlayContent(overlayState) }))] }), renderSlot("right")] }), renderSlot("bottom"), _jsx(PopupHost, { grid: grid }), overlayState.type && isFullOverlay && (_jsx("div", { className: "omnigrid-overlay omnigrid-overlay-full", role: "status", children: renderOverlayContent(overlayState) }))] }));
|
|
836
853
|
}
|
package/dist/content.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export declare function hasRenderableContent(content: ReactNode): boolean;
|
|
|
26
26
|
* Materialises the core's headless content into a ReactNode:
|
|
27
27
|
* - `string | number | boolean` → scalar;
|
|
28
28
|
* - CheckboxControl → `<input type="checkbox">`;
|
|
29
|
+
* - `UIControlDescriptor` → native control (see `controls.ts`);
|
|
29
30
|
* - `SlotNodeContent` → declarative node (buttons etc.);
|
|
30
31
|
* - `SlotComponentContent` → component from the adapter registry;
|
|
31
32
|
* - `SlotHtmlContent` → `<div dangerouslySetInnerHTML>`;
|
package/dist/content.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { cloneElement, createElement, Fragment, isValidElement } from "react";
|
|
2
|
+
import { isUIControlDescriptor, renderControl } from "./controls";
|
|
2
3
|
export function isCheckboxControl(value) {
|
|
3
4
|
return typeof value === "object" && value !== null && value.type === "@omnigrid/checkbox";
|
|
4
5
|
}
|
|
@@ -129,6 +130,7 @@ function slotNodeToReact(node, bridge) {
|
|
|
129
130
|
* Materialises the core's headless content into a ReactNode:
|
|
130
131
|
* - `string | number | boolean` → scalar;
|
|
131
132
|
* - CheckboxControl → `<input type="checkbox">`;
|
|
133
|
+
* - `UIControlDescriptor` → native control (see `controls.ts`);
|
|
132
134
|
* - `SlotNodeContent` → declarative node (buttons etc.);
|
|
133
135
|
* - `SlotComponentContent` → component from the adapter registry;
|
|
134
136
|
* - `SlotHtmlContent` → `<div dangerouslySetInnerHTML>`;
|
|
@@ -141,6 +143,8 @@ export function contentToReactNode(content, bridge) {
|
|
|
141
143
|
return content;
|
|
142
144
|
if (isCheckboxControl(content))
|
|
143
145
|
return renderCheckboxControl(content);
|
|
146
|
+
if (isUIControlDescriptor(content))
|
|
147
|
+
return renderControl(content);
|
|
144
148
|
if (isSlotNodeContent(content))
|
|
145
149
|
return slotNodeToReact(content, bridge);
|
|
146
150
|
if (isSlotComponentContent(content)) {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { UIControlDescriptor, UIInputControl } from "@omnigrid/core";
|
|
3
|
+
/** Type guard for core `UIControlDescriptor` objects. */
|
|
4
|
+
export declare function isUIControlDescriptor(value: unknown): value is UIControlDescriptor;
|
|
5
|
+
export interface DebouncedInputProps {
|
|
6
|
+
descriptor: UIInputControl;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Text/number input that keeps typing in local state and forwards the value to
|
|
10
|
+
* the descriptor callback after `debounceMs` of inactivity. Pending input is
|
|
11
|
+
* flushed on Enter, blur and unmount so it is never lost.
|
|
12
|
+
*/
|
|
13
|
+
export declare function DebouncedInput({ descriptor }: DebouncedInputProps): ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* Renders one framework-agnostic control descriptor with native HTML elements.
|
|
16
|
+
* Descriptor `id`s become React keys, so re-describing a popup updates
|
|
17
|
+
* elements in place instead of remounting them.
|
|
18
|
+
*/
|
|
19
|
+
export declare function renderControl(descriptor: UIControlDescriptor): ReactNode;
|
package/dist/controls.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { createElement, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { createPortal } from "react-dom";
|
|
3
|
+
const CONTROL_TYPES = new Set(["text", "number", "date", "select", "checkbox", "button", "label", "multi-select", "group"]);
|
|
4
|
+
/** Type guard for core `UIControlDescriptor` objects. */
|
|
5
|
+
export function isUIControlDescriptor(value) {
|
|
6
|
+
if (typeof value !== "object" || value === null)
|
|
7
|
+
return false;
|
|
8
|
+
const candidate = value;
|
|
9
|
+
return typeof candidate.type === "string" && CONTROL_TYPES.has(candidate.type) && typeof candidate.id === "string";
|
|
10
|
+
}
|
|
11
|
+
const DEFAULT_DEBOUNCE_MS = 250;
|
|
12
|
+
/**
|
|
13
|
+
* Text/number input that keeps typing in local state and forwards the value to
|
|
14
|
+
* the descriptor callback after `debounceMs` of inactivity. Pending input is
|
|
15
|
+
* flushed on Enter, blur and unmount so it is never lost.
|
|
16
|
+
*/
|
|
17
|
+
export function DebouncedInput({ descriptor }) {
|
|
18
|
+
const [local, setLocal] = useState(descriptor.value);
|
|
19
|
+
const timerRef = useRef(null);
|
|
20
|
+
const pendingRef = useRef(null);
|
|
21
|
+
const onChangeRef = useRef(descriptor.onChange);
|
|
22
|
+
onChangeRef.current = descriptor.onChange;
|
|
23
|
+
const flush = () => {
|
|
24
|
+
if (timerRef.current !== null) {
|
|
25
|
+
clearTimeout(timerRef.current);
|
|
26
|
+
timerRef.current = null;
|
|
27
|
+
}
|
|
28
|
+
if (pendingRef.current !== null) {
|
|
29
|
+
const value = pendingRef.current;
|
|
30
|
+
pendingRef.current = null;
|
|
31
|
+
onChangeRef.current(value);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
// Adopt external value changes (e.g. clearFilters) unless the user has unsent input.
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (pendingRef.current === null)
|
|
37
|
+
setLocal(descriptor.value);
|
|
38
|
+
}, [descriptor.value]);
|
|
39
|
+
useEffect(() => flush, []);
|
|
40
|
+
const handleChange = (event) => {
|
|
41
|
+
const next = event.target.value;
|
|
42
|
+
setLocal(next);
|
|
43
|
+
pendingRef.current = next;
|
|
44
|
+
if (timerRef.current !== null)
|
|
45
|
+
clearTimeout(timerRef.current);
|
|
46
|
+
timerRef.current = setTimeout(flush, descriptor.debounceMs ?? DEFAULT_DEBOUNCE_MS);
|
|
47
|
+
};
|
|
48
|
+
const handleKeyDown = (event) => {
|
|
49
|
+
if (event.key === "Enter") {
|
|
50
|
+
descriptor.onEnter?.();
|
|
51
|
+
flush();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const handleClear = () => {
|
|
55
|
+
setLocal("");
|
|
56
|
+
pendingRef.current = "";
|
|
57
|
+
flush();
|
|
58
|
+
};
|
|
59
|
+
return createElement("div", { className: "omnigrid-control-input-wrapper" }, createElement("input", {
|
|
60
|
+
className: ["omnigrid-control-input", descriptor.className].filter(Boolean).join(" "),
|
|
61
|
+
type: descriptor.type === "number" ? "number" : descriptor.type === "date" ? "date" : "text",
|
|
62
|
+
value: local,
|
|
63
|
+
placeholder: descriptor.placeholder,
|
|
64
|
+
disabled: descriptor.disabled,
|
|
65
|
+
"aria-label": descriptor.label ?? descriptor.placeholder,
|
|
66
|
+
onChange: handleChange,
|
|
67
|
+
onBlur: flush,
|
|
68
|
+
onKeyDown: handleKeyDown,
|
|
69
|
+
}), local.length > 0 &&
|
|
70
|
+
createElement("button", {
|
|
71
|
+
type: "button",
|
|
72
|
+
className: "omnigrid-control-clear",
|
|
73
|
+
"aria-label": "Clear",
|
|
74
|
+
onClick: handleClear,
|
|
75
|
+
}, "×"));
|
|
76
|
+
}
|
|
77
|
+
function withLabel(descriptor, field, inline = false) {
|
|
78
|
+
const className = ["omnigrid-control", `omnigrid-control-${descriptor.type}`].join(" ");
|
|
79
|
+
if (!descriptor.label)
|
|
80
|
+
return createElement("div", { key: descriptor.id, className }, field);
|
|
81
|
+
const label = createElement("span", { className: "omnigrid-control-label" }, descriptor.label);
|
|
82
|
+
return inline ? createElement("label", { key: descriptor.id, className }, field, label) : createElement("label", { key: descriptor.id, className }, label, field);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Custom-styled, keyboard-navigable drop-down select. Replaces the native
|
|
86
|
+
* `<select>` so the control looks identical in light and dark themes and can
|
|
87
|
+
* be fully styled through CSS variables.
|
|
88
|
+
*/
|
|
89
|
+
function SelectControl({ descriptor }) {
|
|
90
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
91
|
+
const [focusedIndex, setFocusedIndex] = useState(null);
|
|
92
|
+
const [anchorRect, setAnchorRect] = useState(null);
|
|
93
|
+
const buttonRef = useRef(null);
|
|
94
|
+
const listboxRef = useRef(null);
|
|
95
|
+
const selectedIndex = useMemo(() => descriptor.options.findIndex((option) => option.value === descriptor.value), [descriptor.options, descriptor.value]);
|
|
96
|
+
const listboxId = `${descriptor.id}-listbox`;
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (!isOpen) {
|
|
99
|
+
setFocusedIndex(null);
|
|
100
|
+
setAnchorRect(null);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const rect = buttonRef.current ? buttonRef.current.getBoundingClientRect() : null;
|
|
104
|
+
if (rect) {
|
|
105
|
+
setAnchorRect({ x: rect.left, y: rect.bottom, width: rect.width });
|
|
106
|
+
}
|
|
107
|
+
setFocusedIndex(selectedIndex === -1 ? 0 : selectedIndex);
|
|
108
|
+
}, [isOpen, selectedIndex]);
|
|
109
|
+
const close = useCallback(() => {
|
|
110
|
+
setIsOpen(false);
|
|
111
|
+
setFocusedIndex(null);
|
|
112
|
+
setAnchorRect(null);
|
|
113
|
+
buttonRef.current?.focus();
|
|
114
|
+
}, []);
|
|
115
|
+
const selectOption = useCallback((index) => {
|
|
116
|
+
const option = descriptor.options[index];
|
|
117
|
+
if (!option || option.disabled)
|
|
118
|
+
return;
|
|
119
|
+
descriptor.onChange(option.value);
|
|
120
|
+
close();
|
|
121
|
+
}, [descriptor, close]);
|
|
122
|
+
const moveFocus = useCallback((delta) => {
|
|
123
|
+
if (focusedIndex === null) {
|
|
124
|
+
setFocusedIndex(0);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const length = descriptor.options.length;
|
|
128
|
+
let next = focusedIndex + delta;
|
|
129
|
+
while (next >= 0 && next < length && descriptor.options[next]?.disabled) {
|
|
130
|
+
next += delta;
|
|
131
|
+
}
|
|
132
|
+
if (next >= 0 && next < length)
|
|
133
|
+
setFocusedIndex(next);
|
|
134
|
+
}, [descriptor.options, focusedIndex]);
|
|
135
|
+
const handleButtonKeyDown = useCallback((event) => {
|
|
136
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp" || event.key === "Enter" || event.key === " ") {
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
setIsOpen(true);
|
|
139
|
+
}
|
|
140
|
+
}, []);
|
|
141
|
+
const handleListboxKeyDown = useCallback((event) => {
|
|
142
|
+
switch (event.key) {
|
|
143
|
+
case "ArrowDown":
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
moveFocus(1);
|
|
146
|
+
break;
|
|
147
|
+
case "ArrowUp":
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
moveFocus(-1);
|
|
150
|
+
break;
|
|
151
|
+
case "Home":
|
|
152
|
+
event.preventDefault();
|
|
153
|
+
setFocusedIndex(0);
|
|
154
|
+
break;
|
|
155
|
+
case "End":
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
setFocusedIndex(descriptor.options.length - 1);
|
|
158
|
+
break;
|
|
159
|
+
case "Enter":
|
|
160
|
+
event.preventDefault();
|
|
161
|
+
if (focusedIndex !== null)
|
|
162
|
+
selectOption(focusedIndex);
|
|
163
|
+
break;
|
|
164
|
+
case "Escape":
|
|
165
|
+
event.preventDefault();
|
|
166
|
+
close();
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}, [descriptor.options.length, focusedIndex, moveFocus, selectOption, close]);
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
if (!isOpen)
|
|
172
|
+
return;
|
|
173
|
+
const handlePointerDown = (event) => {
|
|
174
|
+
const target = event.target;
|
|
175
|
+
if (buttonRef.current &&
|
|
176
|
+
!buttonRef.current.contains(target) &&
|
|
177
|
+
listboxRef.current &&
|
|
178
|
+
!listboxRef.current.contains(target)) {
|
|
179
|
+
close();
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
document.addEventListener("mousedown", handlePointerDown);
|
|
183
|
+
return () => {
|
|
184
|
+
document.removeEventListener("mousedown", handlePointerDown);
|
|
185
|
+
};
|
|
186
|
+
}, [isOpen, close]);
|
|
187
|
+
// Keep the portaled listbox anchored to the trigger while anything scrolls.
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
if (!isOpen || !anchorRect)
|
|
190
|
+
return;
|
|
191
|
+
const reposition = () => {
|
|
192
|
+
const rect = buttonRef.current ? buttonRef.current.getBoundingClientRect() : null;
|
|
193
|
+
if (rect)
|
|
194
|
+
setAnchorRect({ x: rect.left, y: rect.bottom, width: rect.width });
|
|
195
|
+
};
|
|
196
|
+
const targets = [window];
|
|
197
|
+
let node = buttonRef.current;
|
|
198
|
+
while (node && node !== document.body) {
|
|
199
|
+
const style = window.getComputedStyle(node);
|
|
200
|
+
if (style.overflowY === "auto" || style.overflowY === "scroll" || style.overflow === "auto" || style.overflow === "scroll") {
|
|
201
|
+
targets.push(node);
|
|
202
|
+
}
|
|
203
|
+
node = node.parentElement;
|
|
204
|
+
}
|
|
205
|
+
targets.forEach((target) => {
|
|
206
|
+
target.addEventListener("scroll", reposition, { passive: true });
|
|
207
|
+
});
|
|
208
|
+
window.addEventListener("resize", reposition);
|
|
209
|
+
return () => {
|
|
210
|
+
targets.forEach((target) => {
|
|
211
|
+
target.removeEventListener("scroll", reposition);
|
|
212
|
+
});
|
|
213
|
+
window.removeEventListener("resize", reposition);
|
|
214
|
+
};
|
|
215
|
+
}, [isOpen, anchorRect]);
|
|
216
|
+
const currentOption = descriptor.options[selectedIndex === -1 ? 0 : selectedIndex];
|
|
217
|
+
const label = currentOption ? currentOption.label : descriptor.value;
|
|
218
|
+
const listbox = (anchorRect &&
|
|
219
|
+
isOpen &&
|
|
220
|
+
createPortal(createElement("div", {
|
|
221
|
+
ref: listboxRef,
|
|
222
|
+
id: listboxId,
|
|
223
|
+
role: "listbox",
|
|
224
|
+
"aria-label": descriptor.label,
|
|
225
|
+
"data-omnigrid-filter-trigger": "",
|
|
226
|
+
className: "omnigrid-select-listbox",
|
|
227
|
+
onKeyDown: handleListboxKeyDown,
|
|
228
|
+
onClick: (event) => {
|
|
229
|
+
event.stopPropagation();
|
|
230
|
+
},
|
|
231
|
+
style: {
|
|
232
|
+
left: `${anchorRect.x}px`,
|
|
233
|
+
top: `${anchorRect.y}px`,
|
|
234
|
+
minWidth: `${anchorRect.width}px`,
|
|
235
|
+
},
|
|
236
|
+
}, descriptor.options.map((option, index) => {
|
|
237
|
+
const selected = option.value === descriptor.value;
|
|
238
|
+
const focused = focusedIndex === index;
|
|
239
|
+
return createElement("div", {
|
|
240
|
+
key: option.value,
|
|
241
|
+
role: "option",
|
|
242
|
+
"aria-selected": selected,
|
|
243
|
+
tabIndex: focused ? 0 : -1,
|
|
244
|
+
"aria-disabled": option.disabled || undefined,
|
|
245
|
+
className: [
|
|
246
|
+
"omnigrid-select-option",
|
|
247
|
+
focused ? "omnigrid-select-option-focused" : null,
|
|
248
|
+
selected ? "omnigrid-select-option-selected" : null,
|
|
249
|
+
option.disabled ? "omnigrid-select-option-disabled" : null,
|
|
250
|
+
]
|
|
251
|
+
.filter(Boolean)
|
|
252
|
+
.join(" "),
|
|
253
|
+
onClick: () => selectOption(index),
|
|
254
|
+
onKeyDown: (event) => handleListboxKeyDown(event),
|
|
255
|
+
}, option.label);
|
|
256
|
+
})), document.body));
|
|
257
|
+
return createElement("div", {
|
|
258
|
+
className: ["omnigrid-select-wrapper", descriptor.className].filter(Boolean).join(" "),
|
|
259
|
+
}, createElement("button", {
|
|
260
|
+
ref: buttonRef,
|
|
261
|
+
type: "button",
|
|
262
|
+
role: "button",
|
|
263
|
+
"aria-haspopup": "listbox",
|
|
264
|
+
"aria-expanded": isOpen,
|
|
265
|
+
"aria-controls": listboxId,
|
|
266
|
+
"aria-label": descriptor.label,
|
|
267
|
+
className: ["omnigrid-select-button", descriptor.disabled ? "omnigrid-select-button-disabled" : null]
|
|
268
|
+
.filter(Boolean)
|
|
269
|
+
.join(" "),
|
|
270
|
+
disabled: descriptor.disabled,
|
|
271
|
+
onClick: (event) => {
|
|
272
|
+
event.stopPropagation();
|
|
273
|
+
setIsOpen((previous) => !previous);
|
|
274
|
+
},
|
|
275
|
+
onKeyDown: handleButtonKeyDown,
|
|
276
|
+
}, createElement("span", { className: "omnigrid-select-value" }, label), createElement("span", { className: "omnigrid-select-arrow", "aria-hidden": "true" }, "▼")), listbox);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Custom-styled, searchable multi-select listbox with checkboxes for set/category filtering.
|
|
280
|
+
*/
|
|
281
|
+
function MultiSelectControl({ descriptor }) {
|
|
282
|
+
const [searchTerm, setSearchTerm] = useState("");
|
|
283
|
+
const filteredOptions = useMemo(() => {
|
|
284
|
+
if (!searchTerm.trim())
|
|
285
|
+
return descriptor.options;
|
|
286
|
+
const term = searchTerm.toLowerCase();
|
|
287
|
+
return descriptor.options.filter((opt) => opt.label.toLowerCase().includes(term) || opt.value.toLowerCase().includes(term));
|
|
288
|
+
}, [descriptor.options, searchTerm]);
|
|
289
|
+
const allChecked = useMemo(() => descriptor.options.length > 0 && descriptor.options.every((opt) => opt.checked), [descriptor.options]);
|
|
290
|
+
const toggleAll = (checked) => {
|
|
291
|
+
if (checked) {
|
|
292
|
+
descriptor.onChange(descriptor.options.map((opt) => opt.value));
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
descriptor.onChange([]);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
const toggleOption = (val, checked) => {
|
|
299
|
+
const currentlyChecked = new Set(descriptor.options.filter((opt) => opt.checked).map((opt) => opt.value));
|
|
300
|
+
if (checked) {
|
|
301
|
+
currentlyChecked.add(val);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
currentlyChecked.delete(val);
|
|
305
|
+
}
|
|
306
|
+
descriptor.onChange(Array.from(currentlyChecked));
|
|
307
|
+
};
|
|
308
|
+
return createElement("div", { className: ["omnigrid-multi-select-container", descriptor.className].filter(Boolean).join(" ") }, createElement("div", { className: "omnigrid-control-input-wrapper" }, createElement("input", {
|
|
309
|
+
className: "omnigrid-control-input",
|
|
310
|
+
type: "text",
|
|
311
|
+
placeholder: descriptor.searchPlaceholder ?? "Search...",
|
|
312
|
+
value: searchTerm,
|
|
313
|
+
onChange: (e) => setSearchTerm(e.target.value),
|
|
314
|
+
onClick: (e) => e.stopPropagation(),
|
|
315
|
+
}), searchTerm.length > 0 &&
|
|
316
|
+
createElement("button", {
|
|
317
|
+
type: "button",
|
|
318
|
+
className: "omnigrid-control-clear",
|
|
319
|
+
"aria-label": "Clear search",
|
|
320
|
+
onClick: (e) => {
|
|
321
|
+
e.stopPropagation();
|
|
322
|
+
setSearchTerm("");
|
|
323
|
+
},
|
|
324
|
+
}, "×")), createElement("div", { className: "omnigrid-multi-select-actions" }, createElement("label", { className: "omnigrid-multi-select-item" }, createElement("input", {
|
|
325
|
+
type: "checkbox",
|
|
326
|
+
className: "omnigrid-checkbox",
|
|
327
|
+
checked: allChecked,
|
|
328
|
+
onChange: (e) => toggleAll(e.target.checked),
|
|
329
|
+
}), createElement("span", { className: "omnigrid-multi-select-label" }, "(Select all)"))), createElement("div", { className: "omnigrid-multi-select-list" }, filteredOptions.map((option) => createElement("label", { key: option.value, className: "omnigrid-multi-select-item" }, createElement("input", {
|
|
330
|
+
type: "checkbox",
|
|
331
|
+
className: "omnigrid-checkbox",
|
|
332
|
+
checked: option.checked,
|
|
333
|
+
onChange: (e) => toggleOption(option.value, e.target.checked),
|
|
334
|
+
}), createElement("span", { className: "omnigrid-multi-select-label" }, option.label)))));
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Renders one framework-agnostic control descriptor with native HTML elements.
|
|
338
|
+
* Descriptor `id`s become React keys, so re-describing a popup updates
|
|
339
|
+
* elements in place instead of remounting them.
|
|
340
|
+
*/
|
|
341
|
+
export function renderControl(descriptor) {
|
|
342
|
+
switch (descriptor.type) {
|
|
343
|
+
case "text":
|
|
344
|
+
case "number":
|
|
345
|
+
case "date":
|
|
346
|
+
return withLabel(descriptor, createElement(DebouncedInput, { descriptor }));
|
|
347
|
+
case "select":
|
|
348
|
+
return withLabel(descriptor, createElement(SelectControl, { descriptor }));
|
|
349
|
+
case "multi-select":
|
|
350
|
+
return withLabel(descriptor, createElement(MultiSelectControl, { descriptor }));
|
|
351
|
+
case "checkbox":
|
|
352
|
+
return withLabel(descriptor, createElement("input", {
|
|
353
|
+
type: "checkbox",
|
|
354
|
+
className: ["omnigrid-checkbox", descriptor.className].filter(Boolean).join(" "),
|
|
355
|
+
checked: descriptor.checked,
|
|
356
|
+
disabled: descriptor.disabled,
|
|
357
|
+
onChange: (event) => descriptor.onChange(event.target.checked),
|
|
358
|
+
}), true);
|
|
359
|
+
case "button":
|
|
360
|
+
return createElement("button", {
|
|
361
|
+
key: descriptor.id,
|
|
362
|
+
type: "button",
|
|
363
|
+
className: ["omnigrid-control-button", descriptor.variant === "primary" ? "omnigrid-control-button-primary" : null, descriptor.className]
|
|
364
|
+
.filter(Boolean)
|
|
365
|
+
.join(" "),
|
|
366
|
+
disabled: descriptor.disabled,
|
|
367
|
+
onClick: () => descriptor.onClick(),
|
|
368
|
+
}, descriptor.text);
|
|
369
|
+
case "label":
|
|
370
|
+
return createElement("div", { key: descriptor.id, className: ["omnigrid-control-label", descriptor.className].filter(Boolean).join(" ") }, descriptor.text);
|
|
371
|
+
case "group":
|
|
372
|
+
return createElement("div", {
|
|
373
|
+
key: descriptor.id,
|
|
374
|
+
className: ["omnigrid-control", "omnigrid-control-group", descriptor.direction === "row" ? "omnigrid-control-group-row" : null, descriptor.className]
|
|
375
|
+
.filter(Boolean)
|
|
376
|
+
.join(" "),
|
|
377
|
+
}, descriptor.children.map(renderControl));
|
|
378
|
+
default:
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,5 +6,9 @@ export type { PooledCellRenderInfo, PooledColumn, PooledRowCallbacks, PooledRowM
|
|
|
6
6
|
export { OmniGrid } from "./OmniGrid";
|
|
7
7
|
export type { GridProps, OverlayComponentProps, SlotRendererRegistry } from "./OmniGrid";
|
|
8
8
|
export { useGrid } from "./useGrid";
|
|
9
|
+
export { DebouncedInput, isUIControlDescriptor, renderControl } from "./controls";
|
|
10
|
+
export type { DebouncedInputProps } from "./controls";
|
|
11
|
+
export { PopupHost } from "./popup";
|
|
12
|
+
export type { PopupHostProps } from "./popup";
|
|
9
13
|
export type { UseGridResult } from "./useGrid";
|
|
10
14
|
export { type ResolvedColumnPresentation, resolveCellClasses, resolveCellStyle, resolveColumnPresentation, resolveGroupHeaderClasses, resolveGroupHeaderStyle, resolveHeaderClasses, resolveHeaderStyle, } from "./styling";
|
package/dist/index.js
CHANGED
|
@@ -4,4 +4,6 @@ export * from "@omnigrid/core";
|
|
|
4
4
|
export { PooledRowPane } from "./pooledRow";
|
|
5
5
|
export { OmniGrid } from "./OmniGrid";
|
|
6
6
|
export { useGrid } from "./useGrid";
|
|
7
|
+
export { DebouncedInput, isUIControlDescriptor, renderControl } from "./controls";
|
|
8
|
+
export { PopupHost } from "./popup";
|
|
7
9
|
export { resolveCellClasses, resolveCellStyle, resolveColumnPresentation, resolveGroupHeaderClasses, resolveGroupHeaderStyle, resolveHeaderClasses, resolveHeaderStyle, } from "./styling";
|
package/dist/popup.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { GridApi } from "@omnigrid/core";
|
|
3
|
+
export interface PopupHostProps<T> {
|
|
4
|
+
grid: GridApi<T>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Renders the grid's active `UIPopupDescriptor`. Popup state lives here, so
|
|
8
|
+
* opening or updating a popup never re-renders the grid body. The popup is
|
|
9
|
+
* `position: fixed`, which keeps it out of the grid's layout and scroll flow.
|
|
10
|
+
*/
|
|
11
|
+
export declare function PopupHost<T>({ grid }: PopupHostProps<T>): ReactNode;
|
package/dist/popup.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { createElement, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import { renderControl } from "./controls";
|
|
3
|
+
const VIEWPORT_MARGIN = 8;
|
|
4
|
+
function anchorStyle(popup) {
|
|
5
|
+
const base = { position: "absolute", zIndex: 1000 };
|
|
6
|
+
const anchor = popup.anchor;
|
|
7
|
+
if (!anchor)
|
|
8
|
+
return { ...base, left: "50%", top: "50%", transform: "translate(-50%, -50%)" };
|
|
9
|
+
const top = anchor.y + (anchor.height ?? 0);
|
|
10
|
+
if (popup.placement === "bottom-end")
|
|
11
|
+
return { ...base, left: anchor.x + (anchor.width ?? 0), top, transform: "translateX(-100%)" };
|
|
12
|
+
return { ...base, left: anchor.x, top };
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Renders the grid's active `UIPopupDescriptor`. Popup state lives here, so
|
|
16
|
+
* opening or updating a popup never re-renders the grid body. The popup is
|
|
17
|
+
* `position: fixed`, which keeps it out of the grid's layout and scroll flow.
|
|
18
|
+
*/
|
|
19
|
+
export function PopupHost({ grid }) {
|
|
20
|
+
const [popup, setPopup] = useState(() => grid.getPopup());
|
|
21
|
+
const elementRef = useRef(null);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
setPopup(grid.getPopup());
|
|
24
|
+
return grid.on("popupChange", setPopup);
|
|
25
|
+
}, [grid]);
|
|
26
|
+
const closeOnOutside = popup?.closeOnOutsideClick ?? true;
|
|
27
|
+
const closeOnEscape = popup?.closeOnEscape ?? true;
|
|
28
|
+
const isOpen = popup !== null;
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!isOpen)
|
|
31
|
+
return undefined;
|
|
32
|
+
const handlePointerDown = (event) => {
|
|
33
|
+
if (!closeOnOutside)
|
|
34
|
+
return;
|
|
35
|
+
if (event.target instanceof Element && event.target.closest("[data-omnigrid-filter-trigger]"))
|
|
36
|
+
return;
|
|
37
|
+
const element = elementRef.current;
|
|
38
|
+
if (element && event.target instanceof Node && !element.contains(event.target))
|
|
39
|
+
grid.closePopup();
|
|
40
|
+
};
|
|
41
|
+
const handleKeyDown = (event) => {
|
|
42
|
+
if (closeOnEscape && event.key === "Escape")
|
|
43
|
+
grid.closePopup();
|
|
44
|
+
};
|
|
45
|
+
document.addEventListener("pointerdown", handlePointerDown, true);
|
|
46
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
47
|
+
return () => {
|
|
48
|
+
document.removeEventListener("pointerdown", handlePointerDown, true);
|
|
49
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
50
|
+
};
|
|
51
|
+
}, [grid, isOpen, closeOnOutside, closeOnEscape]);
|
|
52
|
+
// Keep an anchored popup inside the window: a single measurement per popup change.
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
const element = elementRef.current;
|
|
55
|
+
if (!element || !popup?.anchor)
|
|
56
|
+
return;
|
|
57
|
+
const rect = element.getBoundingClientRect();
|
|
58
|
+
const parent = element.parentElement;
|
|
59
|
+
const parentRect = parent ? parent.getBoundingClientRect() : { left: 0, top: 0 };
|
|
60
|
+
const maxLeft = Math.max(VIEWPORT_MARGIN, window.innerWidth - VIEWPORT_MARGIN - rect.width);
|
|
61
|
+
const left = Math.min(Math.max(rect.left, VIEWPORT_MARGIN), maxLeft);
|
|
62
|
+
if (left !== rect.left) {
|
|
63
|
+
element.style.transform = "none";
|
|
64
|
+
element.style.left = `${left - parentRect.left}px`;
|
|
65
|
+
}
|
|
66
|
+
}, [popup]);
|
|
67
|
+
if (!popup)
|
|
68
|
+
return null;
|
|
69
|
+
return createElement("div", {
|
|
70
|
+
key: popup.id,
|
|
71
|
+
ref: elementRef,
|
|
72
|
+
role: "dialog",
|
|
73
|
+
"aria-label": popup.title ?? popup.id,
|
|
74
|
+
className: ["omnigrid-popup", popup.className].filter(Boolean).join(" "),
|
|
75
|
+
style: anchorStyle(popup),
|
|
76
|
+
}, popup.title ? createElement("div", { className: "omnigrid-popup-title" }, popup.title) : null, popup.controls.map(renderControl));
|
|
77
|
+
}
|
package/dist/useGrid.js
CHANGED
|
@@ -28,6 +28,22 @@ export function useGrid(options) {
|
|
|
28
28
|
const subscribe = useCallback((listener) => grid.subscribe(listener), [grid]);
|
|
29
29
|
const getSnapshot = useCallback(() => grid.getState(), [grid]);
|
|
30
30
|
const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
31
|
+
// Reconcile plugins when the plugin list changes. Plugins removed from the
|
|
32
|
+
// list are unregistered (their cleanup restores default behavior); new ones
|
|
33
|
+
// are registered. Declared before the data sync so that, on a switch, the
|
|
34
|
+
// old plugin is gone before data is replaced and the new one is attached
|
|
35
|
+
// before the new data arrives. The first run is skipped: the Grid
|
|
36
|
+
// constructor already registered the initial plugins.
|
|
37
|
+
const pluginsSyncedRef = useRef(false);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (grid.isDestroyed())
|
|
40
|
+
return;
|
|
41
|
+
if (!pluginsSyncedRef.current) {
|
|
42
|
+
pluginsSyncedRef.current = true;
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
grid.setPlugins(options.plugins);
|
|
46
|
+
}, [grid, options.plugins]);
|
|
31
47
|
// Sync data when the data reference changes (async load, pagination,
|
|
32
48
|
// parent-side filtering). Skip on the first run — the Grid constructor
|
|
33
49
|
// already initialised data from options.data. Only sync on subsequent
|
|
@@ -72,6 +88,19 @@ export function useGrid(options) {
|
|
|
72
88
|
grid.setColumns(options.columns);
|
|
73
89
|
}
|
|
74
90
|
}, [grid, options.columns]);
|
|
91
|
+
// Controlled filter model: applied when the host passes a new reference.
|
|
92
|
+
// Skipped on the first run — the Grid constructor already applied it.
|
|
93
|
+
const filterSyncedRef = useRef(false);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (grid.isDestroyed())
|
|
96
|
+
return;
|
|
97
|
+
if (!filterSyncedRef.current) {
|
|
98
|
+
filterSyncedRef.current = true;
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (options.filterModel)
|
|
102
|
+
grid.setFilterModel(options.filterModel);
|
|
103
|
+
}, [grid, options.filterModel]);
|
|
75
104
|
// Sync loading state. An explicit boolean prop always wins and is applied
|
|
76
105
|
// on every change (including the first flush — the Grid constructor may
|
|
77
106
|
// have been created with a stale value). `undefined` means the host does
|