Skip to content

Commit 817dd55

Browse files
committed
apply suggestions from code review 2
1 parent d6205ec commit 817dd55

File tree

8 files changed

+45
-72
lines changed

8 files changed

+45
-72
lines changed

content/docs/tutorial/dynamic-enum.mdx

+13-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const url = schema['x-url'];
6868
#### Initializing the React Context
6969

7070
Now that we have access to the API URL, we can use React Context to make this data available across our renderers.
71-
React Context allows you to share data deep in the component tree to access data without needing to pass additional properties through the component hierarchy.
71+
[React Context](https://react.dev/learn/passing-data-deeply-with-context) allows you to share data deep in the component tree to access data without needing to pass additional properties through the component hierarchy.
7272
To set up the React Context for your API service, create it in your application as follows:
7373

7474
```js
@@ -127,7 +127,7 @@ type JsonSchemaWithDependenciesAndEndpoint = JsonSchema & {
127127
dependent: string[];
128128
endpoint: string;
129129
};
130-
export const Country = (
130+
const CountryControl = (
131131
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
132132
) => {
133133
...
@@ -193,6 +193,11 @@ const CountryControl = (
193193
/>
194194
);
195195
};
196+
197+
export default withJsonFormsEnumProps(
198+
withTranslateProps(React.memo(CountryControl)),
199+
false
200+
);
196201
```
197202

198203
Now all that´s left to do is to [create a tester](./custom-renderers#2-create-a-tester) and [register](./custom-renderers#3-register-the-renderer) the new custom renderer in our application.
@@ -214,7 +219,7 @@ type JsonSchemaWithDependenciesAndEndpont = JsonSchema & {
214219
endpoint: string;
215220
};
216221

217-
export const Region = (
222+
const RegionControl = (
218223
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
219224
) => {
220225
const schema = props.schema as JsonSchemaWithDependenciesAndEndpont;
@@ -254,5 +259,10 @@ export const Region = (
254259
/>
255260
);
256261
};
262+
263+
export default withJsonFormsEnumProps(
264+
withTranslateProps(React.memo(RegionControl)),
265+
false
266+
);
257267
```
258268
Again we need to create a [create a tester](./custom-renderers#2-create-a-tester) and [register](./custom-renderers#3-register-the-renderer) the new custom renderer.

src/components/common/api.ts renamed to src/components/common/api.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export class API {
2-
private url: string;
2+
url;
33

4-
constructor(url: string) {
4+
constructor(url) {
55
this.url = url;
66
}
77

8-
async get(endpoint: string): Promise<string[]> {
8+
async get(endpoint){
99
switch (this.url + '/' + endpoint) {
1010
case 'www.api.com/regions/Germany':
1111
return germanStates;
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
import React, { useEffect } from 'react';
22
import { useState } from 'react';
3-
import { ControlProps, JsonSchema, OwnPropsOfEnum } from '@jsonforms/core';
4-
import { TranslateProps } from '@jsonforms/react';
3+
import { withJsonFormsEnumProps, withTranslateProps } from '@jsonforms/react';
54
import { CircularProgress } from '@mui/material';
6-
import { Unwrapped, WithOptionLabel } from '@jsonforms/material-renderers';
5+
import { Unwrapped } from '@jsonforms/material-renderers';
76
import { APIContext } from '../../docs/tutorials/dynamic-enum';
87

98
const { MaterialEnumControl } = Unwrapped;
109

11-
type JsonSchemaWithDependenciesAndEndpoint = JsonSchema & {
12-
dependent: string[];
13-
endpoint: string;
14-
};
1510

16-
export const Country = (
17-
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
11+
const CountryControl = (
12+
props
1813
) => {
1914
const { handleChange } = props;
20-
const [options, setOptions] = useState<string[]>([]);
15+
const [options, setOptions] = useState([]);
2116
const api = React.useContext(APIContext);
22-
const schema = props.schema as JsonSchemaWithDependenciesAndEndpoint;
17+
const schema = props.schema ;
2318

2419
const endponit = schema.endpoint;
25-
const dependent: string[] = schema.dependent ? schema.dependent : [];
20+
const dependent = schema.dependent ? schema.dependent : [];
2621

2722
useEffect(() => {
2823
setOptions([]);
@@ -38,7 +33,7 @@ export const Country = (
3833
return (
3934
<MaterialEnumControl
4035
{...props}
41-
handleChange={(path: string, value: any) => {
36+
handleChange={(path, value) => {
4237
handleChange(path, value);
4338
dependent.forEach((path) => {
4439
handleChange(path, undefined);
@@ -49,4 +44,9 @@ export const Country = (
4944
})}
5045
/>
5146
);
52-
};
47+
};
48+
49+
export default withJsonFormsEnumProps(
50+
withTranslateProps(React.memo(CountryControl)),
51+
false
52+
);

src/components/common/country/CountryControl.tsx

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
import React from 'react';
22
import { useState } from 'react';
3-
import { ControlProps, JsonSchema, OwnPropsOfEnum } from '@jsonforms/core';
4-
import { TranslateProps, useJsonForms } from '@jsonforms/react';
3+
import { useJsonForms, withJsonFormsEnumProps, withTranslateProps } from '@jsonforms/react';
54
import { CircularProgress } from '@mui/material';
6-
import { Unwrapped, WithOptionLabel } from '@jsonforms/material-renderers';
5+
import { Unwrapped } from '@jsonforms/material-renderers';
76
import { APIContext } from '../../docs/tutorials/dynamic-enum';
87
const { MaterialEnumControl } = Unwrapped;
98

10-
type JsonSchemaWithDependenciesAndEndpont = JsonSchema & {
11-
dependent: string[];
12-
endpoint: string;
13-
};
14-
15-
export const Region = (
16-
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
9+
const RegionControl = (
10+
props
1711
) => {
18-
const schema = props.schema as JsonSchemaWithDependenciesAndEndpont;
12+
const schema = props.schema;
1913
const { handleChange } = props;
20-
const [options, setOptions] = useState<string[]>([]);
14+
const [options, setOptions] = useState([]);
2115
const api = React.useContext(APIContext);
2216
const country = useJsonForms().core?.data.country;
23-
const [previousCountry, setPreviousCountry] = useState<String>();
17+
const [previousCountry, setPreviousCountry] = useState();
2418

2519
const endponit = schema.endpoint;
26-
const dependent: string[] = schema.dependent ? schema.dependent : [];
20+
const dependent = schema.dependent ? schema.dependent : [];
2721

2822
if (previousCountry !== country) {
2923
setOptions([]);
@@ -40,7 +34,7 @@ export const Region = (
4034
return (
4135
<MaterialEnumControl
4236
{...props}
43-
handleChange={(path: string, value: any) => {
37+
handleChange={(path, value) => {
4438
handleChange(path, value);
4539
dependent.forEach((path) => {
4640
handleChange(path, undefined);
@@ -51,4 +45,9 @@ export const Region = (
5145
})}
5246
/>
5347
);
54-
};
48+
};
49+
50+
export default withJsonFormsEnumProps(
51+
withTranslateProps(React.memo(RegionControl)),
52+
false
53+
);

src/components/common/region/RegionControl.tsx

-18
This file was deleted.

0 commit comments

Comments
 (0)