Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fea0336
[MISC] Retrieved change from older PR
Oct 28, 2024
4fc99d1
[MISC] Updated documentation
Oct 28, 2024
8640c60
[MISC] Updated types
Oct 28, 2024
15e43ff
[EVO] Added test for custom template name in registry
Oct 28, 2024
485cce7
[FIX] Docx
Oct 28, 2024
0c1839b
[FIX] Templates typing
Oct 28, 2024
c772a6b
[MISC] Updated docs
Oct 28, 2024
b26110f
Merge branch 'main' into feat/custom-template-per-field
lucasmcht Oct 28, 2024
1575178
Merge pull request #2 from lucasmcht/feat/custom-template-per-field
lucasmcht Oct 29, 2024
55a3ccf
Merge branch 'main' into feat/custom-template-per-field
lucasmcht Oct 31, 2024
7777848
[MISC] Moved type and removed blank lines
Nov 4, 2024
2d0c05f
Merge branch 'main' into feat/custom-template-per-field
lucasmcht Nov 4, 2024
a46c87f
[FIX] Template type
Nov 4, 2024
804ba62
Merge branch 'feat/custom-template-per-field' of https://github.com/l…
Nov 4, 2024
6e231d3
Merge branch 'main' into feat/custom-template-per-field
heath-freenome Nov 13, 2024
7527692
Merge branch 'main' into feat/custom-template-per-field
heath-freenome Dec 6, 2024
bfeae84
Merge with master
lucasmcht Mar 23, 2025
e447c9c
FIX: Fixed tests
lucasmcht Mar 23, 2025
008752b
MISC Merge with master
lucasmcht Mar 23, 2025
da78a2b
FIX getTemplate.test.ts
lucasmcht Mar 23, 2025
0c5a97c
fix merge
lucasmcht Mar 23, 2025
f6c5888
DOCS: Add section for custom templates in registry
lucasmcht Mar 23, 2025
a30d506
FIX: Node 14 compat
lucasmcht Mar 27, 2025
36a2ef5
Merge branch 'main' into feat/custom-template-per-field
heath-freenome May 1, 2025
b2cdb68
Update types.ts
heath-freenome May 1, 2025
015bb9f
Update packages/utils/src/types.ts
heath-freenome May 1, 2025
0b18880
Apply suggestions from code review
heath-freenome May 2, 2025
4072cdf
Apply suggestions from code review
heath-freenome May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ should change the heading of the (upcoming) version to include a major version b
- BREAKING CHANGE: Removed the deprecated `RJSF_ADDITONAL_PROPERTIES_FLAG` constant
- Updated the `WrapIfAdditionalTemplateProps` to include `hideError` and `rawErrors` in support of moving `Bootstrap 3` marker classes out of `SchemaField`
- Added support for `patternProperties` [#1944](https://github.com/rjsf-team/react-jsonschema-form/issues/1944)
- Updated `getTemplate()` to allow per-field customization using string key from `Registry`, fixing [#3695](https://github.com/rjsf-team/react-jsonschema-form/issues/3695).
- Updated `TemplatesType` to allow for a string key to be used to reference a custom template in the `Registry`, fixing [#3695](https://github.com/rjsf-team/react-jsonschema-form/issues/3695)
- Updated tests to cover the new `getTemplate()` functionality

## @rjsf/validator-ajv6

Expand All @@ -134,6 +137,7 @@ should change the heading of the (upcoming) version to include a major version b
- Replaced Lerna with Nx, updated all lerna commands to use the Nx CLI
- BREAKING CHANGE: Updated all `peerDependencies` to change minimal `React` support to `>=18`
- Added documentation and playground example for `patternProperties`
- Updated `advanced-customization/custom-templates` with the new feature.

# 6.0.0-alpha.0

Expand Down
84 changes: 78 additions & 6 deletions packages/docs/docs/advanced-customization/custom-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,27 @@ render(
);
```

You also can provide your own field template to a uiSchema by specifying a `ui:ArrayFieldTemplate` property.
You also can provide your own field template to a uiSchema by specifying a `ui:ArrayFieldTemplate` property with your Component :

```tsx
import { UiSchema } from '@rjsf/utils';
import ArrayFieldTemplate from './ArrayFieldTemplate';

const uiSchema: UiSchema = {
'ui:ArrayFieldTemplate': ArrayFieldTemplate,
};
```

or a string value from the `Registry` :

```tsx
import { UiSchema } from '@rjsf/utils';

const uiSchema: UiSchema = {
'ui:ArrayFieldTemplate': 'CustomArrayFieldTemplate',
};
```

Please see the [customArray.tsx sample](https://github.com/rjsf-team/react-jsonschema-form/blob/main/packages/playground/src/samples/customArray.tsx) from the [playground](https://rjsf-team.github.io/react-jsonschema-form/) for another example.

The following props are passed to each `ArrayFieldTemplate`:
Expand Down Expand Up @@ -165,16 +176,27 @@ render(
);
```

You also can provide your own template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property.
You also can provide your own field template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property with your Component :

```tsx
import { UiSchema } from '@rjsf/utils';
import ArrayFieldDescriptionTemplate from './ArrayFieldDescriptionTemplate';

const uiSchema: UiSchema = {
'ui:ArrayFieldDescriptionTemplate': ArrayFieldDescriptionTemplate,
};
```

or a string value from the `Registry` :

```tsx
import { UiSchema } from '@rjsf/utils';

const uiSchema: UiSchema = {
'ui:ArrayFieldDescriptionTemplate': 'CustomArrayFieldDescriptionTemplate',
};
```

The following props are passed to each `ArrayFieldDescriptionTemplate`:

- `description`: The description of the array field being rendered.
Expand Down Expand Up @@ -322,13 +344,24 @@ render(
);
```

You also can provide your own template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property.
You also can provide your own template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property with your Component :

```tsx
import { UiSchema } from '@rjsf/utils';
import ArrayFieldDescriptionTemplate from './ArrayFieldDescriptionTemplate';

const uiSchema: UiSchema = {
'ui:ArrayFieldDescriptionTemplate': ArrayFieldDescriptionTemplate,
};
```

or a string value from the `Registry` :

```tsx
import { UiSchema } from '@rjsf/utils';

const uiSchema: UiSchema = {
'ui:ArrayFieldTitleTemplate': ArrayFieldTitleTemplate,
'ui:ArrayFieldDescriptionTemplate': 'CustomArrayFieldDescriptionTemplate',
};
```

Expand Down Expand Up @@ -676,16 +709,27 @@ render(
);
```

You also can provide your own field template to a uiSchema by specifying a `ui:FieldTemplate` property.
You also can provide your own field template to a uiSchema by specifying a `ui:FieldTemplate` property with your Component :

```tsx
import { UiSchema } from '@rjsf/utils';
import CustomFieldTemplate from './CustomFieldTemplate';

const uiSchema: UiSchema = {
'ui:FieldTemplate': CustomFieldTemplate,
};
```

or a string value from the `Registry` :

```tsx
import { UiSchema } from '@rjsf/utils';

const uiSchema: UiSchema = {
'ui:FieldTemplate': 'CustomFieldTemplate',
};
```

If you want to handle the rendering of each element yourself, you can use the props `rawHelp`, `rawDescription` and `rawErrors`.

The following props are passed to a custom field template component:
Expand Down Expand Up @@ -789,16 +833,27 @@ render(
);
```

You also can provide your own field template to a uiSchema by specifying a `ui:ObjectFieldTemplate` property.
You also can provide your own field template to a uiSchema by specifying a `ui:ObjectFieldTemplate` property with your Component :

```tsx
import { UiSchema } from '@rjsf/utils';
import ObjectFieldTemplate from './ObjectFieldTemplate';

const uiSchema: UiSchema = {
'ui:ObjectFieldTemplate': ObjectFieldTemplate,
};
```

or a string value from the `Registry` :

```tsx
import { UiSchema } from '@rjsf/utils';

const uiSchema: UiSchema = {
'ui:ObjectFieldTemplate': 'ObjectFieldTemplate',
};
```

Please see the [customObject.tsx sample](https://github.com/rjsf-team/react-jsonschema-form/blob/main/packages/playground/src/samples/customObject.tsx) from the [playground](https://rjsf-team.github.io/react-jsonschema-form/) for a better example.

The following props are passed to each `ObjectFieldTemplate` as defined by the `ObjectFieldTemplateProps` in `@rjsf/utils`:
Expand Down Expand Up @@ -1145,3 +1200,20 @@ The following prop is passed to a `SubmitButton`:

- `uiSchema`: The uiSchema object for this field, used to extract the `UISchemaSubmitButtonOptions`.
- `registry`: The `registry` object.

## Custom Templates

You can now add custom components to the registry and reference them in your `uiSchema` using string keys.

### Adding Custom Templates to the Registry

```tsx
import CustomArrayFieldTemplate from './CustomArrayFieldTemplate';
import { UiSchema } from '@rjsf/utils';

// Add the custom template to the registry
const registry = { templates: { CustomArrayFieldTemplate } };

// Use the custom template in the uiSchema
const uiSchema: UiSchema = { 'ui:ArrayFieldTemplate': 'CustomArrayFieldTemplate' };
```
11 changes: 11 additions & 0 deletions packages/utils/src/getTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export default function getTemplate<
if (name === 'ButtonTemplates') {
return templates[name];
}
// Allow templates to be customized per-field by using string keys from the registry
if (
Object.hasOwn(uiOptions, name) &&
typeof uiOptions[name] === 'string' &&
Object.hasOwn(templates, uiOptions[name] as string)
) {
const key = uiOptions[name];
// Evaluating templates[key] results in TS2590: Expression produces a union type that is too complex to represent
// To avoid that, we cast templates to `any` before accessing the key field
return (templates as any)[key];
}
return (
// Evaluating uiOptions[name] results in TS2590: Expression produces a union type that is too complex to represent
// To avoid that, we cast uiOptions to `any` before accessing the name field
Expand Down
Loading