Skip to content

Commit

Permalink
feat: added onBeforeClick event (#141)
Browse files Browse the repository at this point in the history
- Use `onBeforeClick` to prevent the click event, as per original ms-select lib PR wenzhixin/multiple-select#610
  • Loading branch information
ghiscoding authored Oct 27, 2023
1 parent 02c1d45 commit 6b0cdb8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/src/MultipleSelectInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,16 @@ export class MultipleSelectInstance {
const selectElm = e.currentTarget;
const checked = selectElm.checked;
const option = findByParam(this.data, '_key', selectElm.dataset.key);
const close = () => {
if (this.options.single && this.options.isOpen && !this.options.keepOpen) {
this.close();
}
};

if (this.options.onBeforeClick(option) === false) {
close();
return;
}

this._check(option, checked);
this.options.onClick(
Expand All @@ -780,9 +790,7 @@ export class MultipleSelectInstance {
})
);

if (this.options.single && this.options.isOpen && !this.options.keepOpen) {
this.close();
}
close();
}) as EventListener);
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const DEFAULTS: Partial<MultipleSelectOption> = {
onFocus: () => false,
onBlur: () => false,
onOptgroupClick: () => false,
onBeforeClick: () => true,
onClick: () => false,
onFilter: () => false,
onClear: () => false,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/interfaces/multipleSelectOption.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ export interface MultipleSelectOption extends MultipleSelectLocale {
/** Fires when a an optgroup label is clicked on. */
onOptgroupClick: (view: MultipleSelectView) => void;

/** Fires before a checkbox is clicked. Return `false` to prevent the click event. */
onBeforeClick: (view: MultipleSelectView) => boolean;

/** Fires when a checkbox is checked or unchecked. */
onClick: (view: MultipleSelectView) => void;

Expand Down

0 comments on commit 6b0cdb8

Please sign in to comment.