-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathgetTemplate.ts
37 lines (36 loc) · 1.84 KB
/
getTemplate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { FormContextType, TemplatesType, Registry, UIOptionsType, StrictRJSFSchema, RJSFSchema } from './types';
/** Returns the template with the given `name` from either the `uiSchema` if it is defined or from the `registry`
* otherwise. NOTE, since `ButtonTemplates` are not overridden in `uiSchema` only those in the `registry` are returned.
*
* @param name - The name of the template to fetch, restricted to the keys of `TemplatesType`
* @param registry - The `Registry` from which to read the template
* @param [uiOptions={}] - The `UIOptionsType` from which to read an alternate template
* @returns - The template from either the `uiSchema` or `registry` for the `name`
*/
export default function getTemplate<
Name extends keyof TemplatesType<T, S, F>,
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(name: Name, registry: Registry<T, S, F>, uiOptions: UIOptionsType<T, S, F> = {}): TemplatesType<T, S, F>[Name] {
const { templates } = registry;
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
((uiOptions as any)[name] as TemplatesType<T, S, F>[Name]) || templates[name]
);
}