Skip to content

Commit 7e9132d

Browse files
committed
Modified examples/demos/demo1.js
Modified package.json Modified src/index.tsx
1 parent 7385b0a commit 7e9132d

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

examples/demos/demo1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export default class DEMO extends Component {
8686
<TriggerButton
8787
placement="bottomLeft"
8888
action="click"
89+
// hideAction="mouseDown"
8990
text="action:click"
9091
popupTransition={{ classNames: animateClassNames, timeout: 300 }}
9192
/>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-widget-trigger",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "trigger",
55
"main": "cjs/index.js",
66
"module": "esm/index.js",

src/index.tsx

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ const isMobile =
1313
typeof navigator !== "undefined" &&
1414
!!navigator.userAgent.match(/(Android|iPhone|iPad|iPod|iOS|UCWEB)/i);
1515

16-
type ActionType = "click" | "contextMenu" | "focus" | "hover" | "mouseDown";
17-
type ShowActionType = "click" | "contextMenu" | "focus" | "mouseEnter" | "mouseDown";
18-
type HideActionType = "click" | "mouseLeave" | "blur" | "resize" | "scroll" | "mouseDown";
16+
type ActionType = "click" | "contextMenu" | "focus" | "hover" | "mouseDown" | "mouseUp";
17+
type ShowActionType = "click" | "contextMenu" | "focus" | "mouseEnter" | "mouseDown" | "mouseUp";
18+
type HideActionType =
19+
| "click"
20+
| "mouseLeave"
21+
| "blur"
22+
| "resize"
23+
| "scroll"
24+
| "mouseDown"
25+
| "mouseUp";
1926
type Delay = {
2027
show?: number;
2128
hide?: number;
@@ -53,8 +60,10 @@ export interface TriggerProps {
5360
/** 触发事件 */
5461
action?: ActionType | ActionType[] | null;
5562
/** 显示触发事件,同action合并 */
63+
// TODO: 只针对triggerNode?
5664
showAction?: ShowActionType | ShowActionType[] | null;
5765
/** 隐藏触发事件,同action合并 */
66+
// TODO: 只针对Outside?
5867
hideAction?: HideActionType | HideActionType[] | null;
5968
/** 显示/隐藏延迟时间 */
6069
delay?: number | Delay;
@@ -204,15 +213,20 @@ export class Trigger extends React.Component<TriggerProps, TriggerState> {
204213

205214
if (
206215
!this.clickOutsideHandler &&
207-
(this.isMouseDownToHide() || this.isClickToHide() || this.isContextMenuToShow())
216+
(this.isMouseDownToHide() ||
217+
this.isMouseUpToHide() ||
218+
this.isClickToHide() ||
219+
this.isContextMenuToShow())
208220
) {
209-
this.clickOutsideHandler = listen(currentDocument, "mousedown", (e) => {
210-
this.onDocumentClick(e);
211-
});
221+
this.clickOutsideHandler = listen(
222+
currentDocument,
223+
"mousedown",
224+
this.onOutsideClick
225+
);
212226
}
213227

214228
if (!this.touchOutsideHandler && isMobile) {
215-
this.touchOutsideHandler = listen(currentDocument, "click", this.onDocumentClick);
229+
this.touchOutsideHandler = listen(currentDocument, "click", this.onOutsideClick);
216230
}
217231

218232
// close popup when trigger type contains 'onContextMenu' and document is scrolling.
@@ -230,7 +244,7 @@ export class Trigger extends React.Component<TriggerProps, TriggerState> {
230244
}
231245

232246
if (!this.windowScrollHandler && this.isWindowScrollToHide()) {
233-
this.windowScrollHandler = listen(currentDocument, "scroll", this.onDocumentClick);
247+
this.windowScrollHandler = listen(currentDocument, "scroll", this.onOutsideClick);
234248
}
235249

236250
if (!this.windowResizeHandler && this.isWindowResizeToHide()) {
@@ -254,7 +268,7 @@ export class Trigger extends React.Component<TriggerProps, TriggerState> {
254268
return findDOMNode(this) as HTMLElement;
255269
}
256270

257-
protected onDocumentClick = (event: MouseEvent) => {
271+
protected onOutsideClick = (event: MouseEvent) => {
258272
const target = event.target as Element;
259273
const triggerNode = this.getTriggerNode();
260274
const popupRootNode = this.popupInstance.getRootDOM();
@@ -396,6 +410,14 @@ export class Trigger extends React.Component<TriggerProps, TriggerState> {
396410
return this.checkToHide(["mouseDown"]);
397411
}
398412

413+
protected isMouseUpToShow() {
414+
return this.checkToShow(["mouseUp"]);
415+
}
416+
417+
protected isMouseUpToHide() {
418+
return this.checkToHide(["mouseUp"]);
419+
}
420+
399421
protected isClickToShow() {
400422
return this.checkToShow(["click"]);
401423
}
@@ -452,6 +474,14 @@ export class Trigger extends React.Component<TriggerProps, TriggerState> {
452474
}
453475
}
454476

477+
protected onTriggerMouseUp(e: React.MouseEvent) {
478+
const nextVisible = !this.state.popupVisible;
479+
480+
if ((this.isMouseUpToHide() && !nextVisible) || (nextVisible && this.isMouseUpToShow())) {
481+
this.delaySetPopupVisible(nextVisible);
482+
}
483+
}
484+
455485
protected onMouseEnter = (e: React.MouseEvent) => {
456486
this.delaySetPopupVisible(true);
457487
};
@@ -672,6 +702,20 @@ export class Trigger extends React.Component<TriggerProps, TriggerState> {
672702
};
673703
}
674704

705+
if (this.isMouseUpToShow() || this.isMouseUpToHide()) {
706+
newChildProps.onMouseUp = (e) => {
707+
if (child.props.onMouseUp) {
708+
child.props.onMouseUp(e);
709+
}
710+
711+
if (checkDefaultPrevented && e.defaultPrevented) return;
712+
713+
this.clearDelayTimer();
714+
715+
this.onTriggerMouseUp(e);
716+
};
717+
}
718+
675719
if (this.isClickToHide() || this.isClickToShow()) {
676720
newChildProps.onClick = (e) => {
677721
if (child.props.onClick) {

0 commit comments

Comments
 (0)