-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(templates): Allow customization per-field by using Registry string key #3881
base: main
Are you sure you want to change the base?
Changes from all commits
11df0a3
6cdeb43
1579d61
d24956d
26edad0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To truly support things in a way that matches how
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I was wrong. It will require creating a new interface called
Then you will want to update all of the interfaces and types that the Then add the following to the top of the
|
||
// 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,4 +86,19 @@ describe('getTemplate', () => { | |
expect(getTemplate<typeof name>(name, registry, uiOptions)).toBe(CustomTemplate); | ||
}); | ||
}); | ||
it('returns the template from registry using uiOptions key when available', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add another set of tests for custom names |
||
KEYS.forEach(key => { | ||
const name = key as keyof TemplatesType; | ||
expect( | ||
getTemplate<typeof name>( | ||
name, | ||
registry, | ||
Object.keys(uiOptions).reduce((uiOptions, key) => { | ||
uiOptions[key] = key; | ||
return uiOptions; | ||
}, {}) | ||
) | ||
).toBe(FakeTemplate); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding at least one example of how to use an alternate template with a different name in the
uiSchema
here and possibly all the sections you've updated below