Debian Codegraph @

packages/docs/versioned_docs/version-4.2.3/advanced-customization/material-ui/customizing-material-ui.md

raw
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 ```