@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
31 lines (30 loc) • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class ToggleButtonGroup {
onCreate() {
this.state = { pressed: {} };
}
onInput(input) {
this.state.pressed = Object.fromEntries([...(input.button || [])].map(({ pressed }, i) => [i, !!pressed]));
}
handleToggle(index, { originalEvent, pressed }) {
if (this.input.variant === "radio") {
// radio buttons may not be deselected, so `pressed` is not necessary
this.state.pressed = { [index]: true };
}
else if (this.input.variant === "radio-toggle") {
this.state.pressed = { [index]: pressed };
}
else {
// act as a normal checkbox
this.state.pressed = Object.assign(Object.assign({}, this.state.pressed), { [index]: pressed });
}
this.emit("change", {
originalEvent,
pressed: Object.keys(this.state.pressed)
.filter((i) => this.state.pressed[+i])
.map((i) => +i),
});
}
}
module.exports = ToggleButtonGroup;