Make LocalProvider compatible with Modal.xxx

This commit is contained in:
afc163 2016-03-05 17:14:45 +08:00 committed by Benjy Cui
parent 2482140d2f
commit 83f96e5f19
4 changed files with 48 additions and 5 deletions

View File

@ -37,6 +37,18 @@ const Page = React.createClass({
this.setState({ visible: false });
},
render() {
const info = () => {
Modal.info({
title: 'some info',
content: 'some info',
});
};
const confirm = () => {
Modal.confirm({
title: 'some info',
content: 'some info',
});
};
return (
<div>
<Pagination defaultCurrent={1} total={50} showSizeChanger />
@ -50,6 +62,8 @@ const Page = React.createClass({
<Modal title="Locale Modal" visible={this.state.visible} onCancel={this.hideModal}>
<p>Locale Modal</p>
</Modal>
<Button onClick={info}>Show info</Button>
<Button onClick={confirm}>Show confirm</Button>
</div>
);
}

View File

@ -11,6 +11,7 @@ module.exports = {
Modal: {
okText: 'OK',
cancelText: 'Cancel',
justOkText: 'OK',
},
Popconfirm: {
okText: 'OK',

View File

@ -1,4 +1,5 @@
import React from 'react';
import { changeConfirmLocale } from '../modal/confirm';
export default class LocaleProvider extends React.Component {
getChildContext() {
@ -6,6 +7,16 @@ export default class LocaleProvider extends React.Component {
antLocale: this.props.locale,
};
}
componentDidMount() {
this.componentDidUpdate();
}
componentDidUpdate() {
const { locale } = this.props;
changeConfirmLocale(locale && locale.Modal);
}
componentWillUnMount() {
changeConfirmLocale();
}
render() {
return React.Children.only(this.props.children);
}

View File

@ -5,8 +5,24 @@ import Icon from '../icon';
import Button from '../button';
import objectAssign from 'object-assign';
export default function (config) {
const props = objectAssign({}, config || {});
const defaultLocale = {
okText: '确定',
cancelText: '取消',
justOkText: '知道了',
};
let runtimeLocale = { ...defaultLocale };
export function changeConfirmLocale(newLocale) {
if (newLocale) {
objectAssign(runtimeLocale, newLocale);
} else {
runtimeLocale = defaultLocale;
}
}
export default function confirm(config) {
const props = objectAssign({}, config);
let div = document.createElement('div');
document.body.appendChild(div);
@ -22,12 +38,13 @@ export default function (config) {
props.okCancel = true;
}
props.okText = props.okText || (props.okCancel ? '确定' : '知道了');
props.cancelText = props.cancelText || '取消';
props.okText = props.okText ||
(props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
props.cancelText = props.cancelText || runtimeLocale.cancelText;
function close() {
d.setState({
visible: false
visible: false,
});
ReactDOM.unmountComponentAtNode(div);
div.parentNode.removeChild(div);