Skip to content

Commit

Permalink
clean state
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Jul 19, 2016
1 parent 0bd721f commit 322d4b6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 73 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "rc-dialog",
"version": "6.2.0",
"version": "6.2.1",
"description": "dialog ui component for react",
"keywords": [
"react",
Expand Down
12 changes: 12 additions & 0 deletions src/Dialog.jsx
Expand Up @@ -57,6 +57,11 @@ function offset(el) {

const Dialog = React.createClass({
propTypes: {
className: PropTypes.string,
keyboard: PropTypes.bool,
style: PropTypes.object,
mask: PropTypes.bool,
children: PropTypes.any,
onAfterClose: PropTypes.func,
onClose: PropTypes.func,
closable: PropTypes.bool,
Expand All @@ -71,6 +76,13 @@ const Dialog = React.createClass({
getDefaultProps() {
return {
onAfterClose: noop,
className: '',
mask: true,
visible: false,
keyboard: true,
closable: true,
maskClosable: true,
prefixCls: 'rc-dialog',
onClose: noop,
};
},
Expand Down
77 changes: 9 additions & 68 deletions src/DialogWrap.jsx
Expand Up @@ -2,110 +2,51 @@ import React, { PropTypes } from 'react';
import Dialog from './Dialog';
import getContainerRenderMixin from 'rc-util/lib/getContainerRenderMixin';

function noop() {
}

function pick(obj, fields) {
const ret = {};
fields.forEach((f) => {
if (obj[f] !== undefined) {
ret[f] = obj[f];
}
});
return ret;
}

const DialogWrap = React.createClass({
propTypes: {
className: PropTypes.string,
keyboard: PropTypes.bool,
wrapStyle: PropTypes.object,
style: PropTypes.object,
mask: PropTypes.bool,
children: PropTypes.any,
closable: PropTypes.bool,
maskClosable: PropTypes.bool,
prefixCls: PropTypes.string,
visible: PropTypes.bool,
onClose: PropTypes.func,
},
mixins: [
getContainerRenderMixin({
isVisible(instance) {
return instance.state.visible;
return instance.props.visible;
},
autoDestroy: false,
getComponent(instance, extra) {
const props = instance.props;
const dialogProps = pick(props, [
'className', 'closable', 'maskClosable',
'title', 'footer', 'mask', 'keyboard',
'animation', 'transitionName',
'maskAnimation', 'maskTransitionName', 'mousePosition',
'prefixCls', 'style', 'width', 'wrapStyle',
'height', 'zIndex', 'bodyStyle', 'wrapClassName',
]);
return (
<Dialog
{...dialogProps}
onClose={instance.onClose}
visible={instance.state.visible}
{...instance.props}
{...extra}
key="dialog"
>
{props.children}
</Dialog>
/>
);
},
}),
],

getDefaultProps() {
return {
className: '',
mask: true,
keyboard: true,
closable: true,
maskClosable: true,
prefixCls: 'rc-dialog',
onClose: noop,
visible: false,
};
},

getInitialState() {
return {
visible: this.props.visible,
};
},

componentWillReceiveProps({ visible }) {
if (visible !== undefined) {
this.setState({
visible,
});
}
},

shouldComponentUpdate(_, nextState) {
return !!(this.state.visible || nextState.visible);
shouldComponentUpdate({ visible }) {
return !!(this.props.visible || visible);
},

componentWillUnmount() {
if (this.state.visible) {
if (this.props.visible) {
this.renderComponent({
onAfterClose: this.removeContainer,
onClose: noop,
onClose() {
},
visible: false,
});
} else {
this.removeContainer();
}
},

onClose(e) {
this.props.onClose(e);
},

getElement(part) {
return this._component.getElement(part);
},
Expand Down
26 changes: 22 additions & 4 deletions tests/index.js
Expand Up @@ -20,6 +20,24 @@ describe('dialog', () => {

let callback1;

const DialogWrap = React.createClass({
getInitialState() {
return {
visible: false,
maskClosable: true,
};
},
render() {
return (
<Dialog
{...this.props}
visible={this.state.visible}
maskClosable={this.state.maskClosable}
/>
);
},
});

beforeEach(() => {
function onClose() {
callback1 = 1;
Expand All @@ -30,13 +48,13 @@ describe('dialog', () => {

callback1 = 0;
dialog = ReactDOM.render((
<Dialog
<DialogWrap
style={{ width: 600 }}
title={title}
onClose={onClose}
>
<p>第一个dialog</p>
</Dialog>), container);
</DialogWrap>), container);
});

afterEach(() => {
Expand Down Expand Up @@ -149,9 +167,9 @@ describe('dialog', () => {
});

it('renderToBody', () => {
const d = ReactDOM.render(<Dialog>
const d = ReactDOM.render(<DialogWrap>
<p className="renderToBody">1</p>
</Dialog>, container);
</DialogWrap>, container);
expect($('.renderToBody').length).to.be(0);
expect($('.rc-dialog-wrap').length).to.be(0);
d.setState({
Expand Down

0 comments on commit 322d4b6

Please sign in to comment.