Skip to content

Commit bcdb897

Browse files
committed
feat: adding ReactWrapper class
1 parent 90ffecb commit bcdb897

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

src/grid.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as React from "react";
22
import { Component, createRef, RefObject } from "react";
3-
import {Grid as Gridjs, UserConfig} from "gridjs";
3+
import { Grid as Gridjs, UserConfig } from "gridjs";
44

5-
class Grid extends Component<UserConfig, {}> {
5+
class Grid extends Component<Partial<UserConfig>, {}> {
66
private wrapper: RefObject<HTMLDivElement> = createRef();
77
// Grid.js instance
88
private readonly instance = null;

src/wrapper.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { h, createRef as gCreateRef, Component as gComponent } from "gridjs";
2+
3+
import ReactDOM from "react-dom";
4+
5+
export class ReactWrapper extends gComponent<{
6+
element: any;
7+
parent?: string;
8+
}> {
9+
static defaultProps = {
10+
parent: "div",
11+
};
12+
13+
ref = gCreateRef();
14+
15+
componentDidMount() {
16+
ReactDOM.render(this.props.element, this.ref.current);
17+
}
18+
19+
render() {
20+
return h(this.props.parent, { ref: this.ref });
21+
}
22+
}
23+
24+
export function _(element: any, parent?: string) {
25+
return h(ReactWrapper, {
26+
element: element,
27+
parent: parent,
28+
});
29+
}

tests/__snapshots__/grid.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ exports[`Grid component should render a table with pagination and search 1`] = `
66
77
exports[`Grid component should render a table with search 1`] = `"<div><div role=\\"complementary\\" class=\\"gridjs gridjs-container\\" style=\\"width: 100%;\\"><div class=\\"gridjs-head\\"><div class=\\"gridjs-search\\"><input type=\\"search\\" placeholder=\\"Type a keyword...\\" aria-label=\\"Type a keyword...\\" class=\\"gridjs-input gridjs-search-input\\"></div></div><div class=\\"gridjs-wrapper\\" style=\\"width: 100%; height: auto;\\"><table role=\\"grid\\" class=\\"gridjs-table\\" style=\\"width: 100%; height: auto;\\"><thead class=\\"gridjs-thead\\"><tr class=\\"gridjs-tr\\"><th class=\\"gridjs-th\\">a</th><th class=\\"gridjs-th\\">b</th><th class=\\"gridjs-th\\">c</th></tr></thead><tbody class=\\"gridjs-tbody\\"><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">1</td><td class=\\"gridjs-td\\">2</td><td class=\\"gridjs-td\\">3</td></tr></tbody></table></div></div><div id=\\"gridjs-temp\\" class=\\"gridjs-temp\\"></div></div>"`;
88
9+
exports[`Grid component should render a table with the ReactWrapper class 1`] = `"<div><div role=\\"complementary\\" class=\\"gridjs gridjs-container\\" style=\\"width: 100%;\\"><div class=\\"gridjs-wrapper\\" style=\\"width: 100%; height: auto;\\"><table role=\\"grid\\" class=\\"gridjs-table\\" style=\\"width: 100%; height: auto;\\"><thead class=\\"gridjs-thead\\"><tr class=\\"gridjs-tr\\"><th class=\\"gridjs-th\\">a</th><th class=\\"gridjs-th\\">b</th><th class=\\"gridjs-th\\">c</th></tr></thead><tbody class=\\"gridjs-tbody\\"><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">1</td><td class=\\"gridjs-td\\">2</td><td class=\\"gridjs-td\\">3</td></tr><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">4</td><td class=\\"gridjs-td\\">5</td><td class=\\"gridjs-td\\">6</td></tr><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\"><div><b>7</b></div></td><td class=\\"gridjs-td\\"><div><b>8</b></div></td><td class=\\"gridjs-td\\"><span><b>8</b></span></td></tr></tbody></table></div></div><div id=\\"gridjs-temp\\" class=\\"gridjs-temp\\"></div></div>"`;
10+
11+
exports[`Grid component should render a table with the wrapper function 1`] = `"<div><div role=\\"complementary\\" class=\\"gridjs gridjs-container\\" style=\\"width: 100%;\\"><div class=\\"gridjs-wrapper\\" style=\\"width: 100%; height: auto;\\"><table role=\\"grid\\" class=\\"gridjs-table\\" style=\\"width: 100%; height: auto;\\"><thead class=\\"gridjs-thead\\"><tr class=\\"gridjs-tr\\"><th class=\\"gridjs-th\\">a</th><th class=\\"gridjs-th\\">b</th><th class=\\"gridjs-th\\">c</th></tr></thead><tbody class=\\"gridjs-tbody\\"><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">1</td><td class=\\"gridjs-td\\">2</td><td class=\\"gridjs-td\\">3</td></tr><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">4</td><td class=\\"gridjs-td\\">5</td><td class=\\"gridjs-td\\">6</td></tr><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\"><div><b>7</b></div></td><td class=\\"gridjs-td\\"><div><b>8</b></div></td><td class=\\"gridjs-td\\"><span><span>9</span></span></td></tr></tbody></table></div></div><div id=\\"gridjs-temp\\" class=\\"gridjs-temp\\"></div></div>"`;
12+
913
exports[`Grid component should render a table with width 1`] = `"<div><div role=\\"complementary\\" class=\\"gridjs gridjs-container\\" style=\\"width: 500px;\\"><div class=\\"gridjs-wrapper\\" style=\\"width: 500px; height: auto;\\"><table role=\\"grid\\" class=\\"gridjs-table\\" style=\\"width: 500px; height: auto;\\"><thead class=\\"gridjs-thead\\"><tr class=\\"gridjs-tr\\"><th class=\\"gridjs-th\\">a</th><th class=\\"gridjs-th\\">b</th><th class=\\"gridjs-th\\">c</th></tr></thead><tbody class=\\"gridjs-tbody\\"><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">1</td><td class=\\"gridjs-td\\">2</td><td class=\\"gridjs-td\\">3</td></tr></tbody></table></div></div><div id=\\"gridjs-temp\\" class=\\"gridjs-temp\\"></div></div>"`;
1014
1115
exports[`Grid component should render a table without header 1`] = `"<div><div role=\\"complementary\\" class=\\"gridjs gridjs-container\\" style=\\"width: 100%;\\"><div class=\\"gridjs-wrapper\\" style=\\"width: 100%; height: auto;\\"><table role=\\"grid\\" class=\\"gridjs-table\\" style=\\"width: 100%; height: auto;\\"><tbody class=\\"gridjs-tbody\\"><tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">1</td><td class=\\"gridjs-td\\">2</td><td class=\\"gridjs-td\\">3</td></tr></tbody></table></div></div><div id=\\"gridjs-temp\\" class=\\"gridjs-temp\\"></div></div>"`;

tests/grid.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from "react";
22
import { mount } from "enzyme";
33
import { Grid } from "../index";
4+
import { _, ReactWrapper } from "../src/wrapper";
5+
import { h } from "gridjs";
46

57
const flushPromises = () => new Promise(setImmediate);
68

@@ -63,4 +65,49 @@ describe("Grid component", () => {
6365
table.update();
6466
expect(table.html()).toMatchSnapshot();
6567
});
68+
69+
it("should render a table with the ReactWrapper class", async () => {
70+
const table = mount<Grid>(
71+
<Grid
72+
data={[
73+
[1, 2, 3],
74+
[4, 5, 6],
75+
[
76+
h(ReactWrapper, {
77+
element: <b>7</b>,
78+
}),
79+
h(ReactWrapper, {
80+
element: <b>8</b>,
81+
}),
82+
h(ReactWrapper, {
83+
element: <b>8</b>,
84+
parent: "span",
85+
}),
86+
],
87+
]}
88+
columns={["a", "b", "c"]}
89+
/>
90+
);
91+
92+
await flushPromises();
93+
table.update();
94+
expect(table.html()).toMatchSnapshot();
95+
});
96+
97+
it("should render a table with the wrapper function", async () => {
98+
const table = mount<Grid>(
99+
<Grid
100+
data={[
101+
[1, 2, 3],
102+
[4, 5, 6],
103+
[_(<b>7</b>), _(<b>8</b>), _(<span>9</span>, "span")],
104+
]}
105+
columns={["a", "b", "c"]}
106+
/>
107+
);
108+
109+
await flushPromises();
110+
table.update();
111+
expect(table.html()).toMatchSnapshot();
112+
});
66113
});

0 commit comments

Comments
 (0)