Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
74741247
Commit
74741247
authored
8 years ago
by
Tom Robinson
Browse files
Options
Downloads
Patches
Plain Diff
Implement onclickout functionality directly in our OnClickOutsideWrapper. Resolves #3023
parent
35d9642a
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/metabase/components/OnClickOutsideWrapper.jsx
+38
-30
38 additions, 30 deletions
frontend/src/metabase/components/OnClickOutsideWrapper.jsx
with
38 additions
and
30 deletions
frontend/src/metabase/components/OnClickOutsideWrapper.jsx
+
38
−
30
View file @
74741247
import
React
,
{
Component
,
PropTypes
}
from
"
react
"
;
import
ReactDOM
from
"
react-dom
"
;
import
ClickOutComponent
from
'
react-onclickout
'
;
// this feels a little silly, but we have this component ONLY so that we can add the OnClickOutside functionality on an
// arbitrary set of html content. I wish we could do that more easily
// keep track of the order popovers were opened so we only close the last one when clicked outside
var
popoverStack
=
[];
const
popoverStack
=
[];
const
ESC_KEY
=
27
;
export
default
class
OnClickOutsideWrapper
extends
ClickOut
Component
{
export
default
class
OnClickOutsideWrapper
extends
Component
{
static
propTypes
=
{
handleDismissal
:
PropTypes
.
func
.
isRequired
}
};
static
defaultProps
=
{
dismissOnClickOutside
:
true
,
dismissOnEscape
:
true
};
constructor
()
{
super
();
this
.
handleKeyPress
=
this
.
handleKeyPress
.
bind
(
this
);
}
componentDidMount
()
{
super
.
componentDidMount
();
// necessary to ignore click events that fire immediately, causing modals/popovers to close prematurely
this
.
timeout
=
setTimeout
(()
=>
{
this
.
_
timeout
=
setTimeout
(()
=>
{
popoverStack
.
push
(
this
);
// HACK: set the z-index of the parent element to ensure it's always on top
// NOTE: this actually doesn't seem to be working correctly for popovers since PopoverBody creates a stacking context
ReactDOM
.
findDOMNode
(
this
).
parentNode
.
style
.
zIndex
=
popoverStack
.
length
+
2
;
// HACK: add 2 to ensure it's in front of main and nav elements
},
10
);
document
.
addEventListener
(
'
keydown
'
,
this
.
handleKeyPress
,
false
)
}
// HACK: set the z-index of the parent element to ensure it"s always on top
// NOTE: this actually doesn"t seem to be working correctly for popovers since PopoverBody creates a stacking context
ReactDOM
.
findDOMNode
(
this
).
parentNode
.
style
.
zIndex
=
popoverStack
.
length
+
2
;
// HACK: add 2 to ensure it"s in front of main and nav elements
handleKeyPress
(
event
)
{
if
(
event
.
keyCode
===
ESC_KEY
)
{
event
.
preventDefault
();
this
.
onClickOut
();
}
if
(
this
.
props
.
dismissOnEscape
)
{
document
.
addEventListener
(
"
keydown
"
,
this
.
_handleKeyPress
,
false
);
}
if
(
this
.
props
.
dismissOnClickOutside
)
{
window
.
addEventListener
(
"
click
"
,
this
.
_handleClick
,
false
);
}
},
0
);
}
componentWillUnmount
()
{
super
.
componentWillUnmount
();
document
.
removeEventListener
(
'
keydown
'
,
this
.
handleKeyPress
,
false
);
document
.
removeEventListener
(
"
keydown
"
,
this
.
_handleKeyPress
,
false
);
window
.
removeEventListener
(
"
click
"
,
this
.
_handleClick
,
false
);
clearTimeout
(
this
.
_timeout
);
// remove popover from the stack
var
index
=
popoverStack
.
indexOf
(
this
);
if
(
index
>=
0
)
{
popoverStack
.
splice
(
index
,
1
);
}
clearTimeout
(
this
.
timeout
);
}
onClickOut
(
e
)
{
_handleClick
=
(
e
)
=>
{
if
(
!
ReactDOM
.
findDOMNode
(
this
).
contains
(
e
.
target
))
{
this
.
_handleDismissal
();
}
}
_handleKeyPress
=
(
event
)
=>
{
if
(
event
.
keyCode
===
ESC_KEY
)
{
event
.
preventDefault
();
this
.
_handleDismissal
();
}
}
_handleDismissal
(
e
)
{
// only propagate event for the popover on top of the stack
if
(
this
===
popoverStack
[
popoverStack
.
length
-
1
])
{
this
.
props
.
handleDismissal
(
e
);
...
...
@@ -59,6 +67,6 @@ export default class OnClickOutsideWrapper extends ClickOutComponent {
}
render
()
{
return
this
.
props
.
children
;
return
React
.
Children
.
only
(
this
.
props
.
children
)
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment