Hover 模式优化, 移除 2s 自动关闭

This commit is contained in:
2betop 2020-12-22 11:02:32 +08:00
parent 80e58e7f76
commit f899849ca5

View File

@ -91,12 +91,15 @@ export const HocPopOver = (config: Partial<SchemaPopOverObject> = {}) => (
let lastOpenedInstance: PopOverComponent | null = null; let lastOpenedInstance: PopOverComponent | null = null;
class PopOverComponent extends React.Component<PopOverProps, PopOverState> { class PopOverComponent extends React.Component<PopOverProps, PopOverState> {
target: HTMLElement; target: HTMLElement;
timer: NodeJS.Timeout;
static ComposedComponent = Component; static ComposedComponent = Component;
constructor(props: PopOverProps) { constructor(props: PopOverProps) {
super(props); super(props);
this.openPopOver = this.openPopOver.bind(this); this.openPopOver = this.openPopOver.bind(this);
this.closePopOver = this.closePopOver.bind(this); this.closePopOver = this.closePopOver.bind(this);
this.closePopOverLater = this.closePopOverLater.bind(this);
this.clearCloseTimer = this.clearCloseTimer.bind(this);
this.targetRef = this.targetRef.bind(this); this.targetRef = this.targetRef.bind(this);
// this.handleClickOutside = this.handleClickOutside.bind(this); // this.handleClickOutside = this.handleClickOutside.bind(this);
this.state = { this.state = {
@ -121,6 +124,7 @@ export const HocPopOver = (config: Partial<SchemaPopOverObject> = {}) => (
} }
closePopOver() { closePopOver() {
clearTimeout(this.timer);
if (!this.state.isOpened) { if (!this.state.isOpened) {
return; return;
} }
@ -135,6 +139,15 @@ export const HocPopOver = (config: Partial<SchemaPopOverObject> = {}) => (
); );
} }
closePopOverLater() {
// 5s 后自动关闭。
this.timer = setTimeout(this.closePopOver, 2000);
}
clearCloseTimer() {
clearTimeout(this.timer);
}
buildSchema() { buildSchema() {
const {popOver, name, label, translate: __} = this.props; const {popOver, name, label, translate: __} = this.props;
@ -215,6 +228,11 @@ export const HocPopOver = (config: Partial<SchemaPopOverObject> = {}) => (
? this.closePopOver ? this.closePopOver
: undefined : undefined
} }
onMouseEnter={
(popOver as SchemaPopOverObject)?.trigger === 'hover'
? this.clearCloseTimer
: undefined
}
> >
{content} {content}
</div> </div>
@ -237,6 +255,11 @@ export const HocPopOver = (config: Partial<SchemaPopOverObject> = {}) => (
? this.closePopOver ? this.closePopOver
: undefined : undefined
} }
onMouseEnter={
(popOver as SchemaPopOverObject)?.trigger === 'hover'
? this.clearCloseTimer
: undefined
}
> >
{content} {content}
</PopOver> </PopOver>
@ -262,6 +285,7 @@ export const HocPopOver = (config: Partial<SchemaPopOverObject> = {}) => (
const trigger = (popOver as SchemaPopOverObject)?.trigger; const trigger = (popOver as SchemaPopOverObject)?.trigger;
if (trigger === 'hover') { if (trigger === 'hover') {
triggerProps.onMouseEnter = this.openPopOver; triggerProps.onMouseEnter = this.openPopOver;
triggerProps.onMouseLeave = this.closePopOverLater;
} else { } else {
triggerProps.onClick = this.openPopOver; triggerProps.onClick = this.openPopOver;
} }