Skip to content

Commit b25cb60

Browse files
Fix (rjsf-team#2762) by introducing sub-packages that are restricted to each material-ui version (rjsf-team#2840)
* Fixed (rjsf-team#2762) by introducing sub-packages that are restricted to each theme - Updated the `CHANGELOG.md` file for this fix - In the `material-ui` package - Updated the `README.md` to document the new sub-packages as well as `Typescript` and `jest` workarounds - Updated `package*.json` to add the `rollup-plugin-copy` - Also updated the `build` script to support cleaning up a new `dist-temp` directory and building the main `index.js` as well as the `v4` and `v5` sub-packages - Added the `src/v4.ts` file that only imports the files related to the `material-ui` version 4 theme - Added the `src/v5.ts` file that only imports the files related to the `material-ui` version 5 theme - Added a `tsds.config.js` file that customized `tsdx` to support building the main `index.js as well as the `v4` and `v5` sub-packages * - Fixed up some minor documentation issues * - Removed empty `dependencies` block that was added by mistake * - Also added documentation to close (rjsf-team#2757) * - updated change log again * - improved the customization guide slightly * - Added field customization to the material-ui guide * - Updated support for the cjs and esm builds * - removed the `module` since it is no longer needed * - restore `module` and `main` in `package.json` to fix the build (hopefully)
1 parent d87a88b commit b25cb60

File tree

11 files changed

+568
-19298
lines changed

11 files changed

+568
-19298
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ build
119119
_build
120120
site
121121
dist
122+
dist-temp
122123
lib
123124
yarn.lock
124125

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ should change the heading of the (upcoming) version to include a major version b
2424

2525
## Dev / docs / playground
2626
- Enable ui options in playground, to demonstrate submit button options (https://github.com/rjsf-team/react-jsonschema-form/pull/2640)
27+
- Document the `material-ui` context and hook (#2757)
2728

2829
## @rjsf/material-ui
2930
- SubmitButton widget to use new ui:submitButtonOptions on the submit button for forms (https://github.com/rjsf-team/react-jsonschema-form/pull/2833)
31+
- Fixed bundler warning issue (#2762) by exporting a `@rjsf/material-ui/v4` and `@rjsf/material-ui/v5` sub-package
32+
- NOTE: `@rjsf/material-ui` was retained to avoid a breaking change, but using it will continue to cause bundler warnings
33+
- See the `README.md` for the `@rjsf/material-ui` package for updated usage information
3034

3135
## @rjsf/bootstrap-4
3236
- SubmitButton widget to use new ui:submitButtonOptions on the submit button for forms (https://github.com/rjsf-team/react-jsonschema-form/pull/2640)
@@ -47,7 +51,7 @@ should change the heading of the (upcoming) version to include a major version b
4751
- Add new `useMuiComponent()` hook as a shortcut for `useContext(MuiComponentContext)`
4852

4953
## @rjsf/semantic-ui
50-
- Added support for additionalProperties schema property (https://github.com/rjsf-team/react-jsonschema-form/pull/2817)
54+
- Added support for additionalProperties schema property (#2817)
5155
# v4.1.0
5256

5357
## @rjsf/core

docs/advanced-customization/custom-widgets-fields.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,5 @@ render((
340340
If you're curious how this could ever be useful, have a look at the [Kinto formbuilder](https://github.com/Kinto/formbuilder) repository to see how it's used to provide editing capabilities to any form field.
341341

342342
Props passed to a custom SchemaField are the same as [the ones passed to a custom field](#field-props).
343+
344+
NOTE: If you are using the `material-ui` theme and are considering customizing a widget or a field, checkout this [guide](material-ui/customizing-material-ui.md).
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Customizing material-ui fields and widgets
2+
3+
Unlike most other themes, the `material-ui` theme supports the two distinct version of Material UI (versions 4 and 5) side-by-side.
4+
Material UI version 4 is provided by the scoped packages under `@material-ui` and version 5 is provided by the scoped packages under `@mui`.
5+
6+
The components used by `@rjsf/material-ui` for Material UI version 4 and version 5 have identical names and props.
7+
As a result, all of the `fields` and `widgets` provided by the theme are identical as well.
8+
The trick to making the two versions function side-by-side, was done by creating a React context, `MuiComponentContext`, that provides the appropriate set of components used by theme, for the particular scoped package.
9+
10+
In addition to this context, a custom hook, `useMuiComponent()`, is provided to allow quick access to that component set.
11+
12+
## Example of a custom widget for `@rjsf/material-ui`
13+
14+
Here is an update to the `MyCustomWidget` for the `material-ui` theme
15+
16+
```jsx
17+
const schema = {
18+
type: "string"
19+
};
20+
21+
import { useMuiComponent } from '@rjsf/material-ui/v4';
22+
23+
function MyCustomWidget(props) {
24+
const { options, ...otherProps } = props;
25+
const { color, backgroundColor } = options;
26+
const { TextInput } = useMuiComponent();
27+
return <TextInput {...otherProps} style={{ color, backgroundColor }} />;
28+
}
29+
30+
MyCustomWidget.defaultProps = {
31+
options: {
32+
color: "red"
33+
}
34+
};
35+
36+
const uiSchema = {
37+
"ui:widget": MyCustomWidget,
38+
"ui:options": {
39+
backgroundColor: "yellow"
40+
}
41+
};
42+
43+
// renders red on yellow input
44+
render((
45+
<Form schema={schema}
46+
uiSchema={uiSchema} />
47+
), document.getElementById("app"));
48+
```
49+
50+
## Example of a custom field for `@rjsf/material-ui`
51+
52+
Here is an update to the `GeoPosition` for the `material-ui` theme
53+
54+
```jsx
55+
const schema = {
56+
type: "object",
57+
required: ["lat", "lon"],
58+
properties: {
59+
lat: { type: "number"},
60+
lon: { type: "number" }
61+
}
62+
};
63+
64+
import { useMuiComponent } from '@rjsf/material-ui/v4';
65+
66+
// Define a custom component for handling the root position object
67+
function GeoPosition(props) {
68+
const { lat, lon } = props.formData;
69+
const { Box, TextInput } = useMuiComponent();
70+
71+
const onChangeLat = (event) => {
72+
const { target: { value } } = event;
73+
const newData = { ...props.formData, lat: value };
74+
props.onChange(newData);
75+
};
76+
77+
const onChangeLon = (event) => {
78+
const { target: { value } } = event;
79+
const newData = { ...props.formData, lon: value };
80+
props.onChange(newData);
81+
};
82+
83+
return (
84+
<Box>
85+
<TextInput type="number" value={lat} onChange={onChangeLat} />
86+
<TextInput type="number" value={lon} onChange={onChangeLon} />
87+
</Box>
88+
);
89+
}
90+
91+
// Define the custom field component to use for the root object
92+
const uiSchema = { "ui:field": "geo" };
93+
94+
// Define the custom field components to register; here our "geo"
95+
// custom field component
96+
const fields = { geo: GeoPosition };
97+
98+
// Render the form with all the properties we just defined passed
99+
// as props
100+
render((
101+
<Form
102+
schema={schema}
103+
uiSchema={uiSchema}
104+
fields={fields} />
105+
), document.getElementById("app"));
106+
```

docs/api-reference/uiSchema.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,4 @@ const uiSchema = {
284284
```
285285
## Theme Options
286286
[Semantic UI](themes/semantic-ui/uiSchema.md)
287+
[Chakra UI](themes/chakra-ui/uiSchema.md)

packages/material-ui/README.md

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ Exports `material-ui` version 4 and 5 themes, fields and widgets for `react-json
6565

6666
- `@material-ui/core >= 4.12.0` (in 4.12.0, the library developers made it forward compatible with Material-UI V5)
6767
- `@material-ui/icons >= 4.11.0` (in 4.11.0, the library developers made it forward compatible with Material-UI V5)
68-
- `@rjsf/core >= 3.3.0`
68+
- `@rjsf/core >= 4.0.0`
6969

7070
```bash
7171
yarn add @material-ui/core @material-ui/icons @rjsf/core
7272
```
7373

7474
#### Material UI version 5
7575

76-
NOTE: Material UI 5 requires React 17 so you will need to upgrade
76+
NOTE: Material UI 5 requires React 17, so you will need to upgrade
7777

7878
- `@mui/material`
7979
- `@mui/icons-material`
8080
- `@emotion/react`
8181
- `@emotion/styled`
82-
- `@rjsf/core >= 3.3.0`
82+
- `@rjsf/core >= 4.0.0`
8383

8484
```bash
8585
yarn add @mui/material @mui/icons-material @emotion/react @emotion/styled @rjsf/core
@@ -98,39 +98,101 @@ yarn add @rjsf/material-ui
9898
### Material UI version 4
9999

100100
```js
101-
import Form from '@rjsf/material-ui';
102-
// One could also use the `MuiForm4` alias
103-
// import { MuiForm4 as Form } from '@rjsf/material-ui';
101+
import Form from '@rjsf/material-ui/v4';
104102
```
105103

106104
or
107105

108106
```js
109107
import { withTheme } from '@rjsf/core';
110-
import { Theme as MaterialUITheme } from '@rjsf/material-ui';
111-
// One could also use the `Theme4` alias
112-
// import { Theme4 as MaterialUITheme } from '@rjsf/material-ui';
108+
import Theme from '@rjsf/material-ui/v4';
113109

114110
// Make modifications to the theme with your own fields and widgets
115111

116-
const Form = withTheme(MaterialUITheme);
112+
const Form = withTheme(Theme);
113+
```
114+
115+
### Typescript configuration adjustments
116+
117+
If you are using Typescript you may have to update your `tsconfig.json` file in to avoid problems with import aliasing.
118+
119+
If you are experiencing an error that is similar to `TS2307: Cannot find module '@rjsf/material-ui/v4' or its corresponding type declarations.` you can add the following:
120+
121+
```
122+
{
123+
...
124+
"compilerOptions": {
125+
...
126+
"paths": {
127+
"baseUrl": ".",
128+
"@rjsf/material-ui/*": ["node_modules/@rjsf/material-ui/dist/*"]
129+
}
130+
}
131+
}
132+
```
133+
134+
### Jest configuration adjustments
135+
136+
If you are using Jest you may have to update your `jest.config.json` file in to avoid problems with import aliasing.
137+
138+
If you are experiencing an error that is similar to `Cannot find module '@rjsf/material-ui/v4' from '<file path>'` you can add the following:
139+
140+
```
141+
{
142+
"moduleNameMapper": {
143+
"@rjsf/material-ui/v4": "<rootDir>/node_modules/@rjsf/material-ui/dist/v4.js"
144+
},
145+
}
117146
```
118147

119148
### Material UI version 5
120149

121150
```js
122-
import { MuiForm5 as Form } from '@rjsf/material-ui';
151+
import Form from '@rjsf/material-ui/v5';
123152
```
124153

125154
or
126155

127156
```js
128157
import { withTheme } from '@rjsf/core';
129-
import { Theme5 as Mui5Theme } from '@rjsf/material-ui';
158+
import Theme from '@rjsf/material-ui/v5';
130159

131160
// Make modifications to the theme with your own fields and widgets
132161

133-
const Form = withTheme(Mui5Theme);
162+
const Form = withTheme(Theme);
163+
```
164+
165+
### Typescript configuration adjustments
166+
167+
If you are using Typescript you may have to update your `tsconfig.json` file in to avoid problems with import aliasing.
168+
169+
If you are experiencing an error that is similar to `TS2307: Cannot find module '@rjsf/material-ui/v5' or its corresponding type declarations.` you can add the following:
170+
171+
```
172+
{
173+
...
174+
"compilerOptions": {
175+
...
176+
"paths": {
177+
"baseUrl": ".",
178+
"@rjsf/material-ui/*": ["node_modules/@rjsf/material-ui/dist/*"]
179+
}
180+
}
181+
}
182+
```
183+
184+
### Jest configuration adjustments
185+
186+
If you are using Jest you may have to update your `jest.config.json` file in to avoid problems with import aliasing.
187+
188+
If you are experiencing an error that is similar to `Cannot find module '@rjsf/material-ui/v5' from '<file path>'` you can add the following:
189+
190+
```
191+
{
192+
"moduleNameMapper": {
193+
"@rjsf/material-ui/v5": "<rootDir>/node_modules/@rjsf/material-ui/dist/v5.js"
194+
},
195+
}
134196
```
135197

136198
<!-- ROADMAP -->

0 commit comments

Comments
 (0)