-
Notifications
You must be signed in to change notification settings - Fork 841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uses new WindowEvent component for Flyout "close on ESC" #1127
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
55535bb
Adds new EuiWindowEvent component
jasonrhodes 48608b7
Switches flyout close on ESC to window event
jasonrhodes dbe6c71
Removes old event listener on Flyout
jasonrhodes 6acd453
Adds EuiWindowEvent example to docs
jasonrhodes 300fcb3
Updates changelog for EuiWindowEvent and Flyout changes
jasonrhodes b703e5f
Adds more clarification to WindowEvent example
jasonrhodes a348dbb
Moves WindowEvent to services folder
jasonrhodes 73f0ed0
Updates changelog to group changes correctly
jasonrhodes e4d90d9
Adds more about how to use the EuiWindowEvent in example docs
jasonrhodes f8b6f3e
Rewrites examples for WindowEvent docs
jasonrhodes 870a759
Adds EuiWindowEvent props to docs
jasonrhodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
export { default as EuiWindowEvent } from './window_event'; | ||
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,51 @@ | ||
import { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
* Adds and removes window events for you (renders null) | ||
* Usage: | ||
* <WindowEvent event='keydown' handler={this.handleKeyDown} /> | ||
*/ | ||
export default class WindowEvent extends Component { | ||
|
||
componentDidMount() { | ||
this.addEvent(this.props); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if (prevProps.event !== this.props.event || prevProps.handler !== this.props.handler) { | ||
this.removeEvent(prevProps); | ||
this.addEvent(this.props); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.removeEvent(this.props); | ||
} | ||
|
||
addEvent({ event, handler }) { | ||
window.addEventListener(event, handler); | ||
} | ||
|
||
removeEvent({ event, handler }) { | ||
window.removeEventListener(event, handler); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
WindowEvent.displayName = 'WindowEvent'; | ||
|
||
WindowEvent.propTypes = { | ||
/** | ||
* Type of event | ||
*/ | ||
event: PropTypes.string.isRequired, | ||
/** | ||
* Event callback function | ||
*/ | ||
handler: PropTypes.func.isRequired | ||
}; |
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,53 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { EuiWindowEvent } from '.'; | ||
|
||
describe('EuiWindowEvent', () => { | ||
|
||
beforeEach(() => { | ||
window.addEventListener = jest.fn(); | ||
window.removeEventListener = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('attaches handler to window event on mount', () => { | ||
const handler = () => null; | ||
shallow(<EuiWindowEvent event="click" handler={handler} />); | ||
expect(window.addEventListener).toHaveBeenCalledTimes(1); | ||
expect(window.addEventListener).toHaveBeenCalledWith('click', handler); | ||
}); | ||
|
||
test('removes handler on unmount', () => { | ||
const handler = () => null; | ||
const wrapper = shallow(<EuiWindowEvent event="click" handler={handler} />); | ||
wrapper.unmount(); | ||
expect(window.removeEventListener).toHaveBeenLastCalledWith('click', handler); | ||
}); | ||
|
||
test('removes and re-attaches handler to window event on update', () => { | ||
const handler1 = () => null; | ||
const handler2 = () => null; | ||
const wrapper = shallow(<EuiWindowEvent event="click" handler={handler1} />); | ||
|
||
expect(window.addEventListener).toHaveBeenLastCalledWith('click', handler1); | ||
|
||
wrapper.setProps({ event: 'hover', handler: handler2 }); | ||
|
||
expect(window.removeEventListener).toHaveBeenLastCalledWith('click', handler1); | ||
expect(window.addEventListener).toHaveBeenLastCalledWith('hover', handler2); | ||
}); | ||
|
||
test('does not remove or re-attach handler if update is irrelevant', () => { | ||
const handler = () => null; | ||
const wrapper = shallow(<EuiWindowEvent event="click" handler={handler} />); | ||
expect(window.addEventListener).toHaveBeenCalledTimes(1); | ||
|
||
wrapper.setProps({ whatever: 'ugh' }); | ||
expect(window.addEventListener).toHaveBeenCalledTimes(1); | ||
expect(window.removeEventListener).not.toHaveBeenCalled(); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the
default as
here as it's in the component file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how you "proxy export" a default component from another file, I think. Or I could make the export in
window_event
be named and dropdefault as
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think we just name everything so we ensure there are no conflicts or that we can add to them down the line without breaking changes.