From 2fc826cc68d010004d9e5bc89e9f726c4d9f588f Mon Sep 17 00:00:00 2001 From: Preston Goforth
Date: Wed, 18 Sep 2024 10:59:06 -0400 Subject: [PATCH] feat: Add custom widget handler for `object` --- packages/form/src/triage/object.ts | 14 +++++++++++++- packages/form/src/triage/primitive.ts | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/form/src/triage/object.ts b/packages/form/src/triage/object.ts index 9d13bc1..277aa7a 100644 --- a/packages/form/src/triage/object.ts +++ b/packages/form/src/triage/object.ts @@ -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`
${options.message}
`; + } + const children = Object.entries(schema.properties).map( ([propName, propValue]) => { if (Array.isArray(propValue) || typeof propValue === 'boolean') @@ -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 }) ?? diff --git a/packages/form/src/triage/primitive.ts b/packages/form/src/triage/primitive.ts index 0566931..2eb8674 100644 --- a/packages/form/src/triage/primitive.ts +++ b/packages/form/src/triage/primitive.ts @@ -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); } }