Skip to content

Commit fea0336

Browse files
author
LFRW2K\lecmil2
committed
[MISC] Retrieved change from older PR
1 parent c77378b commit fea0336

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
1515
should change the heading of the (upcoming) version to include a major version bump.
1616
1717
-->
18+
# 5.23.0
19+
20+
## @rjsf/utils
21+
22+
- Updated `getTemplate()` to allow per-field customization using string key from `Registry`.
23+
24+
## Dev / docs / playground
25+
26+
- Updated `advanced-customization/custom-templates` with the new feature.
1827

1928
# 5.22.0
2029

packages/docs/docs/advanced-customization/custom-templates.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ render(
7676
);
7777
```
7878

79-
You also can provide your own field template to a uiSchema by specifying a `ui:ArrayFieldTemplate` property.
79+
You also can provide your own field template to a uiSchema by specifying a `ui:ArrayFieldTemplate` property with your Component or a string value from the `Registry`.
8080

8181
```tsx
8282
import { UiSchema } from '@rjsf/utils';
@@ -163,7 +163,7 @@ render(
163163
);
164164
```
165165

166-
You also can provide your own template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property.
166+
You also can provide your own template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property with your Component or a string value from the `Registry`.
167167

168168
```tsx
169169
import { UiSchema } from '@rjsf/utils';
@@ -261,7 +261,7 @@ render(
261261
);
262262
```
263263

264-
You also can provide your own template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property.
264+
You also can provide your own template to a uiSchema by specifying a `ui:ArrayFieldDescriptionTemplate` property with your Component or a string value from the `Registry`.
265265

266266
```tsx
267267
import { UiSchema } from '@rjsf/utils';
@@ -615,7 +615,7 @@ render(
615615
);
616616
```
617617

618-
You also can provide your own field template to a uiSchema by specifying a `ui:FieldTemplate` property.
618+
You also can provide your own field template to a uiSchema by specifying a `ui:FieldTemplate` property with your Component or a string value from the `Registry`.
619619

620620
```tsx
621621
import { UiSchema } from '@rjsf/utils';
@@ -693,7 +693,7 @@ render(
693693
);
694694
```
695695

696-
You also can provide your own field template to a uiSchema by specifying a `ui:ObjectFieldTemplate` property.
696+
You also can provide your own field template to a uiSchema by specifying a `ui:ObjectFieldTemplate` property with your Component or a string value from the `Registry`.
697697

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

packages/utils/src/getTemplate.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ export default function getTemplate<
1818
if (name === 'ButtonTemplates') {
1919
return templates[name];
2020
}
21+
// Allow templates to be customized per-field by using string keys from the registry
22+
if (
23+
Object.hasOwn(uiOptions, name) &&
24+
typeof uiOptions[name] === 'string' &&
25+
Object.hasOwn(templates, uiOptions[name] as string)
26+
) {
27+
const key = uiOptions[name];
28+
// Evaluating templates[key] results in TS2590: Expression produces a union type that is too complex to represent
29+
// To avoid that, we cast templates to `any` before accessing the key field
30+
return (templates as any)[key];
31+
}
2132
return (
2233
// Evaluating uiOptions[name] results in TS2590: Expression produces a union type that is too complex to represent
2334
// To avoid that, we cast uiOptions to `any` before accessing the name field

packages/utils/test/getTemplate.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,19 @@ describe('getTemplate', () => {
8686
expect(getTemplate<typeof name>(name, registry, uiOptions)).toBe(CustomTemplate);
8787
});
8888
});
89+
it('returns the template from registry using uiOptions key when available', () => {
90+
KEYS.forEach((key) => {
91+
const name = key as keyof TemplatesType;
92+
expect(
93+
getTemplate<typeof name>(
94+
name,
95+
registry,
96+
Object.keys(uiOptions).reduce((uiOptions, key) => {
97+
uiOptions[key] = key;
98+
return uiOptions;
99+
}, {})
100+
)
101+
).toBe(FakeTemplate);
102+
});
103+
});
89104
});

0 commit comments

Comments
 (0)