formio / formio.js

JavaScript powered Forms with JSON Form Builder
https://formio.github.io/formio.js
MIT License
1.86k stars 1.05k forks source link

Single checkbox label has no "for" attribute and input has no id #5469

Closed McGern closed 3 months ago

McGern commented 7 months ago

Thanks, this is great library, but noticed that when a single checkbox is rendered, there is no "for" attribute, meaning that label can't be clicked to toggle the checkbox state. When using "selectboxes" (i.e. multiple checkboxes) it works fine.

Assume latest version, because it's happening in the live form builder demo and the example pages - showing as v5.0.0-rc.27

example 1

example 2

VikkiAlenn commented 4 months ago

Thank you for reporting this issue. I am able to reproduce and have logged a ticket for the backlog (FIO-8199). We are always willing to review any contributions to resolve this issue from the open source community.

demetris-manikas commented 4 months ago

This worked for me.

Templates.current = {
    checkbox: {
        form: (ctx: any) => {
            const inputId = `${ctx.id}-${ctx.key}`
            const input = `
                <input
                ref="input"
                class="form-check-input"
                type="checkbox"
                id="${inputId}"
                >
            `;
            const label = `
                <label class="${ctx.input.labelClass} form-check-label" for="${inputId}">
                    <span>${ctx.component.label}</span>
                </label>
            `;
            return `
            <div class="form-check">
                ${input}
                ${label}
            </div>
            `
        }
    }
};
lane-formio commented 3 months ago

Resolved here: https://github.com/formio/formio.js/pull/5581

demetris-manikas commented 3 months ago

Nice. Any chance of seeing this in 5x.rc version anytime soon?

lane-formio commented 3 months ago

I expect so at least in the next month.