Skip to content

Commit

Permalink
feat: Add custom widget handler for object
Browse files Browse the repository at this point in the history
  • Loading branch information
pgoforth committed Sep 18, 2024
1 parent 1e082e4 commit 2fc826c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/form/src/triage/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export const fieldObject = (
if (typeof schema.properties !== 'object')
return widgets.callout?.({ id: '', message: error }) ?? html`${error}`;

const id = path.join('.');

function missing(widgetName: string) {
const options = { id, message: `Missing ${widgetName} widget.` };
return widgets?.callout?.(options) ?? html`<p>${options.message}</p>`;
}

const children = Object.entries(schema.properties).map(
([propName, propValue]) => {
if (Array.isArray(propValue) || typeof propValue === 'boolean')
Expand Down Expand Up @@ -59,13 +66,18 @@ export const fieldObject = (
if (typeof uiSchema?.['ui:title'] === 'string') label = uiSchema['ui:title'];

const options = {
id: path.join('.'),
id,
label,
helpText: schema.description,
children,
level,
};

if (typeof uiSchema?.['ui:widget'] === 'string') {
const customWidgetName = uiSchema?.['ui:widget'];
return widgets?.[customWidgetName]?.(options) || missing(customWidgetName);
}

return (
widgets?.object?.(options) ??
widgets?.callout?.({ id: '', message: error }) ??
Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/triage/primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const fieldPrimitive = (
if (typeof uiOptions?.['ui:widget'] === 'string') {
const customWidgetName = uiOptions?.['ui:widget'];
if (customWidgetName !== 'password') {
return widgets?.[customWidgetName]?.(options) || missing('custom');
return widgets?.[customWidgetName]?.(options) || missing(customWidgetName);
}
}

Expand Down

0 comments on commit 2fc826c

Please sign in to comment.