|
| 1 | +import React from "react"; |
| 2 | +import { DataEditorAll as DataEditor } from "../../data-editor-all.js"; |
| 3 | +import type { GridColumn, Item, TextCell } from "../../internal/data-grid/data-grid-types.js"; |
| 4 | +import { GridCellKind } from "../../internal/data-grid/data-grid-types.js"; |
| 5 | +import { BeautifulWrapper, Description, defaultProps } from "../../data-editor/stories/utils.js"; |
| 6 | +import { SimpleThemeWrapper } from "../../stories/story-utils.js"; |
| 7 | + |
| 8 | +export default { |
| 9 | + title: "Glide-Data-Grid/DataEditor Demos", |
| 10 | + |
| 11 | + decorators: [ |
| 12 | + (Story: React.ComponentType) => ( |
| 13 | + <SimpleThemeWrapper> |
| 14 | + <BeautifulWrapper |
| 15 | + title="Custom Event Target" |
| 16 | + description={ |
| 17 | + <Description> |
| 18 | + This example demonstrates using a custom event target for the data grid. All window events |
| 19 | + are blocked, but the grid still works because it's using the container div as its event |
| 20 | + target instead of window. |
| 21 | + </Description> |
| 22 | + }> |
| 23 | + <Story /> |
| 24 | + </BeautifulWrapper> |
| 25 | + </SimpleThemeWrapper> |
| 26 | + ), |
| 27 | + ], |
| 28 | +}; |
| 29 | + |
| 30 | +export const CustomEventTarget: React.VFC = () => { |
| 31 | + // Create columns |
| 32 | + const [cols] = React.useState<GridColumn[]>(() => { |
| 33 | + return [ |
| 34 | + { |
| 35 | + title: "Column A", |
| 36 | + id: "a", |
| 37 | + width: 150, |
| 38 | + }, |
| 39 | + { |
| 40 | + title: "Column B", |
| 41 | + id: "b", |
| 42 | + width: 150, |
| 43 | + }, |
| 44 | + { |
| 45 | + title: "Column C", |
| 46 | + id: "c", |
| 47 | + width: 150, |
| 48 | + }, |
| 49 | + ]; |
| 50 | + }); |
| 51 | + |
| 52 | + // Create data |
| 53 | + const getCellContent = React.useCallback((cell: Item): TextCell => { |
| 54 | + const [col, row] = cell; |
| 55 | + return { |
| 56 | + kind: GridCellKind.Text, |
| 57 | + allowOverlay: true, |
| 58 | + displayData: `${col}, ${row}`, |
| 59 | + data: `${col}, ${row}`, |
| 60 | + }; |
| 61 | + }, []); |
| 62 | + |
| 63 | + // Create a ref for our custom event target container |
| 64 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 65 | + |
| 66 | + // State to track if the container is mounted |
| 67 | + const [containerMounted, setContainerMounted] = React.useState(false); |
| 68 | + |
| 69 | + // State to track window click attempts |
| 70 | + const [windowClickAttempts, setWindowClickAttempts] = React.useState(0); |
| 71 | + |
| 72 | + // Update containerMounted state after the component mounts |
| 73 | + React.useEffect(() => { |
| 74 | + if (containerRef.current !== null) { |
| 75 | + setContainerMounted(true); |
| 76 | + } |
| 77 | + }, []); |
| 78 | + |
| 79 | + // Block all window events |
| 80 | + React.useEffect(() => { |
| 81 | + const blockEvent = (e: Event) => { |
| 82 | + // Don't block events if they're inside our container |
| 83 | + if (containerRef.current && e.target instanceof Node && containerRef.current.contains(e.target)) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + e.stopPropagation(); |
| 88 | + e.stopImmediatePropagation(); |
| 89 | + if (e.cancelable) { |
| 90 | + e.preventDefault(); |
| 91 | + } |
| 92 | + |
| 93 | + // Count click attempts outside the grid |
| 94 | + if (e.type === "click") { |
| 95 | + setWindowClickAttempts(prev => prev + 1); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + // Block all mouse and touch events on window |
| 100 | + const events = ["mousedown", "mouseup", "mousemove", "click", "touchstart", "touchend", "touchmove"]; |
| 101 | + |
| 102 | + // Add event blockers to window |
| 103 | + for (const event of events) { |
| 104 | + window.addEventListener(event, blockEvent, true); |
| 105 | + } |
| 106 | + |
| 107 | + return () => { |
| 108 | + // Clean up event blockers |
| 109 | + for (const event of events) { |
| 110 | + window.removeEventListener(event, blockEvent, true); |
| 111 | + } |
| 112 | + }; |
| 113 | + }, []); |
| 114 | + |
| 115 | + return ( |
| 116 | + <div style={{ display: "flex", flexDirection: "column", height: "100%" }}> |
| 117 | + <div style={{ marginBottom: 10, padding: 10, backgroundColor: "#f0f0f0", borderRadius: 4 }}> |
| 118 | + <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}> |
| 119 | + <span style={{ color: "#666" }}>Window click attempts blocked: {windowClickAttempts}</span> |
| 120 | + <button |
| 121 | + onClick={() => alert("This button should not work if window events are blocked!")} |
| 122 | + style={{ padding: "5px 10px" }}> |
| 123 | + Try clicking me (should not work) |
| 124 | + </button> |
| 125 | + </div> |
| 126 | + <div style={{ marginTop: 10, fontSize: 14, color: "#666" }}> |
| 127 | + Try clicking outside the grid or on the button above - these clicks should be blocked. But the grid |
| 128 | + below should still be fully interactive! |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + |
| 132 | + <div |
| 133 | + ref={containerRef} |
| 134 | + style={{ |
| 135 | + flex: 1, |
| 136 | + position: "relative", |
| 137 | + border: "2px solid #3c78d8", |
| 138 | + borderRadius: 4, |
| 139 | + padding: 15, |
| 140 | + }}> |
| 141 | + {containerMounted && ( |
| 142 | + <DataEditor |
| 143 | + {...defaultProps} |
| 144 | + width="100%" |
| 145 | + height="100%" |
| 146 | + rows={1000} |
| 147 | + columns={cols} |
| 148 | + getCellContent={getCellContent} |
| 149 | + experimental={{ |
| 150 | + eventTarget: containerRef.current as HTMLElement, |
| 151 | + }} |
| 152 | + /> |
| 153 | + )} |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + ); |
| 157 | +}; |
0 commit comments