forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI Framework] Add KuiOutsideClickDetector (elastic#13521)
* Add KuiOutsideClickDetector. * Convert KuiColorPicker and KuiPopover to use KuiOutsideClickDetector.
- Loading branch information
1 parent
f0a3811
commit 0c7cec4
Showing
7 changed files
with
134 additions
and
112 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
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
3 changes: 3 additions & 0 deletions
3
...k/src/components/outside_click_detector/__snapshots__/outside_click_detector.test.js.snap
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,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`KuiOutsideClickDetector is rendered 1`] = `<div />`; |
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,3 @@ | ||
export { | ||
KuiOutsideClickDetector, | ||
} from './outside_click_detector'; |
48 changes: 48 additions & 0 deletions
48
ui_framework/src/components/outside_click_detector/outside_click_detector.js
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,48 @@ | ||
import { | ||
Children, | ||
cloneElement, | ||
Component, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export class KuiOutsideClickDetector extends Component { | ||
static propTypes = { | ||
children: PropTypes.node.isRequired, | ||
onOutsideClick: PropTypes.func.isRequired, | ||
} | ||
|
||
onClickOutside = event => { | ||
if (!this.wrapperRef) { | ||
return; | ||
} | ||
|
||
if (this.wrapperRef === event.target) { | ||
return; | ||
} | ||
|
||
if (this.wrapperRef.contains(event.target)) { | ||
return; | ||
} | ||
|
||
this.props.onOutsideClick(); | ||
} | ||
|
||
componentDidMount() { | ||
document.addEventListener('click', this.onClickOutside); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener('click', this.onClickOutside); | ||
} | ||
|
||
render() { | ||
const props = Object.assign({}, this.props.children.props, { | ||
ref: node => { | ||
this.wrapperRef = node; | ||
}, | ||
}); | ||
|
||
const child = Children.only(this.props.children); | ||
return cloneElement(child, props); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
ui_framework/src/components/outside_click_detector/outside_click_detector.test.js
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,17 @@ | ||
import React from 'react'; | ||
import { render } from 'enzyme'; | ||
|
||
import { KuiOutsideClickDetector } from './outside_click_detector'; | ||
|
||
describe('KuiOutsideClickDetector', () => { | ||
test('is rendered', () => { | ||
const component = render( | ||
<KuiOutsideClickDetector onOutsideClick={() => {}}> | ||
<div /> | ||
</KuiOutsideClickDetector> | ||
); | ||
|
||
expect(component) | ||
.toMatchSnapshot(); | ||
}); | ||
}); |
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