Skip to content

feat: Add custom widget handler for object #13

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

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/form/src/json-schema-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,12 @@ export class Jsf extends LitElement {

/* When user hit "Enter" while in some adequate inputs */
protected _handleKeydown(event: KeyboardEvent) {
console.log('cccccccccccccc');
const hasModifier =
event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;

if (event.key === 'Enter' && !hasModifier) {
setTimeout(() => {
if (!event.defaultPrevented && !event.isComposing) {
console.log({ event });
const form = this.#formRef.value!;
// const valid = form.reportValidity();
let valid = true;
Expand Down Expand Up @@ -398,7 +396,7 @@ export class Jsf extends LitElement {
#submit = () => {
const options: Widgets['submit'] = {
id: '__submit_button',
label: this.submitButtonLabel,
label: this.submitButtonText,
};
const error = 'Missing submit widget.';
return (
Expand All @@ -421,7 +419,6 @@ export class Jsf extends LitElement {
${ref(this.#formRef)}
part="base"
@submit=${(event: Event) => {
console.log('hey');
event.preventDefault();

const valid = (event.target as HTMLFormElement).reportValidity();
Expand Down
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