You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/operate/customize/look-and-feel/override_components.md
+76-90Lines changed: 76 additions & 90 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,10 @@
2
2
3
3
*Introduced in InvenioRDM v11*
4
4
5
-
This documentation is targeted to developers who want to customize specific UI React components in an instance.
5
+
This documentation is targeted at developers who want to customize specific UI React components in an instance.
6
6
For this guide, we assume that you are familiar with React and JavaScript.
7
7
8
-
## Override React components
8
+
## About overriding
9
9
10
10
!!! warning "Experimental feature"
11
11
@@ -18,44 +18,34 @@ For this guide, we assume that you are familiar with React and JavaScript.
18
18
The UI of InvenioRDM is composed of classic HTML web pages for mostly static content, and React web apps for very dynamic content to enhance the user experience.
19
19
While a [dedicated guide](./templates.md) describes how to override HTML web pages (Jinja templates), this guide focus on how to override React components.
20
20
21
-
InvenioRDM uses the React library [`react-overridable`](https://github.com/indico/react-overridable). The library provides a mechanism to mark React components as "overridable" by ID.
21
+
InvenioRDM uses the React library [`react-overridable`](https://github.com/indico/react-overridable).
22
+
The library provides a mechanism to mark React components as "overridable" by ID, which is implemented for many components across the InvenioRDM codebase.
22
23
Developers can define a mapping which is then applied when each React component is rendered.
23
-
The override can be specified in one of three ways:
24
24
25
-
- completely replacing a component with a custom one
26
-
- a static override of the props passed to the component
27
-
- a 'dynamic' override of the props based on the form's state
28
-
29
-
As example for this guide, you will learn how to override the UI React component **in the upload form that marks a record as "Metadata only"**. More specifically, you will replace the "Metadata-only record" checkbox with a toggle, a "switch-like" component.
25
+
## 1. Find the component to override
30
26
31
-

32
-
33
-
## Guided example
34
-
35
-
### 1. Identify which component to override
36
-
37
-
At the moment, the easiest way to understand how to identify if the component that you want to override is a classic HTML component or a React component is to use the Developer Tools in your browser (e.g. [Chrome](https://developer.chrome.com/docs/devtools/) or [Firefox DevTools](https://firefox-source-docs.mozilla.org/devtools-user/)). You can inspect the code and take advantage of some useful [React browser extensions](https://beta.reactjs.org/learn/react-developer-tools) to select and inspect elements:
27
+
At the moment, the easiest way to identify if the component that you want to override is a classic HTML component or a React component is to use the Developer Tools in your browser (e.g. [Chrome](https://developer.chrome.com/docs/devtools/) or [Firefox DevTools](https://firefox-source-docs.mozilla.org/devtools-user/)). You can inspect the code and take advantage of the [React Developer Tools](https://react.dev/learn/react-developer-tools) browser extension to select and inspect elements:
You can then find the component in the InvenioRDM modules source code, searching it in your local development environment or using the search feature in GitHub in the [inveniosoftware organization](https://github.com/search?q=org%3Ainveniosoftware+FileUploaderToolbar&type=code).
42
-
43
-
You can always [ask for help](../../../install/troubleshoot.md#getting-help)!
31
+
If the component shows up in the React tree, it is a React component and can be overriden using the methods described on this page.
32
+
Otherwise, it is an HTML component that can be [overriden using Jinja templates](./templates.md).
44
33
45
-
### 2. Find the React Overridable ID
46
-
47
-
The easiest way to to find the ID of an overridable component is to use `react-overridable`'s built-in developer tool.
34
+
Next, you can find the ID of an overridable component using `react-overridable`'s built-in developer tool.
48
35
Simply open a browser console on your local instance and call the global function `reactOverridableEnableDevMode()`.
49
36
All overridable components will display a small red overlay tag showing their ID.
50
37
You can click a tag to copy its ID to your clipboard.
51
38
52
39

53
40
54
-
The React component's overridable ID for the '`Metadata-only record`' checkbox component is `InvenioRdmRecords.DepositForm.FileUploaderToolbar.MetadataOnlyToggle`. It can be found in the [`invenio-rdm-records`](https://github.com/inveniosoftware/invenio-rdm-records/blob/dd72962b713f07b81699f7d5c9a8a673d585466a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/FileUploaderToolbar.js#L56) module.
41
+
You can search the ID in the [`inveniosoftware` organisation on GitHub](https://github.com/inveniosoftware/) to find the component and its props, which
42
+
can be helpful when overriding.
43
+
44
+
If you're struggling, you can always [ask for help](../../../install/troubleshoot.md#getting-help)!
55
45
56
-
### 3. Find or create the mapping file
46
+
##2. Find or create the mapping file
57
47
58
-
In new InvenioRDM v11 or above installations, a near-empty file named `mapping.js` is available at the following path in your `assets` folder:
48
+
In new InvenioRDM installations at v11 or above, a near-empty file named `mapping.js` is available at the following path in your `assets` folder:
59
49
60
50
```terminal
61
51
├── assets
@@ -71,75 +61,24 @@ For existing installations, you will have to create it. It is a very simple file
71
61
exportconstoverriddenComponents= {};
72
62
```
73
63
74
-
The `const overriddenComponents` is the map that will contain all your future overridden components.
75
-
76
-
### 4. Create a new component creation
77
-
78
-
Let's create a new React component, very similar to the default `FileUploaderToolbar`, changing the UI component that will render. In the same file `mapping.js`, add the following code above the `const overriddenComponents`:
64
+
The `const overriddenComponents` is the map that will contain all your future overrides.
In many cases, you simply want to make a small modification to a component by changing one of its props instead of recreating the component from scratch by yourself.
136
-
This can either be done by specifying the desired prop overrides statically, or by expressing them as a function of the form state.
70
+
- a static override of the props passed to the component
71
+
- a 'dynamic' override of the props based on the form's state
72
+
- completely replacing a component with a custom one
137
73
138
-
### Static
74
+
### a. Statically override a component's props
139
75
140
76
You can use the `parametrize` function built into `react-overridable`, into which you need to pass the component you wish to override and an object containing your props.
141
77
These props will be 'merged' with the existing props, with yours taking precedence over existing ones of the same name.
142
78
79
+
In this case, the props are defined once in your `mapping.js` file and are not updated during the runtime of the application.
80
+
You are also unable to access any React/Formik context while defining the props.
The built-in fields in the deposit form (e.g. title, description, etc.) have a number of common props to make customizing basic functionality easier.
196
182
@@ -202,7 +188,7 @@ The following props may be overriden on all built-in fields:
202
188
-`placeholder`: same as the [HTML attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/placeholder)
203
189
204
190
Additionally, the following props may be overriden on fields that are not mandatory.
205
-
At the moment, this is all fields except the Resource Type, Title, Publication Date, and Creatibutors.
191
+
At the moment, this is all fields except Resource Type, Title, Publication Date, and Creatibutors.
206
192
207
193
-`hidden`: if `true`, the field is not rendered at all
208
194
- If a field already had a value before being hidden, this will still be included in the model and will be sent to the server when the form is submitted.
@@ -214,7 +200,7 @@ At the moment, this is all fields except the Resource Type, Title, Publication D
214
200
Many fields have their own props in addition to these.
215
201
Please [view the source code](https://github.com/inveniosoftware/invenio-rdm-records/tree/master/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields) for more details.
0 commit comments