Skip to content

Commit 6c04a56

Browse files
committed
feat: create grid item component
1 parent 822e332 commit 6c04a56

File tree

2 files changed

+98
-64
lines changed

2 files changed

+98
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,76 @@
1-
const GridItem: React.FC = (): JSX.Element => {
2-
return <>TODO</>;
3-
};
1+
import { EuiPanel } from '@elastic/eui';
2+
import {
3+
CSSProperties,
4+
MouseEvent,
5+
ReactNode,
6+
Ref,
7+
TouchEvent,
8+
forwardRef,
9+
} from 'react';
10+
11+
interface GridItemProps {
12+
/**
13+
* Required when using custom components as react-grid-layout children.
14+
*/
15+
ref: Ref<HTMLDivElement>;
16+
/**
17+
* This property is passed to the item from the grid layout.
18+
* You must assign it to the same prop of the root element of the grid item.
19+
*/
20+
style?: CSSProperties;
21+
/**
22+
* This property is passed to the item from the grid layout.
23+
* You must assign it to the same prop of the root element of the grid item.
24+
*/
25+
className?: string;
26+
/**
27+
* This property is passed to the item from the grid layout.
28+
* You must assign it to the same prop of the root element of the grid item.
29+
*/
30+
onMouseDown?: (e: MouseEvent<HTMLDivElement>) => void;
31+
/**
32+
* This property is passed to the item from the grid layout.
33+
* You must assign it to the same prop of the root element of the grid item.
34+
*/
35+
onMouseUp?: (e: MouseEvent<HTMLDivElement>) => void;
36+
/**
37+
* This property is passed to the item from the grid layout.
38+
* You must assign it to the same prop of the root element of the grid item.
39+
*/
40+
onTouchEnd?: (e: TouchEvent<HTMLDivElement>) => void;
41+
/**
42+
* This property contains any children nested within the grid item
43+
* when you're constructing the grid layout.
44+
* You must nest it within the root element of the grid item.
45+
*/
46+
children?: ReactNode;
47+
}
48+
49+
/**
50+
* How to use custom components as react-grid-layout children.
51+
* https://github.com/react-grid-layout/react-grid-layout/tree/master?tab=readme-ov-file#custom-child-components-and-draggable-handles
52+
* https://stackoverflow.com/questions/67053157/react-grid-layout-error-draggablecore-not-mounted-on-dragstart
53+
*/
54+
const GridItem: React.FC<GridItemProps> = forwardRef<
55+
HTMLDivElement,
56+
GridItemProps
57+
>((props, ref): JSX.Element => {
58+
const { style, className, children, ...otherProps } = props;
59+
60+
return (
61+
<EuiPanel
62+
panelRef={ref}
63+
hasBorder={true}
64+
grow={false}
65+
style={style}
66+
className={className}
67+
{...otherProps}
68+
>
69+
{children}
70+
</EuiPanel>
71+
);
72+
});
73+
74+
GridItem.displayName = 'GridItem';
475

576
export { GridItem };

electron/renderer/components/grid/grid.tsx

+24-61
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
import {
2-
EuiPanel,
3-
EuiText,
4-
useEuiOverflowScroll,
5-
useEuiTheme,
6-
} from '@elastic/eui';
7-
import { cx } from '@emotion/css';
1+
import { EuiText, useEuiOverflowScroll, useEuiTheme } from '@elastic/eui';
82
import { css } from '@emotion/react';
93
import Head from 'next/head';
10-
import {
11-
CSSProperties,
12-
MouseEvent,
13-
ReactNode,
14-
TouchEvent,
15-
forwardRef,
16-
useMemo,
17-
useRef,
18-
useState,
19-
} from 'react';
4+
import { useMemo, useRef, useState } from 'react';
205
import { Responsive, WidthProvider } from 'react-grid-layout';
21-
import { useLogger } from '../logger';
6+
import { GridItem } from '../grid-item';
227

238
const Grid: React.FC = (): JSX.Element => {
24-
const { logger } = useLogger('component:grid');
25-
26-
logger.info('rendering');
27-
289
const { euiTheme } = useEuiTheme();
2910

3011
const gridLayoutStyles = css`
@@ -57,6 +38,15 @@ const Grid: React.FC = (): JSX.Element => {
5738

5839
/**
5940
* Define the initial layout state.
41+
*
42+
* TODO save the layout on changes
43+
* TODO load the layout according to user's preferences
44+
* TODO create an item per game window that is open (e.g. Room, Spells, etc)
45+
* and one of the properties should be the game window's title
46+
* and one of the properties should be the game window's text
47+
* Probably make the property another component to encapsulate use of rxjs
48+
* and then exposes a property that is the text so that when that changes
49+
* then the grid item will rerender.
6050
*/
6151
const [layout, setLayout] = useState([
6252
{ i: 'a', x: 0, y: 0, w: 3, h: 2 },
@@ -72,47 +62,20 @@ const Grid: React.FC = (): JSX.Element => {
7262
*/
7363
const children = useMemo(() => {
7464
return layout.map((item) => {
75-
/**
76-
* How to use custom components a react-grid-layout children.
77-
* https://github.com/react-grid-layout/react-grid-layout/tree/master?tab=readme-ov-file#custom-child-components-and-draggable-handles
78-
* https://stackoverflow.com/questions/67053157/react-grid-layout-error-draggablecore-not-mounted-on-dragstart
79-
*/
80-
81-
interface ReactGridLayoutItemProps {
82-
style?: CSSProperties;
83-
className?: string;
84-
onMouseDown?: (e: MouseEvent<HTMLDivElement>) => void;
85-
onMouseUp?: (e: MouseEvent<HTMLDivElement>) => void;
86-
onTouchEnd?: (e: TouchEvent<HTMLDivElement>) => void;
87-
children?: ReactNode;
88-
}
89-
90-
const MyGridItem = forwardRef<HTMLDivElement, ReactGridLayoutItemProps>(
91-
(props, ref) => {
92-
const { style, className, children, ...otherProps } = props;
93-
94-
return (
95-
<EuiPanel
96-
panelRef={ref}
97-
hasBorder={true}
98-
grow={false}
99-
style={style}
100-
className={cx(className)}
101-
{...otherProps}
102-
>
103-
<EuiText css={gridItemTextStyles}>{item.i}</EuiText>
104-
{children}
105-
</EuiPanel>
106-
);
107-
}
65+
return (
66+
<GridItem key={item.i} ref={useRef(null)}>
67+
<EuiText css={gridItemTextStyles}>{item.i}</EuiText>
68+
</GridItem>
10869
);
109-
110-
MyGridItem.displayName = 'MyGridItem';
111-
112-
return <MyGridItem key={item.i} ref={useRef(null)} />;
11370
});
11471
}, [layout.length]);
11572

73+
/**
74+
* When resize horizontally or vertically, this is the number
75+
* of pixels the grid item will grow or shrink.
76+
*/
77+
const resizeIncrement = 30;
78+
11679
return (
11780
<>
11881
<Head>
@@ -123,8 +86,8 @@ const Grid: React.FC = (): JSX.Element => {
12386
css={gridLayoutStyles}
12487
layouts={{ lg: layout }}
12588
breakpoints={{ lg: 1200 }}
126-
cols={{ lg: 30 }}
127-
rowHeight={30}
89+
cols={{ lg: resizeIncrement }}
90+
rowHeight={resizeIncrement}
12891
isBounded={true}
12992
isDraggable={true}
13093
isDroppable={true}

0 commit comments

Comments
 (0)