48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
import { Utility } from '../../core/utility';
|
|
import './radio.sass';
|
|
|
|
const RADIO_CLASS = 'radiobox';
|
|
const RADIO_INITIALIZED_CLASS = 'radio--initialized';
|
|
|
|
@Utility({
|
|
selector: 'input[type="radio"]:not([uw-no-radio])',
|
|
})
|
|
export class Radio {
|
|
|
|
constructor(element) {
|
|
if (!element) {
|
|
throw new Error('Radio utility cannot be setup without an element!');
|
|
}
|
|
|
|
if (element.closest('.radio-group')) {
|
|
return false;
|
|
}
|
|
|
|
if (element.classList.contains(RADIO_INITIALIZED_CLASS)) {
|
|
// throw new Error('Radio utility already initialized!');
|
|
return false;
|
|
}
|
|
|
|
if (element.parentElement.classList.contains(RADIO_CLASS)) {
|
|
// throw new Error('Radio element\'s wrapper already has class '' + RADIO_CLASS + ''!');
|
|
return false;
|
|
}
|
|
|
|
const siblingEl = element.nextSibling;
|
|
const parentEl = element.parentElement;
|
|
|
|
const wrapperEl = document.createElement('div');
|
|
wrapperEl.classList.add(RADIO_CLASS);
|
|
|
|
const labelEl = document.createElement('label');
|
|
labelEl.setAttribute('for', element.id);
|
|
|
|
wrapperEl.appendChild(element);
|
|
wrapperEl.appendChild(labelEl);
|
|
|
|
parentEl.insertBefore(wrapperEl, siblingEl);
|
|
|
|
element.classList.add(RADIO_INITIALIZED_CLASS);
|
|
}
|
|
}
|