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
## How can I listen to form changes in the React standalone component?
9
11
10
12
When using JSON Forms within your react app, at some point you'll need to access the current form data.
@@ -182,6 +184,160 @@ Now default values within the schema file can be used:
182
184
"default": "Max"
183
185
}
184
186
```
187
+
188
+
## How to update a field dependent on another field in JSON Forms?
189
+
190
+
Consider a carwash application offering various services and calculating the resulting price.
191
+
The schema includes a multiselect field called services and a price attribute, with the price field set as readonly.
192
+
193
+
<DependentFieldExample/>
194
+
195
+
There are three approaches to update a field in JSON Forms based on the value of another field:
196
+
Utilizeing the JSONF Forms middleware, using the onChange method or create a custom render.
197
+
198
+
#### Approach 1: JSON Forms middleware
199
+
200
+
We can utilize the JSON Forms middleware to compute and set the price.
201
+
JSON Forms utilizes the reducer pattern and various actions to update its state.
202
+
The middleware intercepts the call to the JSON Forms reducers and calls your custom code instead.
203
+
For detailed insights into the JSON Forms middleware, the reducer pattern, and JSON Forms actions, refer to the documentation [here](/docs/middleware).
204
+
In this scenario, we want to customize the behavior associated with the `UPDATE_DATA` action, which is triggered when the form's data is changed by the user.
205
+
We initially invoke JSON Forms default reducer to update the data and identify any errors.
206
+
Subsequently, we adjust the price fields based on the selected services and update the state with the newly calculated data.
207
+
We additionally override the `INIT` and `UPDATE_CORE` actions, in case the data prop passed to JSON Forms doesn't have the correct price set yet.
In the onChange method, the price is calculated based on the selected services.
275
+
If the calculated price matches the existing value, the data remains unchanged.
276
+
However, if the price differs, a new data object is created with the updated price and passed to JSON Forms.
277
+
278
+
:::caution
279
+
280
+
It's important to only create a new object when necessary to avoid infinite loops.
281
+
282
+
:::
283
+
284
+
#### Approach 3: Create a custom renderer
285
+
286
+
Another possibility is to implement a custom renderer.
287
+
A custom renderer allows you to define and use your rendering logic instead of relying on the default renderers provided by JSON Forms.
288
+
For a comprehensive guide and additional information on creating custom renderers, refer to our [Custom Renderers Tutorial](/docs/tutorial/custom-renderers).
289
+
290
+
In this tutorial, we'll create the ServicesRenderer designed to display offered services.
291
+
While the default renderer will still handle displaying the price, we'll calculate and set the price within our custom renderer.
292
+
293
+
The basis of the ServiceRenderer is the MaterialEnumArrayRenderer, which is the default renderer of JSON Forms for this data type.
294
+
JSON Forms renderers usually use the handleChange, addItem, or removeItem functions to set data, so we adjust these functions to not only add or remove items but also dynamically calculate and set the current price based on the selected services.
0 commit comments