chore(radio): implemented destroy
This commit is contained in:
parent
b1c662da88
commit
da8894a708
@ -9,39 +9,51 @@ const RADIO_INITIALIZED_CLASS = 'radio--initialized';
|
||||
})
|
||||
export class Radio {
|
||||
|
||||
_element;
|
||||
_wrapperEl;
|
||||
_labelEl;
|
||||
|
||||
constructor(element) {
|
||||
if (!element) {
|
||||
throw new Error('Radio utility cannot be setup without an element!');
|
||||
}
|
||||
|
||||
if (element.closest('.radio-group')) {
|
||||
this._element = element;
|
||||
|
||||
if (this._element.closest('.radio-group')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (element.classList.contains(RADIO_INITIALIZED_CLASS)) {
|
||||
if (this._element.classList.contains(RADIO_INITIALIZED_CLASS)) {
|
||||
// throw new Error('Radio utility already initialized!');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (element.parentElement.classList.contains(RADIO_CLASS)) {
|
||||
if (this._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 siblingEl = this._element.nextSibling;
|
||||
const parentEl = this._element.parentElement;
|
||||
|
||||
const wrapperEl = document.createElement('div');
|
||||
wrapperEl.classList.add(RADIO_CLASS);
|
||||
this._wrapperEl = document.createElement('div');
|
||||
this._wrapperEl.classList.add(RADIO_CLASS);
|
||||
|
||||
const labelEl = document.createElement('label');
|
||||
labelEl.setAttribute('for', element.id);
|
||||
this._labelEl = document.createElement('label');
|
||||
this._labelEl.setAttribute('for', this._element.id);
|
||||
|
||||
wrapperEl.appendChild(element);
|
||||
wrapperEl.appendChild(labelEl);
|
||||
this._wrapperEl.appendChild(this._element);
|
||||
this._wrapperEl.appendChild(this._labelEl);
|
||||
|
||||
parentEl.insertBefore(wrapperEl, siblingEl);
|
||||
parentEl.insertBefore(this._wrapperEl, siblingEl);
|
||||
|
||||
element.classList.add(RADIO_INITIALIZED_CLASS);
|
||||
this._element.classList.add(RADIO_INITIALIZED_CLASS);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this._labelEl.remove();
|
||||
this._wrapperEl.remove();
|
||||
this._element.classList.remove(RADIO_INITIALIZED_CLASS);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user