mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
PanelMenu: Make menu disappear on button press (#25015)
* Fix button press outside * Add a prop to toggle button press * Add comments * Remove public
This commit is contained in:
parent
84031649e3
commit
5f4526ca64
@ -2,7 +2,14 @@ import { PureComponent } from 'react';
|
|||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
|
/**
|
||||||
|
* When clicking outside of current element
|
||||||
|
*/
|
||||||
onClick: () => void;
|
onClick: () => void;
|
||||||
|
/**
|
||||||
|
* Runs the 'onClick' function when pressing a key outside of the current element. Defaults to true.
|
||||||
|
*/
|
||||||
|
includeButtonPress: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
@ -10,16 +17,26 @@ interface State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class ClickOutsideWrapper extends PureComponent<Props, State> {
|
export class ClickOutsideWrapper extends PureComponent<Props, State> {
|
||||||
|
static defaultProps = {
|
||||||
|
includeButtonPress: true,
|
||||||
|
};
|
||||||
state = {
|
state = {
|
||||||
hasEventListener: false,
|
hasEventListener: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
window.addEventListener('click', this.onOutsideClick, false);
|
window.addEventListener('click', this.onOutsideClick, false);
|
||||||
|
if (this.props.includeButtonPress) {
|
||||||
|
// Use keyup since keydown already has an eventlistener on window
|
||||||
|
window.addEventListener('keyup', this.onOutsideClick, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
window.removeEventListener('click', this.onOutsideClick, false);
|
window.removeEventListener('click', this.onOutsideClick, false);
|
||||||
|
if (this.props.includeButtonPress) {
|
||||||
|
window.addEventListener('keyup', this.onOutsideClick, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onOutsideClick = (event: any) => {
|
onOutsideClick = (event: any) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user