Skip to content

Commit

Permalink
Added react-keydown for modal closing
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsbrggr committed Aug 25, 2017
1 parent b100135 commit bbc5a24
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export default class ClickModal extends React.PureComponent<Props> {
};

render() {
const {className, clickElement, ...modalProps} = this.props;
const {className, clickElement, children, ...modalProps} = this.props;
return (
<div className={className}>
{React.cloneElement(clickElement, {onClick: this.handleElementClick})}
<Modal
isOpen={this.modalOpen}
onRequestClose={this.handleRequestClose}
{...modalProps}>
{this.props.children}
{children}
</Modal>
</div>
);
Expand Down
19 changes: 3 additions & 16 deletions src/Sulu/Bundle/AdminBundle/Resources/js/components/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import {observable, action} from 'mobx';
import {observer} from 'mobx-react';
import Portal from 'react-portal';
import classnames from 'classnames';
import classNames from 'classnames';
import {afterElementsRendered} from '../../services/DOM';
import Backdrop from '../Backdrop';
import ModalBox from './ModalBox';
Expand All @@ -15,8 +15,6 @@ type Props = ModalProps & {
onRequestClose: () => void,
};

const ESC_KEY = 27;

@observer
export default class Modal extends React.PureComponent<Props> {
static defaultProps = {
Expand All @@ -31,14 +29,9 @@ export default class Modal extends React.PureComponent<Props> {
}

componentDidMount() {
window.addEventListener('keydown', this.handleKeyDown);
this.toggleModal();
}

componentWillUnmount() {
window.removeEventListener('keydown', this.handleKeyDown);
}

@action componentWillReceiveProps(newProps: Props) {
this.isOpenHasChanged = newProps.isOpen !== this.props.isOpen;
}
Expand All @@ -55,20 +48,14 @@ export default class Modal extends React.PureComponent<Props> {
}));
}

handleKeyDown = (event: KeyboardEvent) => {
if (event.keyCode === ESC_KEY) {
this.props.onRequestClose();
}
};

@action handleTransitionEnd = () => {
afterElementsRendered(action(() => {
this.isOpenHasChanged = false;
}));
};

render() {
const containerClasses = classnames({
const containerClass = classNames({
[modalStyles.container]: true,
[modalStyles.isDown]: this.isVisible,
});
Expand All @@ -77,7 +64,7 @@ export default class Modal extends React.PureComponent<Props> {
return (
<Portal isOpened={isOpen || this.isOpenHasChanged}>
<div
className={containerClasses}
className={containerClass}
onTransitionEnd={this.handleTransitionEnd}>
<div className={modalStyles.box}>
<ModalBox
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import React from 'react';
import keydown from 'react-keydown';
import Icon from '../Icon';
import type {ModalProps} from './types';
import Actions from './Actions';
Expand All @@ -16,6 +17,10 @@ export default class ModalBox extends React.PureComponent<Props> {
actions: [],
};

@keydown('esc') requestClose() {
this.props.onRequestClose();
}

render() {
const {title, onRequestClose, children, actions, onConfirm, confirmText} = this.props;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const actions = [
onRequestClose={() => setState({open: false})}
actions={actions}
confirmText="Apply"
isOpen={state.open} >
isOpen={state.open}>
<div style={{width: '900px', height: '500px', display: 'flex', alignItems: 'center', justifyContent: 'center'}}>
<img src="http://www.nyan.cat/cats/original.gif" />
</div>
Expand All @@ -39,7 +39,7 @@ const button = (<button>Open modal</button>);
clickElement={button}
title="Nana Nana Nana"
actions={actions}
confirmText="Ok" >
confirmText="Ok">
<div style={{width: '900px', height: '500px', display: 'flex', alignItems: 'center', justifyContent: 'center'}}>
<img src="https://media.giphy.com/media/NmhVw98IHkQtq/source.gif" />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('The component should render in body when open', () => {
title="My modal title"
onRequestClose={onRequestClose}
confirmText="Apply"
isOpen={true} >
isOpen={true}>
<p>My modal content</p>
</Modal>
).render();
Expand All @@ -30,7 +30,7 @@ test('The component should not render in body when closed', () => {
title="My modal title"
onRequestClose={onRequestClose}
confirmText="Apply"
isOpen={false} >
isOpen={false}>
<p>My modal content</p>
</Modal>
).render();
Expand All @@ -45,7 +45,7 @@ test('The component should request to be closed on click on backdrop', () => {
title="My modal title"
onRequestClose={requestCloseSpy}
confirmText="Apply"
isOpen={true} >
isOpen={true}>
<p>My modal content</p>
</Modal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test('The component should render', () => {
actions={actions}
onRequestClose={onRequestClose}
onConfirm={onConfirm}
confirmText="Alright mate!" >
confirmText="Alright mate!">
<p>My modal content</p>
</ModalBox>
);
Expand All @@ -31,7 +31,7 @@ test('The component should request to be closed when the close icon is clicked',
title="My title"
onRequestClose={onRequestClose}
onConfirm={onConfirm}
confirmText="Alright mate!" >
confirmText="Alright mate!">
<p>My modal content</p>
</ModalBox>
);
Expand All @@ -47,7 +47,7 @@ test('The component should call the callback when the confirm button is clicked'
title="My title"
onRequestClose={onRequestClose}
onConfirm={onConfirm}
confirmText="Alright mate!" >
confirmText="Alright mate!">
<p>My modal content</p>
</ModalBox>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (state.imageLoaded) {
<RectangleSelection
minWidth={150}
minHeight={50}
onChange={s => setState({selection: s})} >
onChange={s => setState({selection: s})}>
<img src="https://unsplash.it/800/500" />
</RectangleSelection>
Expand Down
1 change: 1 addition & 0 deletions src/Sulu/Bundle/AdminBundle/Resources/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"path-to-regexp": "^1.7.0",
"react": "^15.6.0",
"react-dom": "^15.6.0",
"react-keydown": "^1.9.4",
"react-portal": "^3.1.0",
"whatwg-fetch": "^2.0.3"
},
Expand Down

0 comments on commit bbc5a24

Please sign in to comment.