-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract QueryView Execute button to a component and fix tooltip issues
- Loading branch information
1 parent
3f6d887
commit c52bd2c
Showing
2 changed files
with
67 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
client/app/pages/queries/components/QueryViewExecuteButton.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { useState, useMemo, useEffect } from "react"; | ||
import PropTypes from "prop-types"; | ||
import Button from "antd/lib/button"; | ||
import Tooltip from "antd/lib/tooltip"; | ||
import { KeyboardShortcuts, humanReadableShortcut } from "@/services/keyboard-shortcuts"; | ||
|
||
export default function QueryViewExecuteButton({ shortcut, disabled, children, onClick }) { | ||
const [tooltipVisible, setTooltipVisible] = useState(false); | ||
|
||
const eventHandlers = useMemo( | ||
() => ({ | ||
onMouseEnter: () => setTooltipVisible(true), | ||
onMouseLeave: () => setTooltipVisible(false), | ||
}), | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
if (disabled) { | ||
setTooltipVisible(false); | ||
} | ||
}, [disabled]); | ||
|
||
useEffect(() => { | ||
if (shortcut) { | ||
const shortcuts = { | ||
[shortcut]: onClick, | ||
}; | ||
|
||
KeyboardShortcuts.bind(shortcuts); | ||
return () => { | ||
KeyboardShortcuts.unbind(shortcuts); | ||
}; | ||
} | ||
}, [shortcut, onClick]); | ||
|
||
return ( | ||
<Tooltip placement="top" title={humanReadableShortcut(shortcut, 1)} visible={tooltipVisible}> | ||
<span {...eventHandlers}> | ||
<Button type="primary" disabled={disabled} onClick={onClick} style={disabled ? { pointerEvents: "none" } : {}}> | ||
{children} | ||
</Button> | ||
</span> | ||
</Tooltip> | ||
); | ||
} | ||
|
||
QueryViewExecuteButton.propTypes = { | ||
shortcut: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
children: PropTypes.node, | ||
onClick: PropTypes.func, | ||
}; | ||
|
||
QueryViewExecuteButton.defaultProps = { | ||
shortcut: null, | ||
disabled: false, | ||
children: null, | ||
onClick: () => {}, | ||
}; |