feat(combobox): reuse CheckboxGroup for required ComboboxCheckbox

Merged
PI
4mo ago
+61-2

Result

required-combobox-checkbox

What

ComboboxCheckbox now carries the same ruby-ui--checkbox-group and ruby-ui--form-field wiring that Checkbox already has. A multi-select combobox can therefore reuse the existing CheckboxGroup controller to enforce "at least one option required" — no new controller and no combobox-side validation logic.

Usage mirrors a regular checkbox group: wrap the options in CheckboxGroup(data_required: true). While nothing is checked, the group keeps every option required (so the browser blocks submission); once any option is checked, the constraint is lifted.

Why

There was no client-side "require at least one" for a checkbox combobox. Rather than reimplement group validity inside the combobox controller, this wires ComboboxCheckbox to the controller that already solves it for Checkbox, keeping a single source of truth.

Changes

  • ComboboxCheckbox: add ruby_ui__checkbox_group_target: "checkbox", ruby_ui__form_field_target: "input", and the change->ruby-ui--checkbox-group#onChange / change->ruby-ui--form-field#onInput / invalid->ruby-ui--form-field#onInvalid actions (mirrors Checkbox).
  • Docs: new "Required" example wrapping the options in CheckboxGroup(data_required: true).
  • mcp/data/registry.json rebuilt; gem test added.

Note on "Select all"

ComboboxToggleAllCheckbox sets checked programmatically, which does not emit a change event, so select-all does not re-run CheckboxGroup#onChange. This is harmless for submission: select-all only produces a uniform state — all checked (valid, since each checked option satisfies its own required) or all unchecked (correctly blocked). The only residue is that an already-visible FormFieldError message is not refreshed by the select-all action itself; it clears on the next option interaction. Left as-is (out of scope).

Test instructions

  1. bin/dev in docs/, open /docs/combobox, scroll to the Required example.
  2. Open the combobox and submit the form without checking anything → submission is blocked and FormFieldError shows "Pick at least one option".
  3. Check any option → the error clears and the form submits.
  4. Check one, then uncheck it → the required constraint is reapplied and submission is blocked again.

Verified locally: with nothing checked form.checkValidity() is false; after checking one option it is true; the FormFieldError toggles accordingly. Gem suite green, StandardRB clean.