Skip to content

Commit 0a56a31

Browse files
committed
Simplify initializer
1 parent 21d0056 commit 0a56a31

File tree

6 files changed

+41
-26
lines changed

6 files changed

+41
-26
lines changed

dist/radioBox.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ interface RadioBoxOption {
1111
bindLabel?: boolean;
1212
styles?: object;
1313
}
14+
interface RadioInputElement extends HTMLInputElement {
15+
withID: boolean;
16+
radioBoxChange?: EventListener;
17+
labelToRestore?: HTMLLabelElement;
18+
}
1419

1520
declare class RadioBox {
1621
private static instances;
@@ -33,14 +38,19 @@ declare class RadioBox {
3338
private radioBoxChange;
3439
private destroy;
3540
set onChange(callback: OnChangeCallback);
36-
empty(): RadioBox;
37-
refresh(): void;
38-
static destroyAll(): void;
41+
/**
42+
* Get all radio box elements
43+
* @return {RadioInputElement[]} All radio box elements
44+
*/
45+
get elements(): RadioInputElement[];
3946
/**
4047
* Get value of the checked radio box
4148
* @return {string} Value of the checked radio box
4249
*/
4350
get value(): string | null;
51+
empty(): RadioBox;
52+
refresh(): void;
53+
static destroyAll(): void;
4454
}
4555

4656
export { RadioBox as default };

dist/radioBox.esm.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class Utils {
205205
labelToRestore = labelSibling.cloneNode(true);
206206
// Prefer the explicitly set title, fall back to text from the label.
207207
title = title || labelSibling.textContent;
208+
// Remove the original label
209+
labelSibling.parentNode.removeChild(labelSibling);
208210
}
209211
}
210212
return { title, remainLabel, randomID, labelToRestore };
@@ -226,6 +228,8 @@ class Utils {
226228
if (labelNode.parentNode) {
227229
labelNode.parentNode.insertBefore(cloneEle, labelNode);
228230
}
231+
// Replace the original element with the new one
232+
ele.parentNode.replaceChild(templateNode.firstElementChild || templateNode, ele);
229233
return { cloneEle, templateNode, labelNode };
230234
}
231235
static insertRadioboxTitle(title, bindLabel, labelNode, cloneEle) {
@@ -327,7 +331,7 @@ styleInject(css_248z);
327331

328332
class RadioBox {
329333
static instances = [];
330-
static version = '2.0.1';
334+
static version = '2.0.2';
331335
static firstLoad = true;
332336
element = null;
333337
options;
@@ -393,18 +397,13 @@ class RadioBox {
393397
let bindLabel = this.options.bindLabel ?? false;
394398
let { title, remainLabel, randomID, labelToRestore } = Utils.handleRadioboxTitle(ele, labelSibling);
395399
bindLabel = remainLabel ? true : bindLabel;
396-
if (title && labelSibling && labelSibling.tagName === 'LABEL') {
397-
title = labelSibling.textContent || title;
398-
labelSibling.parentNode?.removeChild(labelSibling);
399-
}
400400
// Handle radiobox checked status
401401
if (this.options.checked) {
402402
// Initialize radiobox checked status based on options
403403
this.updateRadioboxCheckedStatus(ele, index);
404404
}
405405
// Insert radiobox
406-
let { cloneEle, templateNode, labelNode } = Utils.insertRadiobox(this.id.toString(), ele, randomID, remainLabel);
407-
ele.parentNode?.replaceChild(templateNode.firstElementChild || templateNode, ele);
406+
let { cloneEle, labelNode } = Utils.insertRadiobox(this.id.toString(), ele, randomID, remainLabel);
408407
// Insert radiobox title
409408
Utils.insertRadioboxTitle(title, bindLabel, labelNode, cloneEle);
410409
// Add event listener
@@ -460,6 +459,21 @@ class RadioBox {
460459
set onChange(callback) {
461460
this.onChangeCallback = callback;
462461
}
462+
/**
463+
* Get all radio box elements
464+
* @return {RadioInputElement[]} All radio box elements
465+
*/
466+
get elements() {
467+
return this.allElement;
468+
}
469+
/**
470+
* Get value of the checked radio box
471+
* @return {string} Value of the checked radio box
472+
*/
473+
get value() {
474+
let checkedRadio = this.allElement.find(element => element.checked);
475+
return checkedRadio ? checkedRadio.value : null;
476+
}
463477
empty() {
464478
this.allElement.forEach(element => {
465479
element.checked = false;
@@ -481,14 +495,6 @@ class RadioBox {
481495
instance.destroy();
482496
}
483497
}
484-
/**
485-
* Get value of the checked radio box
486-
* @return {string} Value of the checked radio box
487-
*/
488-
get value() {
489-
let checkedRadio = this.allElement.find(element => element.checked);
490-
return checkedRadio ? checkedRadio.value : null;
491-
}
492498
}
493499

494500
export { RadioBox as default };

dist/radioBox.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@carry0987/radio-box",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "A library for create radio type check box",
55
"type": "module",
66
"main": "dist/radioBox.min.js",

src/module/utils-ext.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class Utils {
6565
labelToRestore = labelSibling.cloneNode(true) as HTMLLabelElement;
6666
// Prefer the explicitly set title, fall back to text from the label.
6767
title = title || labelSibling.textContent;
68+
// Remove the original label
69+
labelSibling.parentNode!.removeChild(labelSibling);
6870
}
6971
}
7072

@@ -93,6 +95,8 @@ class Utils {
9395
if (labelNode.parentNode) {
9496
labelNode.parentNode.insertBefore(cloneEle, labelNode);
9597
}
98+
// Replace the original element with the new one
99+
ele.parentNode!.replaceChild(templateNode.firstElementChild || templateNode, ele);
96100

97101
return {cloneEle, templateNode, labelNode};
98102
}

0 commit comments

Comments
 (0)