Skip to content

Commit 6b0cdb8

Browse files
authored
feat: added onBeforeClick event (#141)
- Use `onBeforeClick` to prevent the click event, as per original ms-select lib PR wenzhixin/multiple-select#610
1 parent 02c1d45 commit 6b0cdb8

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/src/MultipleSelectInstance.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,16 @@ export class MultipleSelectInstance {
769769
const selectElm = e.currentTarget;
770770
const checked = selectElm.checked;
771771
const option = findByParam(this.data, '_key', selectElm.dataset.key);
772+
const close = () => {
773+
if (this.options.single && this.options.isOpen && !this.options.keepOpen) {
774+
this.close();
775+
}
776+
};
777+
778+
if (this.options.onBeforeClick(option) === false) {
779+
close();
780+
return;
781+
}
772782

773783
this._check(option, checked);
774784
this.options.onClick(
@@ -780,9 +790,7 @@ export class MultipleSelectInstance {
780790
})
781791
);
782792

783-
if (this.options.single && this.options.isOpen && !this.options.keepOpen) {
784-
this.close();
785-
}
793+
close();
786794
}) as EventListener);
787795
}
788796

lib/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const DEFAULTS: Partial<MultipleSelectOption> = {
6666
onFocus: () => false,
6767
onBlur: () => false,
6868
onOptgroupClick: () => false,
69+
onBeforeClick: () => true,
6970
onClick: () => false,
7071
onFilter: () => false,
7172
onClear: () => false,

lib/src/interfaces/multipleSelectOption.interface.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ export interface MultipleSelectOption extends MultipleSelectLocale {
226226
/** Fires when a an optgroup label is clicked on. */
227227
onOptgroupClick: (view: MultipleSelectView) => void;
228228

229+
/** Fires before a checkbox is clicked. Return `false` to prevent the click event. */
230+
onBeforeClick: (view: MultipleSelectView) => boolean;
231+
229232
/** Fires when a checkbox is checked or unchecked. */
230233
onClick: (view: MultipleSelectView) => void;
231234

0 commit comments

Comments
 (0)