@reusable-ui/radio
Version:
A UI for the user to select single option.
19 lines (18 loc) • 1.1 kB
JavaScript
export const broadcastClearEvent = (currentRadio, selectedRadio = currentRadio) => {
const name = currentRadio.name;
const parentGroup = currentRadio.closest('form') ?? document.body;
if (parentGroup) {
// after <currentRadio> finishes rendering => un-check (clear) the other checked radio (within the same name at the same <form>):
Promise.resolve().then(() => {
for (const radio of Array.from(parentGroup.querySelectorAll('input[type="radio"]'))) {
if (radio === currentRadio)
continue; // <radio> is self => skip
if (radio.name !== name)
continue; // <radio>'s name is different to us => skip
// fire a custom `onClear` event to notify other <Radio>(s) to *uncheck*:
const customEvent = new CustomEvent('clear', { bubbles: true, detail: { selected: selectedRadio } });
radio.dispatchEvent(customEvent); // needs to bubble to support <EditableControl> => `inputValidator.handleChange()`
} // for
});
} // if
};