) {
super.updated?.(changed);
// `indeterminate` is a DOM property only — no HTML attribute.
if (this._input && changed.has('indeterminate')) {
this._input.indeterminate = this.indeterminate;
} else if (this._input && this.indeterminate && this._input.indeterminate !== true) {
this._input.indeterminate = true;
}
// Re-attach the author's label children after each render.
if (this._label && this.label === undefined && this._labelContent.length) {
// Only repopulate if it's currently empty (Lit's render leaves it
// empty when there's no `${this.label}` text).
if (!this._label.firstChild) {
for (const node of this._labelContent) this._label.appendChild(node.cloneNode(true));
}
}
}
private _onChange = (ev: Event) => {
const target = ev.target as HTMLInputElement;
this.checked = target.checked;
if (this.indeterminate) this.indeterminate = false;
this.dispatchEvent(
new CustomEvent('bs-change', {
bubbles: true,
composed: true,
detail: { checked: this.checked, value: this.value },
}),
);
};
override render() {
const nativeType = this.type === 'radio' ? 'radio' : 'checkbox';
const wrapperClasses = classMap({
'form-check': true,
'form-switch': this.type === 'switch',
'form-check-inline': this.inline,
'form-check-reverse': this.reverse,
});
const inputClasses = classMap({
'form-check-input': true,
'is-invalid': this.invalid,
'is-valid': this.valid,
});
const id = this.id || `bs-check-${Math.random().toString(36).slice(2, 9)}`;
const hasLabel = this.label !== undefined || !this.ariaLabel;
return html`
${hasLabel
? this.label !== undefined
? html``
: html``
: nothing}
`;
}
}
defineElement('bs-form-check', BsFormCheck);
// Aliases for ergonomics.
export class BsCheckbox extends BsFormCheck {
override type: CheckType = 'checkbox';
}
defineElement('bs-checkbox', BsCheckbox);
export class BsRadio extends BsFormCheck {
override type: CheckType = 'radio';
}
defineElement('bs-radio', BsRadio);
export class BsSwitch extends BsFormCheck {
override type: CheckType = 'switch';
}
defineElement('bs-switch', BsSwitch);
declare global {
interface HTMLElementTagNameMap {
'bs-form-check': BsFormCheck;
'bs-checkbox': BsCheckbox;
'bs-radio': BsRadio;
'bs-switch': BsSwitch;
}
}