Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
adds specific onCancel event in Dialog (fixes #221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Leunen committed Feb 18, 2016
1 parent b443b09 commit 33e7e5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/pages/dialogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Demo extends React.Component {

| Element | Prop | Type | Effect | Remarks |
|:----------|:-------------|:----------|:-------------|:-------------|
| Dialog | onCancel | Function | Defines the handler for the cancel event. (When the user press the "Escape" key) | Optional. By default, the default behavior (closing the dialog) is prevented. |
| Dialog | open | Boolean | Set the open state of the dialog | Optional |
| DialogTitle | component | String, Element, Function | Specify the custom component to use to render the element | Optional. Default 'h4' |
| DialogActions | fullWidth | Boolean | Apply the full-width effect to all children of dialog actions | Optional |
12 changes: 12 additions & 0 deletions src/Dialog/Dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import React, { PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';

const prevent = (event) => event.preventDefault();

class Dialog extends React.Component {
static propTypes = {
className: PropTypes.string,
onCancel: PropTypes.func,
open: PropTypes.bool
};

static defaultProps = {
onCancel: prevent
};

componentDidMount() {
this.refs.dialog.addEventListener('cancel', prevent);
if(this.props.open) {
findDOMNode(this).showModal();
}
Expand All @@ -32,6 +40,10 @@ class Dialog extends React.Component {
}
}

componentWillUnmount() {
this.refs.dialog.removeEventListener('cancel', prevent);
}

render() {
const { className, children, open, ...otherProps } = this.props;

Expand Down

0 comments on commit 33e7e5a

Please sign in to comment.