Skip to content

Commit d888df2

Browse files
authored
Merge pull request #38 from open-rpc/fix/add-tags
fix: add tags
2 parents bb64574 + 2114751 commit d888df2

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

package-lock.json

Lines changed: 5 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@material-ui/core": "^3.9.2",
1515
"@material-ui/icons": "^3.0.2",
1616
"@open-rpc/meta-schema": "^1.1.0",
17+
"hash-color-material": "^1.1.3",
1718
"json-schema": "^0.2.3",
1819
"lodash": "^4.17.11",
1920
"react": "^16.8.4",

src/Methods/Methods.test.tsx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,76 @@ it("renders schema methods result", () => {
124124
expect(div.innerHTML.includes("id")).toBe(true);
125125
ReactDOM.unmountComponentAtNode(div);
126126
});
127+
128+
it("renders schema methods tags", () => {
129+
const div = document.createElement("div");
130+
const schema = {
131+
methods: [
132+
{
133+
name: "foobar",
134+
result: {
135+
schema: {
136+
properties: {
137+
id: {
138+
format: "int64",
139+
type: "integer",
140+
},
141+
name: {
142+
type: "string",
143+
},
144+
tag: {
145+
type: "string",
146+
},
147+
},
148+
required: [
149+
"id",
150+
],
151+
},
152+
},
153+
tags: [
154+
{
155+
name: "tag3",
156+
},
157+
{
158+
name: "tag4",
159+
}
160+
]
161+
},
162+
{
163+
result: {
164+
schema: {
165+
properties: {
166+
id: {
167+
format: "int64",
168+
type: "integer",
169+
},
170+
name: {
171+
type: "string",
172+
},
173+
tag: {
174+
type: "string",
175+
},
176+
},
177+
required: [
178+
"id",
179+
],
180+
},
181+
},
182+
tags: [
183+
{
184+
name: "salad",
185+
},
186+
{
187+
name: "mytag",
188+
},
189+
],
190+
},
191+
],
192+
};
193+
ReactDOM.render(<Methods schema={schema as any}/>, div);
194+
expect(div.innerHTML.includes("tag3")).toBe(true);
195+
expect(div.innerHTML.includes("tag4")).toBe(true);
196+
expect(div.innerHTML.includes("salad")).toBe(true);
197+
expect(div.innerHTML.includes("mytag")).toBe(true);
198+
ReactDOM.unmountComponentAtNode(div);
199+
});

src/Methods/Methods.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from "react";
22
import { withStyles, Theme, WithStyles } from "@material-ui/core/styles";
33
import ExpansionPanel from "@material-ui/core/ExpansionPanel";
44
import ExpansionPanelSummary from "@material-ui/core/ExpansionPanelSummary";
5+
import _ from "lodash";
56
import ExpansionPanelDetails from "@material-ui/core/ExpansionPanelDetails";
67
import Typography from "@material-ui/core/Typography";
78
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
@@ -12,6 +13,8 @@ import ExamplePairings from "../ExamplePairings/ExamplePairings";
1213
import Errors from "../Errors/Errors";
1314
import { types } from "@open-rpc/meta-schema";
1415
import Links from "../Links/Links";
16+
import { Chip } from "@material-ui/core";
17+
import Tags from "../Tags/Tags";
1518

1619
const styles = (theme: Theme) => ({
1720
heading: {
@@ -54,6 +57,12 @@ class Methods extends Component<IProps> {
5457
<Typography key={method.name} className={classes.heading}>{method.name}</Typography>
5558
<Typography key={method.summary} className={classes.secondaryHeading}>{method.summary}</Typography>
5659
</ExpansionPanelSummary>
60+
61+
{method.tags && method.tags.length > 0 &&
62+
<ExpansionPanelDetails key="tags">
63+
<Tags tags={method.tags as any} />
64+
</ExpansionPanelDetails>
65+
}
5766
{method.description &&
5867
<ExpansionPanelDetails key="description">
5968
<ReactMarkdown source={method.description} />

src/Tags/Tags.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import Tags from "./Tags";
4+
5+
it("renders empty with empty tags", () => {
6+
const div = document.createElement("div");
7+
ReactDOM.render(<Tags tags={[]} />, div);
8+
expect(div.innerHTML).toBe("");
9+
ReactDOM.unmountComponentAtNode(div);
10+
});
11+
12+
it("renders schema tags", () => {
13+
const div = document.createElement("div");
14+
const tags = [
15+
{
16+
name: "salad",
17+
},
18+
{
19+
name: "mytag",
20+
}
21+
];
22+
ReactDOM.render(<Tags tags={tags} />, div);
23+
expect(div.innerHTML.includes("salad")).toBe(true);
24+
expect(div.innerHTML.includes("mytag")).toBe(true);
25+
ReactDOM.unmountComponentAtNode(div);
26+
});

src/Tags/Tags.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { Component } from "react";
2+
import { types } from "@open-rpc/meta-schema";
3+
import { Chip } from "@material-ui/core";
4+
const hashColor = require("hash-color-material");
5+
6+
interface IProps {
7+
tags?: types.TagObject[];
8+
}
9+
10+
export default class Tags extends Component<IProps> {
11+
public render() {
12+
const { tags }: { tags?: any} = this.props;
13+
if (!tags || tags.length === 0) {
14+
return null;
15+
}
16+
return (
17+
<>
18+
{
19+
tags.map((tag: any, k: number) => {
20+
return (
21+
<Chip
22+
key={tag.name}
23+
label={tag.name}
24+
style={{ backgroundColor: hashColor.getColorFromString(tag.name, false) }}
25+
/>
26+
);
27+
})
28+
}
29+
</>
30+
);
31+
}
32+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"rules": {
66
"ordered-imports": false,
7+
"no-var-requires": false,
78
"no-console": [true, "log"],
89
"indent": [true, "spaces", 2]
910
}

0 commit comments

Comments
 (0)