Skip to content

Commit 4d3ea4e

Browse files
authored
fix: simplify more code and avoid reusing code (DRY) (#368)
* fix: simplify more code and avoid reusing code (DRY)
1 parent 04f13c8 commit 4d3ea4e

File tree

5 files changed

+24
-26
lines changed

5 files changed

+24
-26
lines changed

packages/multiple-select-vanilla/src/MultipleSelectInstance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class MultipleSelectInstance {
180180
}
181181

182182
// single or multiple
183-
if (typeof this.options.single === 'undefined') {
183+
if (this.options.single === undefined) {
184184
this.options.single = !this.elm.multiple;
185185
}
186186

packages/multiple-select-vanilla/src/constants.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import English from './locales/multiple-select-en-US.js';
55
const BLOCK_ROWS = 50;
66
const CLUSTER_BLOCKS = 4;
77

8+
const noopFalse = () => false;
9+
const noopTrue = () => true;
810
const DEFAULTS: Partial<MultipleSelectOption> = {
911
name: '',
1012
placeholder: '',
@@ -64,23 +66,23 @@ const DEFAULTS: Partial<MultipleSelectOption> = {
6466
textTemplate: (elm: HTMLOptionElement) => elm.innerHTML.trim(),
6567
labelTemplate: (elm: HTMLOptionElement) => elm.label,
6668

67-
onBeforeOpen: () => false,
68-
onOpen: () => false,
69-
onClose: () => false,
70-
onCheckAll: () => false,
71-
onUncheckAll: () => false,
72-
onFocus: () => false,
73-
onBlur: () => false,
74-
onOptgroupClick: () => false,
75-
onBeforeClick: () => true,
76-
onClick: () => false,
77-
onFilter: () => false,
78-
onFilterClear: () => false,
79-
onClear: () => false,
80-
onAfterCreate: () => false,
81-
onDestroy: () => false,
82-
onAfterDestroy: () => false,
83-
onDestroyed: () => false,
69+
onBeforeOpen: noopFalse,
70+
onOpen: noopFalse,
71+
onClose: noopFalse,
72+
onCheckAll: noopFalse,
73+
onUncheckAll: noopFalse,
74+
onFocus: noopFalse,
75+
onBlur: noopFalse,
76+
onOptgroupClick: noopFalse,
77+
onBeforeClick: noopTrue,
78+
onClick: noopFalse,
79+
onFilter: noopFalse,
80+
onFilterClear: noopFalse,
81+
onClear: noopFalse,
82+
onAfterCreate: noopFalse,
83+
onDestroy: noopFalse,
84+
onAfterDestroy: noopFalse,
85+
onDestroyed: noopFalse,
8486
};
8587

8688
const METHODS = [

packages/multiple-select-vanilla/src/multiple-select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ multipleSelect.locales = { ...English } as MultipleSelectLocales; // load Englis
6666
multipleSelect.methods = Constants.METHODS;
6767

6868
// add it to the window object so it can be used as standalone
69-
if (typeof window !== 'undefined') {
69+
if (window !== undefined) {
7070
window.multipleSelect = multipleSelect;
7171
}

packages/multiple-select-vanilla/src/services/binding-event.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class BindingEventService {
5656
) {
5757
if (elementOrElements) {
5858
const elements = Array.isArray(elementOrElements) ? elementOrElements : [elementOrElements];
59-
const eventNames = Array.isArray(eventNameOrNames) ? eventNameOrNames || '' : [eventNameOrNames || ''];
59+
const eventNames = Array.isArray(eventNameOrNames) ? eventNameOrNames : [eventNameOrNames || ''];
6060

6161
for (const element of elements) {
6262
if (!listener) {
@@ -94,8 +94,7 @@ export class BindingEventService {
9494
} else {
9595
// unbind everything
9696
while (this._boundedEvents.length > 0) {
97-
const boundedEvent = this._boundedEvents.pop() as ElementEventListener;
98-
const { element, eventName, listener } = boundedEvent;
97+
const { element, eventName, listener } = this._boundedEvents.pop() as ElementEventListener;
9998
this.unbind(element, eventName, listener);
10099
}
101100
}

packages/multiple-select-vanilla/src/services/virtual-scroll.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ export class VirtualScroll {
111111
protected getNum() {
112112
this.scrollTop = this.scrollEl.scrollTop;
113113
const blockSize = (this.clusterHeight || 0) - (this.blockHeight || 0);
114-
if (blockSize) {
115-
return Math.floor(this.scrollTop / blockSize) || 0;
116-
}
117-
return 0;
114+
return blockSize ? Math.floor(this.scrollTop / blockSize) || 0 : 0;
118115
}
119116

120117
protected initData(rows: HtmlStruct[], num: number) {

0 commit comments

Comments
 (0)