Skip to content

Commit 47217ad

Browse files
committed
feat: adding plugin
1 parent cf4aed0 commit 47217ad

File tree

5 files changed

+273
-2
lines changed

5 files changed

+273
-2
lines changed

docs/plugin/advanced-plugins.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
id: advanced-plugins
3+
title: Advanced Plugin
4+
---
5+
6+
import { LiveExample } from "../../lib/liveExample.js";
7+
8+
Now that we know how to write a basic plugin, let's take a look at some complex examples.
9+
10+
## Using the pipeline
11+
12+
Grid.js has an internal pipeline which is the brain of Grid.js and takes care of processing, filter and refining the data.
13+
You can always get access to the current pipeline by using `this.config.pipeline` in your plugin component (make sure you have extended `BaseComponent`).
14+
15+
In this example, we have a table of Name (string) and Salary (double) and our custom plugin is in charge of summing salaries
16+
and displaying the total in the footer.
17+
18+
<LiveExample children={
19+
`
20+
class TotalSalaryPlugin extends BaseComponent {
21+
constructor(...props) {
22+
super(...props);
23+
24+
this.state = {
25+
total: 0
26+
};
27+
}
28+
29+
setTotal() {
30+
this.config.pipeline.process().then(data => {
31+
this.setState({
32+
total: data.toArray().reduce((prev, row) => prev + row[1], 0)
33+
});
34+
});
35+
}
36+
37+
componentDidMount() {
38+
// initial setState
39+
this.setTotal();
40+
this.config.pipeline.on('updated', this.setTotal.bind(this));
41+
}
42+
43+
render() {
44+
return h('b', {}, \`Total: $\${this.state.total.toLocaleString()}\`);
45+
}
46+
}
47+
48+
const grid = new Grid({
49+
search: true,
50+
sort: true,
51+
columns: ['Name', 'Salary'],
52+
data: [
53+
['John', Math.round(Math.random() * 100000)],
54+
['Mark', Math.round(Math.random() * 100000)],
55+
['Josh', Math.round(Math.random() * 100000)],
56+
['Sara', Math.round(Math.random() * 100000)],
57+
['Maria', Math.round(Math.random() * 100000)],
58+
]
59+
});
60+
61+
grid.plugin.add({
62+
id: 'salaryplugin',
63+
component: h(TotalSalaryPlugin, {}),
64+
position: PluginPosition.Footer,
65+
});
66+
`
67+
} />
68+
69+
## Using the translation
70+
71+
Likewise, you can get access to the Translator object and localize strings in your custom plugin:
72+
73+
<LiveExample children={
74+
`
75+
class HelloPlugin extends BaseComponent {
76+
render() {
77+
return h('b', {}, this._('hello'));
78+
}
79+
}
80+
81+
const grid = new Grid({
82+
columns: ['Name', 'Salary'],
83+
data: [
84+
['John', Math.round(Math.random() * 100000)],
85+
['Mark', Math.round(Math.random() * 100000)],
86+
['Josh', Math.round(Math.random() * 100000)],
87+
],
88+
language: {
89+
'hello': 'bonjour!!'
90+
}
91+
});
92+
93+
grid.plugin.add({
94+
id: 'bonjour',
95+
component: h(HelloPlugin, {}),
96+
position: PluginPosition.Header,
97+
});
98+
`
99+
} />

docs/plugin/basics.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: basics
3+
title: Plugin basics
4+
---
5+
6+
Grid.js is a modular JavaScript framework which allows you to add custom plugins to it or remove the existing ones.
7+
In this article, we talk about what is a Grid.js plugin and how to develop one.
8+
9+
## Introduction
10+
11+
Grid.js uses [Preact](https://preactjs.com/) under the hood to render the table and other components like search, pagination, etc.
12+
A Grid.js plugin is a `class` or a `function` that render a Virtual Node. This interface is very similar to a React component.
13+
14+
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
15+
take care of rendering and calling your plugin.
16+
17+
A [Plugin](https://github.com/grid-js/gridjs/blob/master/src/plugin.ts) has following properties:
18+
19+
```ts
20+
interface Plugin {
21+
id: string;
22+
position: PluginPosition;
23+
component: VNode<any>;
24+
order?: number;
25+
}
26+
```
27+
28+
Where `id` is a unique string, `position` defines where that plugin should be rendered and there is an optional `order`
29+
property to change the ordering of plugins in a specific plugin position (e.g. header).
30+
31+
## Plugin Position
32+
33+
Grid.js can render a plugin in different positions. Simply import `PluginPosition` enum from `gridjs` package and use it
34+
when you are calling the `plugin.add(...)` function:
35+
36+
```js
37+
import { PluginPosition } from "gridjs";
38+
```
39+
40+
:::tip
41+
If you need to render your plugin in a position that doesn't already exist, please open a GitHub issue and we will add
42+
a new PluginPosition!
43+
:::
44+
45+
## Adding a Plugin
46+
47+
Adding a plugin to a Grid.js instance is as easy as calling `gridjs_instance.plugin.add(...)`, for instance:
48+
49+
```js
50+
grid.plugin.add({
51+
id: 'myplugin',
52+
component: h(MyPlugin, {}),
53+
position: PluginPosition.Header,
54+
});
55+
```
56+
57+
Note that `position` and `id` are mandatory fields and `component` is the actual plugin class or function that we want to render.
58+
You can render the same plugin multiple times by calling the `plugin.add()` function and passing an unqiue ID.
59+
60+
## Ordering of plugins
61+
62+
You can pass an optional `order` property to `plugin.add()` to define the ordering of your components:
63+
64+
```js
65+
grid.plugin.add({
66+
id: 'myfirstplugin',
67+
component: h(MyPlugin, {}),
68+
position: PluginPosition.Header,
69+
order: 1,
70+
});
71+
72+
grid.plugin.add({
73+
id: 'mysecondplugin',
74+
component: h(MyPlugin, {}),
75+
position: PluginPosition.Header,
76+
order: 2,
77+
});
78+
```
79+
80+
In above example, `myfirstplugin` renders first and then `mysecondplugin` will be rendered.
81+
82+
83+
Now, let's take a look at writing a simple plugin.

docs/plugin/writing-plugin.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
id: writing-plugin
3+
title: Writing a Plugin
4+
---
5+
6+
import { LiveExample } from "../../lib/liveExample.js";
7+
8+
In this section, we will explore different ways to write a Grid.js plugin.
9+
10+
## Using a class
11+
12+
Grid.js has a `BaseComponent` class which is used everywhere to ensure that all components are receiving the same set of
13+
internal properties and functions. Simply extend this class and add your own functionality to develop a new plugin.
14+
15+
First of all, import both `BaseComponent` and `BaseProps` from `gridjs`:
16+
17+
```js
18+
import { BaseComponent, BaseProps, h } from "gridjs";
19+
```
20+
21+
Then, create a new class and extend `BaseComponent`:
22+
23+
```js
24+
class MyPlugin extends BaseComponent {
25+
render() {
26+
return h('h1', {}, 'Hello World!');
27+
}
28+
}
29+
```
30+
31+
<LiveExample children={
32+
`
33+
class MyPlugin extends BaseComponent {
34+
render() {
35+
return h('h1', {}, 'Hello World!');
36+
}
37+
}
38+
39+
const grid = new Grid({
40+
columns: ['Name', 'Email', 'Phone Number'],
41+
data: [
42+
['John', '[email protected]', '(353) 01 222 3333'],
43+
['Mark', '[email protected]', '(01) 22 888 4444'],
44+
]
45+
});
46+
47+
grid.plugin.add({
48+
id: 'myplugin',
49+
component: h(MyPlugin, {}),
50+
position: PluginPosition.Header,
51+
});
52+
`
53+
} />
54+
55+
## Using a function
56+
57+
You can also write a simple function that returns a VNode and renders the component:
58+
59+
```js
60+
import { h } from "gridjs";
61+
```
62+
63+
<LiveExample children={
64+
`
65+
function MyPlugin() {
66+
return h('h1', {}, 'Hello world!');
67+
}
68+
69+
const grid = new Grid({
70+
columns: ['Name', 'Email', 'Phone Number'],
71+
data: [
72+
['John', '[email protected]', '(353) 01 222 3333'],
73+
['Mark', '[email protected]', '(01) 22 888 4444'],
74+
]
75+
});
76+
77+
grid.plugin.add({
78+
id: 'myplugin',
79+
component: h(MyPlugin, {}),
80+
position: PluginPosition.Header,
81+
});
82+
`
83+
} />
84+

lib/liveExample.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Grid, html, h, createRef as gCreateRef, Component as gComponent } from "gridjs";
1+
import { Grid, html, h, createRef as gCreateRef, Component as gComponent, PluginPosition, BaseComponent, BaseProps } from "gridjs";
22
import CodeBlock from "@theme/CodeBlock";
33
import React, {Component, useEffect, useRef} from "react";
44
import * as faker from 'faker';
@@ -25,7 +25,7 @@ function () {
2525
<div ref={wrapperRef} />
2626
);
2727
}
28-
` } live={true} scope={{ Grid, html, h, gCreateRef, gComponent, CodeBlock, useEffect, useRef, faker, ...this.props.scope }} />
28+
` } live={true} scope={{ Grid, html, h, gCreateRef, gComponent, PluginPosition, BaseComponent, BaseProps, CodeBlock, useEffect, useRef, faker, ...this.props.scope }} />
2929
)
3030
}
3131
}

sidebars.js

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ module.exports = {
2323
'config/sort',
2424
'config/pagination',
2525
],
26+
"🧩 Plugin": [
27+
'plugin/basics',
28+
'plugin/writing-plugin',
29+
'plugin/advanced-plugins',
30+
],
2631
"🔌 Integrations": [
2732
'integrations/react',
2833
'integrations/vue',

0 commit comments

Comments
 (0)