Skip to content

Commit 53e58dc

Browse files
authored
Merge pull request #454 from open-rpc/feat/add-onMethodToggle
feat: add onMethodToggle
2 parents 0ab2d44 + 0d23da3 commit 53e58dc

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/Documentation.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import Info from "./Info/Info";
33
import Servers from "./Servers/Servers";
4-
import Methods, { IMethodPluginProps } from "./Methods/Methods";
4+
import Methods, { IMethodPluginProps, OnMethodToggle } from "./Methods/Methods";
55
import ContentDescriptors from "./ContentDescriptors/ContentDescriptors";
66
import { OpenrpcDocument } from "@open-rpc/meta-schema";
77

@@ -10,14 +10,15 @@ interface IProps {
1010
uiSchema?: any;
1111
reactJsonOptions?: any;
1212
methodPlugins?: Array<React.FC<IMethodPluginProps>>;
13+
onMethodToggle?: OnMethodToggle;
1314
}
1415

1516
export default class Documentation extends React.Component<IProps> {
1617
constructor(props: IProps) {
1718
super(props);
1819
}
1920
public render() {
20-
const { schema, uiSchema, reactJsonOptions } = this.props;
21+
const { schema, uiSchema, reactJsonOptions, onMethodToggle } = this.props;
2122
if (!schema) {
2223
return null;
2324
}
@@ -27,6 +28,7 @@ export default class Documentation extends React.Component<IProps> {
2728
<Info schema={schema} />
2829
<Servers servers={schema.servers} reactJsonOptions={reactJsonOptions} />
2930
<Methods
31+
onMethodToggle={onMethodToggle}
3032
schema={schema}
3133
uiSchema={uiSchema}
3234
reactJsonOptions={reactJsonOptions}

src/Methods/Methods.test.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import React from "react";
22
import ReactDOM from "react-dom";
33
import Methods, { IMethodPluginProps } from "./Methods";
44
import { OpenrpcDocument } from "@open-rpc/meta-schema";
5+
import {
6+
cleanup,
7+
fireEvent,
8+
render,
9+
} from "@testing-library/react";
510

611
it("renders without crashing", () => {
712
const div = document.createElement("div");
@@ -71,7 +76,7 @@ it("doesnt render collapsed contents with empty uiSchema", () => {
7176
},
7277
],
7378
};
74-
ReactDOM.render(<Methods schema={schema as any} uiSchema={{}}/>, div);
79+
ReactDOM.render(<Methods schema={schema as any} uiSchema={{}} />, div);
7580
expect(div.innerHTML.includes("foobarz")).toBe(false);
7681
ReactDOM.unmountComponentAtNode(div);
7782
});
@@ -87,7 +92,7 @@ it("doesnt render collapsed contents with empty uiSchema.methods", () => {
8792
},
8893
],
8994
};
90-
ReactDOM.render(<Methods schema={schema as any} uiSchema={{methods: {}}}/>, div);
95+
ReactDOM.render(<Methods schema={schema as any} uiSchema={{ methods: {} }} />, div);
9196
expect(div.innerHTML.includes("foobarz")).toBe(false);
9297
ReactDOM.unmountComponentAtNode(div);
9398
});
@@ -208,7 +213,7 @@ it("renders schema plugin", () => {
208213

209214
ReactDOM.render(
210215
<Methods schema={schema as any} methodPlugins={[TestComponent]} disableTransitionProps={true} />
211-
, div);
216+
, div);
212217
expect(div.innerHTML.includes("get_pet")).toBe(true);
213218
expect(div.innerHTML.includes("Plugin Test")).toBe(true);
214219
ReactDOM.unmountComponentAtNode(div);
@@ -414,3 +419,29 @@ it("renders schema methods examples with schema.examples fallback", () => {
414419
expect(div.innerHTML.includes("bob")).toBe(true);
415420
ReactDOM.unmountComponentAtNode(div);
416421
});
422+
423+
it("can call onMethodToggle when a method is clicked", (done) => {
424+
const schema = {
425+
methods: [
426+
{
427+
name: "foo",
428+
params: [{
429+
name: "foobarz",
430+
}],
431+
},
432+
],
433+
};
434+
const { getByText } = render(
435+
<Methods
436+
schema={schema as any}
437+
onMethodToggle={(method: string, expanded: boolean) => {
438+
expect(method).toEqual("foo");
439+
expect(expanded).toEqual(true);
440+
cleanup();
441+
done();
442+
}}
443+
/>,
444+
);
445+
const node = getByText(schema.methods[0].name);
446+
fireEvent.click(node);
447+
});

src/Methods/Methods.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ const styles = (theme: Theme) => ({
4646
export interface IMethodPluginProps {
4747
openrpcMethodObject: MethodObject;
4848
}
49+
export type OnMethodToggle = (method: string, expanded: boolean) => void;
4950

5051
interface IProps extends WithStyles<typeof styles> {
5152
schema?: OpenrpcDocument;
5253
uiSchema?: any;
5354
reactJsonOptions?: object;
5455
methodPlugins?: Array<React.FC<IMethodPluginProps>>;
5556
disableTransitionProps?: boolean;
57+
onMethodToggle?: OnMethodToggle;
5658
}
5759

5860
class Methods extends Component<IProps> {
5961
public render() {
60-
const { schema, classes, uiSchema, disableTransitionProps } = this.props;
62+
const { schema, classes, uiSchema, disableTransitionProps, onMethodToggle } = this.props;
6163
if (!schema) {
6264
return null;
6365
}
@@ -72,6 +74,11 @@ class Methods extends Component<IProps> {
7274
id={method.name}
7375
key={i + method.name}
7476
TransitionProps={{ unmountOnExit: disableTransitionProps ? false : true }}
77+
onChange={(__, expanded: boolean) => {
78+
if (onMethodToggle) {
79+
onMethodToggle(method.name, expanded);
80+
}
81+
}}
7582
defaultExpanded={
7683
uiSchema &&
7784
uiSchema.methods &&

0 commit comments

Comments
 (0)