Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make config.trigger optional as it is not really required #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export default function MsComponent(props) {

| Option | Default | Type | Version | Description |
| -----------------| -------------------------------------------| ---------| -----------| ----------------|
| trigger | Required parameter. No default value | String or HTMLElement | | DOM selector string or HTMLElement element <br/> e.g. '#my-trigger' or document.querySelector('#my-trigger') |
| trigger | Required parameter. No default value | String or HTMLElement | | DOM selector string or HTMLElement element <br/> e.g. '#my-trigger' or document.querySelector('#my-trigger'), or left empty and call with `.show()` manually |
| wheels | Required parameter. No default value | Array | | The data displayed on the wheel |
| onChange | function(data, indexArr, instance){} | function |≥1.2.0 | A callback function after successful selected|
| onTransitionEnd | function(data, indexArr, instance){} | function |≥1.2.0 | A callback function that after each gesture sliding|
Expand Down
29 changes: 19 additions & 10 deletions src/ms-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "./style/mobile-select.less";

export default class MobileSelect {
mobileSelect!: HTMLDivElement;
trigger!: HTMLElement;
trigger?: HTMLElement;
wheelList!: HTMLCollectionOf<HTMLElement>;
sliderList!: HTMLCollectionOf<HTMLElement>;
wheelsContain!: HTMLDivElement;
Expand Down Expand Up @@ -211,7 +211,7 @@ export default class MobileSelect {
return typeof wheelsData[0]?.data?.[0] === "object";
}

static REQUIRED_PARAMS = ["trigger", "wheels"] as (keyof CustomConfig)[];
static REQUIRED_PARAMS = ["wheels"] as (keyof CustomConfig)[];

static checkRequiredConfig(config: CustomConfig): boolean {
const requiredParams = MobileSelect.REQUIRED_PARAMS;
Expand All @@ -238,6 +238,11 @@ export default class MobileSelect {

checkTriggerAvailable() {
const { config } = this;

if (!config.trigger) {
return true;
}

// @ts-ignore
this.trigger =
config.trigger instanceof HTMLElement
Expand All @@ -246,7 +251,7 @@ export default class MobileSelect {
if (!this.trigger) {
MobileSelect.log(
"error",
"trigger HTMLElement does not found on your document."
"trigger parameter must be valid dom element or selector."
);
return false;
}
Expand Down Expand Up @@ -288,7 +293,7 @@ export default class MobileSelect {
}

setTriggerInnerText(value: string) {
if (this.config.triggerDisplayValue) {
if (this.trigger && this.config.triggerDisplayValue) {
this.trigger.textContent = value;
}
}
Expand Down Expand Up @@ -389,16 +394,20 @@ export default class MobileSelect {

registerEvents(type: "add" | "remove"): void {
for (const [domName, item] of Object.entries(this.eventHandleMap)) {
const target = this[domName as keyof MobileSelect] as HTMLElement;

if (!target) continue;

if (typeof item.event === "string") {
(this[domName as keyof MobileSelect] as HTMLElement)[
`${type}EventListener`
](item.event, item.fn as EventListener, { passive: false });
target[`${type}EventListener`](item.event, item.fn as EventListener, {
passive: false
});
} else {
// 数组
item.event.forEach((eventName) => {
(this[domName as keyof MobileSelect] as HTMLElement)[
`${type}EventListener`
](eventName, item.fn as EventListener, { passive: false });
target[`${type}EventListener`](eventName, item.fn as EventListener, {
passive: false
});
});
}
}
Expand Down
Loading