Skip to content

Commit 4567540

Browse files
committed
fix: plugin docs
1 parent 2b37f49 commit 4567540

File tree

6 files changed

+84
-96
lines changed

6 files changed

+84
-96
lines changed

docs/plugins/advanced-plugins.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,34 @@ title: Advanced Plugins
44

55
import { LiveExample } from "../../lib/liveExample.js";
66

7-
Now that we know how to write a basic plugin, let's take a look at some complex examples.
7+
Now that we know how to write a basic Grid.js plugin, let's take a look at some complex examples.
88

99
## Using the pipeline
1010

11-
Grid.js has an internal pipeline which is the brain of Grid.js and takes care of processing, filter and refining the data.
12-
You can always get access to the current pipeline by using `this.config.pipeline` in your plugin component (make sure you have extended `BaseComponent`).
11+
Grid.js has an internal pipeline which takes care of processing, filter and refining the raw data. You can get access to the current pipeline by using `config.pipeline` (via the `useConfig` hook) or you can use the `useSelector` hook to subscribe to a specific part of
12+
Grid.js state.
1313

1414
In this example, we have a table of Name (string) and Salary (double) and our custom plugin is in charge of summing salaries
1515
and displaying the total in the footer.
1616

17+
```js
18+
import { useSelector } from "gridjs";
19+
```
20+
1721
<LiveExample children={
1822
`
19-
class TotalSalaryPlugin extends BaseComponent {
20-
constructor(...props) {
21-
super(...props);
22-
23-
this.state = {
24-
total: 0
25-
};
26-
}
23+
function TotalSalaryPlugin() {
24+
const [total, setTotal] = useState(0);
25+
const data = useSelector((state) => state.data);
2726

28-
setTotal() {
29-
this.config.pipeline.process().then(data => {
30-
this.setState({
31-
total: data.toArray().reduce((prev, row) => prev + row[1], 0)
32-
});
33-
});
34-
}
35-
36-
componentDidMount() {
37-
// initial setState
38-
this.setTotal();
39-
this.config.pipeline.on('updated', this.setTotal.bind(this));
40-
}
27+
useEffect(() => {
28+
if (!data) return;
29+
30+
setTotal(data.toArray().reduce((prev, row) => prev + row[1], 0));
31+
}, [data]);
4132

42-
render() {
43-
return h('b', {}, \`Total: $\${this.state.total.toLocaleString()}\`);
44-
}
33+
return h('b', {}, \`Total: $\${total.toLocaleString()}\`);
4534
}
46-
4735
const grid = new Grid({
4836
search: true,
4937
sort: true,
@@ -67,15 +55,18 @@ grid.plugin.add({
6755

6856
## Using the translation
6957

70-
Likewise, you can get access to the Translator object and localize strings in your custom plugin. `_` is a method of `BaseComponent`
71-
and since you've extended `BaseComponent`, you will have access to `this._` throughout your custom plugin:
58+
Likewise, you can get access to the Translator function using the `useTranslator` hook to localize strings in your Grid.js plugin:
59+
60+
```js
61+
import { useTranslator } from "gridjs";
62+
```
7263

7364
<LiveExample children={
7465
`
75-
class HelloPlugin extends BaseComponent {
76-
render() {
77-
return h('b', {}, this._('hello'));
78-
}
66+
function HelloPlugin() {
67+
const _ = useTranslator();
68+
69+
return h('b', {}, _('hello'));
7970
}
8071

8172
const grid = new Grid({
@@ -97,3 +88,12 @@ grid.plugin.add({
9788
});
9889
`
9990
} />
91+
92+
## Hooks
93+
94+
Grid.js provides the following hooks. You can use them to build and customize the behavior of your plugin:
95+
96+
- `useConfig`: Retrieve the current Grid.js config object
97+
- `useSelector`: Subscribe to a specific part of the Grid.js state (e.g. `useSelector(state => state.data)`)
98+
- `useTranslator`: Get the Grid.js Translator function
99+
- `useStore`: Retrieve the Grid.js internal Store object

docs/plugins/basics.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ In this article, we talk about what is a Grid.js plugin and how to develop one.
88
## Introduction
99

1010
Grid.js uses [Preact](https://preactjs.com/) under the hood to render the table and other components like search, pagination, etc.
11-
A Grid.js plugin is a `class` or a `function` that render a Virtual Node. This interface is very similar to a React component.
11+
A Grid.js plugin is a [Preact Functional Component](https://preactjs.com/guide/v10/components/#functional-components) that render a Virtual Node. This interface is very similar to a React component.
1212

1313
Once you have a component that can render an element, then you can add it to the list of Grid.js plugin and Grid.js will
14-
take care of rendering and calling your plugin.
14+
take care of rendering your plugin.
1515

1616
A [Plugin](https://github.com/grid-js/gridjs/blob/master/src/plugin.ts) has following properties:
1717

1818
```ts
19-
interface Plugin {
19+
interface Plugin<T extends FunctionComponent> {
2020
id: string;
2121
position: PluginPosition;
22-
component: VNode<any>;
22+
component: T;
2323
order?: number;
2424
}
2525
```
@@ -53,11 +53,12 @@ grid.plugin.add({
5353
});
5454
```
5555

56-
Note that `position` and `id` are mandatory fields and `component` is the actual plugin class or function that we want to render.
56+
Note that `position` and `id` are mandatory fields and `component` is the actual plugin function that we want to render.
5757
You can render the same plugin multiple times by calling the `plugin.add()` function and passing an unqiue ID.
5858

5959
## Adding a Plugin using React Wrapper
60-
Just use ```plugins``` property to add all plugin that you want.
60+
Just use the ```plugins``` property to add all plugin that you want.
61+
6162
```js
6263
<Grid
6364
...
@@ -71,7 +72,6 @@ Just use ```plugins``` property to add all plugin that you want.
7172
/>
7273
```
7374

74-
7575
## Ordering of plugins
7676

7777
You can pass an optional `order` property to `plugin.add()` to define the ordering of your components:

docs/plugins/writing-plugin.md

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,34 @@ title: Writing a Plugin
44

55
import { LiveExample } from "../../lib/liveExample.js";
66

7-
In this section, we will explore different ways to write a Grid.js plugin.
8-
9-
## Using a class
10-
11-
Grid.js has a `BaseComponent` class which is used everywhere to ensure that all components are receiving the same set of
12-
internal properties and functions. Simply extend this class and add your own functionality to develop a new plugin.
13-
14-
First of all, import both `BaseComponent` and `BaseProps` from `gridjs`:
7+
Grid.js Plugins are Preact Functional Components. Simply create a new functional component to render a plugin:
158

169
```js
17-
import { BaseComponent, BaseProps, h } from "gridjs";
10+
import { h } from "gridjs";
1811
```
1912

20-
Then, create a new class and extend `BaseComponent`:
21-
2213
```js
23-
class MyPlugin extends BaseComponent {
24-
render() {
25-
return h('h1', {}, 'Hello World!');
26-
}
14+
function MyPlugin () {
15+
return h('h1', {}, 'Hello World!');
2716
}
2817
```
2918

30-
<LiveExample children={
31-
`
32-
class MyPlugin extends BaseComponent {
33-
render() {
34-
return h('h1', {}, 'Hello World!');
35-
}
36-
}
37-
38-
const grid = new Grid({
39-
columns: ['Name', 'Email', 'Phone Number'],
40-
data: [
41-
['John', 'john@example.com', '(353) 01 222 3333'],
42-
['Mark', 'mark@gmail.com', '(01) 22 888 4444'],
43-
]
44-
});
45-
46-
grid.plugin.add({
47-
id: 'myplugin',
48-
component: MyPlugin,
49-
position: PluginPosition.Header,
50-
});
51-
`
52-
} />
53-
54-
## Using a function
55-
56-
You can also write a simple function that returns a VNode and renders the component:
19+
:::tip
20+
You don't have to use the `h` function to render elements if your bundler is set up to understand Preact JSX renderer:
5721

5822
```js
59-
import { h } from "gridjs";
23+
function MyPlugin () {
24+
return <h1>Hello World!</h1>;
25+
}
6026
```
6127

28+
See this guide for more details https://preactjs.com/guide/v10/getting-started#setting-up-jsx
29+
:::
30+
6231
<LiveExample children={
6332
`
64-
function MyPlugin() {
65-
return h('h1', {}, 'Hello world!');
33+
function MyPlugin () {
34+
return h('h1', {}, 'Hello World!');
6635
}
6736

6837
const grid = new Grid({
@@ -80,4 +49,3 @@ grid.plugin.add({
8049
});
8150
`
8251
} />
83-

lib/liveExample.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Grid, html, h, createRef as gCreateRef, Component as gComponent, PluginPosition, BaseComponent } from "gridjs";
1+
import { Grid, html, h, createRef as gCreateRef, Component as gComponent, PluginPosition, BaseComponent, useConfig, useTranslator, useSelector, useState, useEffect } from "gridjs";
22
import { esES, frFR } from "gridjs/l10n";
33
import CodeBlock from "@theme/CodeBlock";
4-
import React, {Component, useEffect, useRef} from "react";
4+
import React, {Component, useRef, useEffect as reactUseEffect} from "react";
55
import * as faker from 'faker';
66

77
export class LiveExample extends Component {
@@ -16,7 +16,7 @@ function () {
1616
1717
const wrapperRef = useRef(null);
1818
19-
useEffect(() => {
19+
reactUseEffect(() => {
2020
if (typeof (grid) == 'object' && wrapperRef && wrapperRef.current && wrapperRef.current.childNodes.length === 0) {
2121
grid.render(wrapperRef.current);
2222
}
@@ -26,7 +26,27 @@ function () {
2626
<div ref={wrapperRef} />
2727
);
2828
}
29-
` } live={true} scope={{ Grid, html, h, gCreateRef, gComponent, PluginPosition, BaseComponent, CodeBlock, useEffect, useRef, faker, esES, frFR, ...this.props.scope }} />
29+
` } live={true} scope={{
30+
Grid,
31+
html,
32+
h,
33+
reactUseEffect,
34+
gCreateRef,
35+
gComponent,
36+
PluginPosition,
37+
BaseComponent,
38+
CodeBlock,
39+
useEffect,
40+
useRef,
41+
faker,
42+
esES,
43+
frFR,
44+
useState,
45+
useConfig,
46+
useSelector,
47+
useTranslator,
48+
...this.props.scope
49+
}} />
3050
)
3151
}
3252
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"chartist": "^0.11.4",
1919
"classnames": "^2.3.1",
2020
"faker": "^5.5.3",
21-
"gridjs": "^6.0.5",
21+
"gridjs": "^6.0.6",
2222
"gridjs-react": "^6.0.1",
2323
"postcss-scss": "^3.0.5",
2424
"react": "^17.0.2",

0 commit comments

Comments
 (0)