Skip to content

Commit

Permalink
fix: add tags
Browse files Browse the repository at this point in the history
fixes #37
  • Loading branch information
shanejonas committed Apr 1, 2019
1 parent bb64574 commit aaf77aa
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@material-ui/core": "^3.9.2",
"@material-ui/icons": "^3.0.2",
"@open-rpc/meta-schema": "^1.1.0",
"hash-color-material": "^1.1.3",
"json-schema": "^0.2.3",
"lodash": "^4.17.11",
"react": "^16.8.4",
Expand Down
59 changes: 59 additions & 0 deletions src/Methods/Methods.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,62 @@ it("renders schema methods result", () => {
expect(div.innerHTML.includes("id")).toBe(true);
ReactDOM.unmountComponentAtNode(div);
});

it("renders schema methods tags", () => {
const div = document.createElement("div");
const schema = {
methods: [
{
name: "foobar",
result: {
schema: {
properties: {
id: {
format: "int64",
type: "integer",
},
name: {
type: "string",
},
tag: {
type: "string",
},
},
required: [
"id",
],
},
},
tags: ["tag3", "tag4"],
},
{
result: {
schema: {
properties: {
id: {
format: "int64",
type: "integer",
},
name: {
type: "string",
},
tag: {
type: "string",
},
},
required: [
"id",
],
},
},
tags: ["salad", "mytag"],
},
],
};
ReactDOM.render(<Methods schema={schema as any}/>, div);
expect(div.innerHTML.includes("tag3")).toBe(true);
expect(div.innerHTML.includes("tag4")).toBe(true);
expect(div.innerHTML.includes("salad")).toBe(true);
expect(div.innerHTML.includes("mytag")).toBe(true);
ReactDOM.unmountComponentAtNode(div);
});
9 changes: 9 additions & 0 deletions src/Methods/Methods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from "react";
import { withStyles, Theme, WithStyles } from "@material-ui/core/styles";
import ExpansionPanel from "@material-ui/core/ExpansionPanel";
import ExpansionPanelSummary from "@material-ui/core/ExpansionPanelSummary";
import _ from "lodash";
import ExpansionPanelDetails from "@material-ui/core/ExpansionPanelDetails";
import Typography from "@material-ui/core/Typography";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
Expand All @@ -12,6 +13,8 @@ import ExamplePairings from "../ExamplePairings/ExamplePairings";
import Errors from "../Errors/Errors";
import { types } from "@open-rpc/meta-schema";
import Links from "../Links/Links";
import { Chip } from "@material-ui/core";
import Tags from "../Tags/Tags";

const styles = (theme: Theme) => ({
heading: {
Expand Down Expand Up @@ -54,6 +57,12 @@ class Methods extends Component<IProps> {
<Typography key={method.name} className={classes.heading}>{method.name}</Typography>
<Typography key={method.summary} className={classes.secondaryHeading}>{method.summary}</Typography>
</ExpansionPanelSummary>

{method.tags && method.tags.length > 0 &&
<ExpansionPanelDetails key="tags">
<Tags tags={method.tags as any} />
</ExpansionPanelDetails>
}
{method.description &&
<ExpansionPanelDetails key="description">
<ReactMarkdown source={method.description} />
Expand Down
19 changes: 19 additions & 0 deletions src/Tags/Tags.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import ReactDOM from "react-dom";
import Tags from "./Tags";

it("renders empty with empty tags", () => {
const div = document.createElement("div");
ReactDOM.render(<Tags tags={[]} />, div);
expect(div.innerHTML).toBe("");
ReactDOM.unmountComponentAtNode(div);
});

it("renders schema tags", () => {
const div = document.createElement("div");
const tags = ["salad", "mytag"];
ReactDOM.render(<Tags tags={tags} />, div);
expect(div.innerHTML.includes("salad")).toBe(true);
expect(div.innerHTML.includes("mytag")).toBe(true);
ReactDOM.unmountComponentAtNode(div);
});
31 changes: 31 additions & 0 deletions src/Tags/Tags.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { Component } from "react";
import { types } from "@open-rpc/meta-schema";
import { Chip } from "@material-ui/core";
const hashColor = require("hash-color-material");

interface IProps {
tags?: string[];
}

export default class Tags extends Component<IProps> {
public render() {
const { tags } = this.props;
if (!tags || tags.length === 0) {
return null;
}
return (
<>
{
tags.map((tag: any, k: number) => {
return (
<Chip
key={tag}
label={tag}
style={{ backgroundColor: hashColor.getColorFromString(tag, false) }} />
);
})
}
</>
);
}
}
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
],
"rules": {
"ordered-imports": false,
"no-var-requires": false,
"no-console": [true, "log"],
"indent": [true, "spaces", 2]
}
Expand Down

0 comments on commit aaf77aa

Please sign in to comment.