Skip to content

Commit 9d1ff6d

Browse files
fix: Improved the widget/field customization docs (#4302)
* fix Improved the widget/field customization docs Updated the widget and field customization docs to add examples of wrapping a widget/field to adjust props * Apply suggestions from code review - Responded to reviewer feedback Co-authored-by: Nick Grosenbacher <nickgrosenbacher@gmail.com> --------- Co-authored-by: Nick Grosenbacher <nickgrosenbacher@gmail.com>
1 parent f0eb734 commit 9d1ff6d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ should change the heading of the (upcoming) version to include a major version b
1616
1717
-->
1818

19+
# 5.21.2
20+
21+
## Dev / docs / playground
22+
23+
- Updated the `custom-widgets-fields.md` to add examples of wrapping a widget/field
24+
1925
# 5.21.1
2026

2127
## @rjsf/utils

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,34 @@ render(<Form schema={schema} uiSchema={uiSchema} validator={validator} />, docum
279279

280280
All the widgets that render a text input use the `BaseInputTemplate` component internally. If you need to customize all text inputs without customizing all widgets individually, you can provide a `BaseInputTemplate` component in the `templates` property of `Form` (see [Custom Templates](./custom-templates.md#baseinputtemplate)).
281281

282+
### Wrapping an existing widget to customize it
283+
284+
Sometimes you just need to customize the properties that are passed to an existing widget.
285+
The way to do this varies based upon whether you are using core or some other theme (such as mui).
286+
287+
Here is an example of modifying the `SelectWidget` to change the ordering of `enumOptions`:
288+
289+
```tsx
290+
import { WidgetProps } from '@rjsf/utils';
291+
import { getDefaultRegistry } from '@rjsf/core';
292+
import { Widgets } from '@rjsf/mui';
293+
294+
import myOptionsOrderFunction from './myOptionsOrderFunction';
295+
296+
const {
297+
widgets: { SelectWidget },
298+
} = getDefaultRegistry(); // To get widgets from core
299+
// const { SelectWidget } = Widgets; // To get widgets from a theme do this
300+
301+
function MySelectWidget(props: WidgetProps) {
302+
const { options } = props;
303+
let { enumOptions } = options;
304+
// Reorder the `enumOptions` however you want
305+
enumOptions = myOptionsOrderFunction(enumOptions);
306+
return <SelectWidget {...props} options={{ ...options, enumOptions }} />;
307+
}
308+
```
309+
282310
## Custom field components
283311

284312
You can provide your own field components to a uiSchema for basically any json schema data type, by specifying a `ui:field` property.
@@ -475,3 +503,37 @@ const schema: RJSFSchema = {
475503

476504
render(<Form schema={schema} validator={validator} fields={fields} />, document.getElementById('app'));
477505
```
506+
507+
### Wrapping an existing field to customize it
508+
509+
Sometimes you just need to customize the properties that are passed to an existing field.
510+
511+
Here is an example of wrapping the `ObjectField` to tweak the `onChange` handler to look for a specific kind of bad data:
512+
513+
```tsx
514+
import { useCallback } from 'react';
515+
import { FieldProps } from '@rjsf/utils';
516+
import { getDefaultRegistry } from '@rjsf/core';
517+
518+
import checkBadData from './checkBadData';
519+
520+
const {
521+
fields: { ObjectField },
522+
} = getDefaultRegistry();
523+
524+
function MyObjectField(props: FieldProps) {
525+
const { onChange } = props;
526+
const onChangeHandler = useCallback(
527+
(newFormData: T | undefined, es?: ErrorSchema<T>, id?: string) => {
528+
let data = newFormData;
529+
let error = es;
530+
if (checkBadData(newFormData)) {
531+
// Format the `error` and fix the `data` here
532+
}
533+
onChange(data, error, id);
534+
},
535+
[onChange]
536+
);
537+
return <ObjectField {...props} onChange={onChangeHandler} />;
538+
}
539+
```

0 commit comments

Comments
 (0)