Skip to content

Commit e936663

Browse files
author
PeAd
committed
Happy needs to only initalise global callbacks ONCE or bind them to the actually inserted elements. I chose to, for the sake of least changes, only add the callback once globally.
Moreover, the happy-checkbox selector selects the new fancy checkbox, not the native checkbox like the code assumes. Also swaps the order of operations so any callback bound on the native input which may want to change/check the state of the happy checkbox is in a consistent, new state.
1 parent 9c2a997 commit e936663

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

assets/integrations/happy.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ export class Happy {
1717
};
1818

1919
init() {
20-
if(!this.sheet){
21-
const styleElement = new CSSStyleSheet();
22-
styleElement.replaceSync(happyStyles);
23-
document.adoptedStyleSheets.push(styleElement);
24-
this.sheet = styleElement;
20+
21+
if(!this.sheet) {
22+
this.firstInit()
2523
}
2624
this.removeBySelector(".happy-radio");
2725
this.removeBySelector(".happy-checkbox");
@@ -30,6 +28,24 @@ export class Happy {
3028
this.initCheckbox();
3129
}
3230

31+
firstInit(){
32+
const styleElement = new CSSStyleSheet();
33+
styleElement.replaceSync(happyStyles);
34+
document.adoptedStyleSheets.push(styleElement);
35+
this.sheet = styleElement;
36+
37+
/**
38+
* The first init call, adds handlers for fancy looking checkboxes. The callback neds only be added once.
39+
*/
40+
document.addEventListener("click", this.checkCheckboxStateOnClick.bind(this));
41+
document.addEventListener("change", this.checkCheckboxStateOnChange.bind(this));
42+
43+
/**
44+
* Set aciton functionality for native change of radios
45+
*/
46+
document.addEventListener("change", this.radioOnChange.bind(this));
47+
}
48+
3349
/**
3450
* @deprecated
3551
*/
@@ -95,15 +111,11 @@ export class Happy {
95111
* Init state
96112
*/
97113
this.checkRadioState(input);
98-
99-
/**
100-
* Set aciton functionality for native change
101-
*/
102-
document.addEventListener("change", this.radioOnChange.bind(this));
103114
});
104115
}
105116

106117
initCheckbox() {
118+
107119
document.querySelectorAll<HTMLInputElement>("input[type=checkbox].happy").forEach(input => {
108120
/**
109121
* Paste happy component into html
@@ -128,12 +140,6 @@ export class Happy {
128140
* Init state
129141
*/
130142
this.checkCheckboxState(input);
131-
132-
/**
133-
* Set action functionality for click || native change
134-
*/
135-
document.addEventListener("click", this.checkCheckboxStateOnClick.bind(this));
136-
document.addEventListener("change", this.checkCheckboxStateOnChange.bind(this));
137143
});
138144
}
139145

@@ -149,7 +155,6 @@ export class Happy {
149155
: target instanceof SVGGraphicsElement
150156
? target.closest("svg")?.parentNode
151157
: target;
152-
153158
if (!(happyInput instanceof HTMLElement) || !happyInput.classList.contains("happy-checkbox")) {
154159
return;
155160
}
@@ -160,14 +165,13 @@ export class Happy {
160165
const value = happyInput.getAttribute("data-value");
161166

162167
const input = document.querySelector(
163-
`.happy-checkbox[data-name="${name}"]` + (!!value ? `[value="${value}"]` : "")
168+
`input[type=checkbox].happy[name="${name}"]` + (!!value ? `[value="${value}"]` : "")
164169
);
165170
if (!(input instanceof HTMLInputElement)) return;
166171

167172
const checked = happyInput.classList.contains("active");
168-
169-
input.checked = !checked;
170173
checked ? happyInput.classList.remove("active") : happyInput.classList.add("active");
174+
input.checked = !checked;
171175
}
172176

173177
checkCheckboxStateOnChange({target}: Event) {

0 commit comments

Comments
 (0)