Skip to content

Commit

Permalink
apply suggestions from code review 2
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBoll committed Apr 15, 2024
1 parent d6205ec commit 817dd55
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 72 deletions.
16 changes: 13 additions & 3 deletions content/docs/tutorial/dynamic-enum.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const url = schema['x-url'];
#### Initializing the React Context

Now that we have access to the API URL, we can use React Context to make this data available across our renderers.
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.
[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.
To set up the React Context for your API service, create it in your application as follows:

```js
Expand Down Expand Up @@ -127,7 +127,7 @@ type JsonSchemaWithDependenciesAndEndpoint = JsonSchema & {
dependent: string[];
endpoint: string;
};
export const Country = (
const CountryControl = (
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
) => {
...
Expand Down Expand Up @@ -193,6 +193,11 @@ const CountryControl = (
/>
);
};

export default withJsonFormsEnumProps(
withTranslateProps(React.memo(CountryControl)),
false
);
```

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.
Expand All @@ -214,7 +219,7 @@ type JsonSchemaWithDependenciesAndEndpont = JsonSchema & {
endpoint: string;
};

export const Region = (
const RegionControl = (
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
) => {
const schema = props.schema as JsonSchemaWithDependenciesAndEndpont;
Expand Down Expand Up @@ -254,5 +259,10 @@ export const Region = (
/>
);
};

export default withJsonFormsEnumProps(
withTranslateProps(React.memo(RegionControl)),
false
);
```
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.
6 changes: 3 additions & 3 deletions src/components/common/api.ts → src/components/common/api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export class API {
private url: string;
url;

constructor(url: string) {
constructor(url) {
this.url = url;
}

async get(endpoint: string): Promise<string[]> {
async get(endpoint){
switch (this.url + '/' + endpoint) {
case 'www.api.com/regions/Germany':
return germanStates;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import React, { useEffect } from 'react';
import { useState } from 'react';
import { ControlProps, JsonSchema, OwnPropsOfEnum } from '@jsonforms/core';
import { TranslateProps } from '@jsonforms/react';
import { withJsonFormsEnumProps, withTranslateProps } from '@jsonforms/react';
import { CircularProgress } from '@mui/material';
import { Unwrapped, WithOptionLabel } from '@jsonforms/material-renderers';
import { Unwrapped } from '@jsonforms/material-renderers';
import { APIContext } from '../../docs/tutorials/dynamic-enum';

const { MaterialEnumControl } = Unwrapped;

type JsonSchemaWithDependenciesAndEndpoint = JsonSchema & {
dependent: string[];
endpoint: string;
};

export const Country = (
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
const CountryControl = (
props
) => {
const { handleChange } = props;
const [options, setOptions] = useState<string[]>([]);
const [options, setOptions] = useState([]);
const api = React.useContext(APIContext);
const schema = props.schema as JsonSchemaWithDependenciesAndEndpoint;
const schema = props.schema ;

const endponit = schema.endpoint;
const dependent: string[] = schema.dependent ? schema.dependent : [];
const dependent = schema.dependent ? schema.dependent : [];

useEffect(() => {
setOptions([]);
Expand All @@ -38,7 +33,7 @@ export const Country = (
return (
<MaterialEnumControl
{...props}
handleChange={(path: string, value: any) => {
handleChange={(path, value) => {
handleChange(path, value);
dependent.forEach((path) => {
handleChange(path, undefined);
Expand All @@ -49,4 +44,9 @@ export const Country = (
})}
/>
);
};
};

export default withJsonFormsEnumProps(
withTranslateProps(React.memo(CountryControl)),
false
);
18 changes: 0 additions & 18 deletions src/components/common/country/CountryControl.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import React from 'react';
import { useState } from 'react';
import { ControlProps, JsonSchema, OwnPropsOfEnum } from '@jsonforms/core';
import { TranslateProps, useJsonForms } from '@jsonforms/react';
import { useJsonForms, withJsonFormsEnumProps, withTranslateProps } from '@jsonforms/react';
import { CircularProgress } from '@mui/material';
import { Unwrapped, WithOptionLabel } from '@jsonforms/material-renderers';
import { Unwrapped } from '@jsonforms/material-renderers';
import { APIContext } from '../../docs/tutorials/dynamic-enum';
const { MaterialEnumControl } = Unwrapped;

type JsonSchemaWithDependenciesAndEndpont = JsonSchema & {
dependent: string[];
endpoint: string;
};

export const Region = (
props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps
const RegionControl = (
props
) => {
const schema = props.schema as JsonSchemaWithDependenciesAndEndpont;
const schema = props.schema;
const { handleChange } = props;
const [options, setOptions] = useState<string[]>([]);
const [options, setOptions] = useState([]);
const api = React.useContext(APIContext);
const country = useJsonForms().core?.data.country;
const [previousCountry, setPreviousCountry] = useState<String>();
const [previousCountry, setPreviousCountry] = useState();

const endponit = schema.endpoint;
const dependent: string[] = schema.dependent ? schema.dependent : [];
const dependent = schema.dependent ? schema.dependent : [];

if (previousCountry !== country) {
setOptions([]);
Expand All @@ -40,7 +34,7 @@ export const Region = (
return (
<MaterialEnumControl
{...props}
handleChange={(path: string, value: any) => {
handleChange={(path, value) => {
handleChange(path, value);
dependent.forEach((path) => {
handleChange(path, undefined);
Expand All @@ -51,4 +45,9 @@ export const Region = (
})}
/>
);
};
};

export default withJsonFormsEnumProps(
withTranslateProps(React.memo(RegionControl)),
false
);
18 changes: 0 additions & 18 deletions src/components/common/region/RegionControl.tsx

This file was deleted.

0 comments on commit 817dd55

Please sign in to comment.