Skip to content

Commit

Permalink
feat: virtual dom
Browse files Browse the repository at this point in the history
  • Loading branch information
afshinm committed Jun 21, 2020
1 parent e039faf commit e28ccb4
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/dictionaries/afshin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions docs/examples/react-cells.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
id: react-cells
title: React Component in cells
keywords:
- javascript
- table
- javascript table
- gridjs
- grid js
- react
- react component
---

import { Grid, h, createRef as gCreateRef, Component as gComponent } from "gridjs";
import CodeBlock from "@theme/CodeBlock"
import { useEffect, useRef } from "react";
import ReactDOM from 'react-dom';
import * as faker from 'faker';

Grid.js uses Preact to render the elements and that means that you can take advantage of Preact's Virtual DOM and render
complex cells. In this example, we want to render React components in our cells.

First of all, let's import `Component` and `createRef` from Grid.js:
```js
import {
Grid,
h,
createRef as gCreateRef,
Component as gComponent
} from "gridjs";
```

:::info
`gComponent` and `gCreateRef` are both coming from Grid.js package.
I have renamed them in this example to avoid naming conflicts with React.
:::

Next, we can create a component named `ReactComponent`, that takes one input (our React component) mounts it to our table:

```js
class ReactComponent extends gComponent {
ref = gCreateRef(null);

componentDidMount() {
ReactDOM.render(this.props.element, this.ref.current);
}

render() {
return h('div', { ref: this.ref });
}
}
```

Here is the finalized example:

<CodeBlock children={
`
class ReactComponent extends gComponent {
ref = gCreateRef(null);

componentDidMount() {
ReactDOM.render(this.props.element, this.ref.current)
}

render() {
return h('div', { ref: this.ref });
}
}
const grid = new Grid({
columns: [
'Name',
'Email',
'Actions'
],
data: Array(5).fill().map(x => [
faker.name.findName(),
faker.internet.email(),
h(ReactComponent, { element: <b>Boom!!</b> })
])
});
`
}
transformCode={(code) =>
`
function () {
${code}

const wrapperRef = useRef(null);

useEffect(() => {
grid.render(wrapperRef.current);
});

return (
<div ref={wrapperRef} />
);
}
`
} live={true} scope={{
Grid,
CodeBlock,
useEffect,
useRef,
faker,
h,
gComponent,
gCreateRef,
ReactDOM
}} />
78 changes: 78 additions & 0 deletions docs/examples/virtual-dom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
id: virtual-dom
title: Virtual DOM
keywords:
- javascript
- table
- javascript table
- gridjs
- grid js
- preact
- virtual DOM
- vdom
---

import { Grid, h } from "gridjs";
import CodeBlock from "@theme/CodeBlock"
import { useEffect, useRef } from "react";
import * as faker from 'faker';

Grid.js uses Preact to render the elements and that means that you can take advantage of Preact's Virtual DOM and render
complex cells.

Simply, import `h` from the `gridjs` package:

```js
import { h } from "gridjs";
```

Then, create a custom Preact component:

```js
function bold(text) {
return h('b', {}, text);
}
```

Finally, connect the component to Grid.js:

<CodeBlock children={
`
function bold(text) {
return h('b', {}, text);
}
const grid = new Grid({
columns: [
'Name',
'Email'
],
data: Array(5).fill().map(x => [
faker.name.findName(),
bold(faker.internet.email())
])
});
`
}
transformCode={(code) =>
`
function () {
${code}

const wrapperRef = useRef(null);

useEffect(() => {
grid.render(wrapperRef.current);
});

return (
<div ref={wrapperRef} />
);
}
`
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef, faker, h }} />

<br/>

:::tip
Explore [Preact's documentation](https://preactjs.com/guide/v10/components) for more details.
:::
2 changes: 2 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ module.exports = {
'examples/force-render',
'examples/cell-formatting',
'examples/html-cells',
'examples/virtual-dom',
'examples/react-cells',
'examples/multi-sort',
'examples/custom-sort',
'examples/i18n',
Expand Down

0 comments on commit e28ccb4

Please sign in to comment.