Skip to content

Commit e28ccb4

Browse files
committed
feat: virtual dom
1 parent e039faf commit e28ccb4

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

.idea/dictionaries/afshin.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples/react-cells.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
id: react-cells
3+
title: React Component in cells
4+
keywords:
5+
- javascript
6+
- table
7+
- javascript table
8+
- gridjs
9+
- grid js
10+
- react
11+
- react component
12+
---
13+
14+
import { Grid, h, createRef as gCreateRef, Component as gComponent } from "gridjs";
15+
import CodeBlock from "@theme/CodeBlock"
16+
import { useEffect, useRef } from "react";
17+
import ReactDOM from 'react-dom';
18+
import * as faker from 'faker';
19+
20+
Grid.js uses Preact to render the elements and that means that you can take advantage of Preact's Virtual DOM and render
21+
complex cells. In this example, we want to render React components in our cells.
22+
23+
First of all, let's import `Component` and `createRef` from Grid.js:
24+
```js
25+
import {
26+
Grid,
27+
h,
28+
createRef as gCreateRef,
29+
Component as gComponent
30+
} from "gridjs";
31+
```
32+
33+
:::info
34+
`gComponent` and `gCreateRef` are both coming from Grid.js package.
35+
I have renamed them in this example to avoid naming conflicts with React.
36+
:::
37+
38+
Next, we can create a component named `ReactComponent`, that takes one input (our React component) mounts it to our table:
39+
40+
```js
41+
class ReactComponent extends gComponent {
42+
ref = gCreateRef(null);
43+
44+
componentDidMount() {
45+
ReactDOM.render(this.props.element, this.ref.current);
46+
}
47+
48+
render() {
49+
return h('div', { ref: this.ref });
50+
}
51+
}
52+
```
53+
54+
Here is the finalized example:
55+
56+
<CodeBlock children={
57+
`
58+
class ReactComponent extends gComponent {
59+
ref = gCreateRef(null);
60+
61+
componentDidMount() {
62+
ReactDOM.render(this.props.element, this.ref.current)
63+
}
64+
65+
render() {
66+
return h('div', { ref: this.ref });
67+
}
68+
}
69+
const grid = new Grid({
70+
columns: [
71+
'Name',
72+
'Email',
73+
'Actions'
74+
],
75+
data: Array(5).fill().map(x => [
76+
faker.name.findName(),
77+
faker.internet.email(),
78+
h(ReactComponent, { element: <b>Boom!!</b> })
79+
])
80+
});
81+
`
82+
}
83+
transformCode={(code) =>
84+
`
85+
function () {
86+
${code}
87+
88+
const wrapperRef = useRef(null);
89+
90+
useEffect(() => {
91+
grid.render(wrapperRef.current);
92+
});
93+
94+
return (
95+
<div ref={wrapperRef} />
96+
);
97+
}
98+
`
99+
} live={true} scope={{
100+
Grid,
101+
CodeBlock,
102+
useEffect,
103+
useRef,
104+
faker,
105+
h,
106+
gComponent,
107+
gCreateRef,
108+
ReactDOM
109+
}} />

docs/examples/virtual-dom.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
id: virtual-dom
3+
title: Virtual DOM
4+
keywords:
5+
- javascript
6+
- table
7+
- javascript table
8+
- gridjs
9+
- grid js
10+
- preact
11+
- virtual DOM
12+
- vdom
13+
---
14+
15+
import { Grid, h } from "gridjs";
16+
import CodeBlock from "@theme/CodeBlock"
17+
import { useEffect, useRef } from "react";
18+
import * as faker from 'faker';
19+
20+
Grid.js uses Preact to render the elements and that means that you can take advantage of Preact's Virtual DOM and render
21+
complex cells.
22+
23+
Simply, import `h` from the `gridjs` package:
24+
25+
```js
26+
import { h } from "gridjs";
27+
```
28+
29+
Then, create a custom Preact component:
30+
31+
```js
32+
function bold(text) {
33+
return h('b', {}, text);
34+
}
35+
```
36+
37+
Finally, connect the component to Grid.js:
38+
39+
<CodeBlock children={
40+
`
41+
function bold(text) {
42+
return h('b', {}, text);
43+
}
44+
const grid = new Grid({
45+
columns: [
46+
'Name',
47+
'Email'
48+
],
49+
data: Array(5).fill().map(x => [
50+
faker.name.findName(),
51+
bold(faker.internet.email())
52+
])
53+
});
54+
`
55+
}
56+
transformCode={(code) =>
57+
`
58+
function () {
59+
${code}
60+
61+
const wrapperRef = useRef(null);
62+
63+
useEffect(() => {
64+
grid.render(wrapperRef.current);
65+
});
66+
67+
return (
68+
<div ref={wrapperRef} />
69+
);
70+
}
71+
`
72+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef, faker, h }} />
73+
74+
<br/>
75+
76+
:::tip
77+
Explore [Preact's documentation](https://preactjs.com/guide/v10/components) for more details.
78+
:::

sidebars.js

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ module.exports = {
6161
'examples/force-render',
6262
'examples/cell-formatting',
6363
'examples/html-cells',
64+
'examples/virtual-dom',
65+
'examples/react-cells',
6466
'examples/multi-sort',
6567
'examples/custom-sort',
6668
'examples/i18n',

0 commit comments

Comments
 (0)