ant-design/components/locale-provider/index.tsx

81 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import PropTypes from 'prop-types';
2018-01-13 17:53:25 +08:00
import * as moment from 'moment';
2017-07-31 01:50:46 +08:00
import { ModalLocale, changeConfirmLocale } from '../modal/locale';
2016-03-03 16:28:02 +08:00
2018-01-13 17:53:25 +08:00
export interface Locale {
locale: string;
Pagination?: Object;
DatePicker?: Object;
TimePicker?: Object;
Calendar?: Object;
Table?: Object;
Modal?: ModalLocale;
Popconfirm?: Object;
Transfer?: Object;
Select?: Object;
Upload?: Object;
}
2016-08-02 16:10:26 +08:00
export interface LocaleProviderProps {
2018-01-13 17:53:25 +08:00
locale: Locale;
children?: React.ReactElement<any>;
2016-08-02 16:10:26 +08:00
}
2018-01-13 17:53:25 +08:00
function setMomentLocale(locale: Locale) {
if (locale && locale.locale) {
moment.locale(locale.locale);
} else {
moment.locale('en');
2018-01-13 17:53:25 +08:00
}
}
2016-08-02 16:10:26 +08:00
export default class LocaleProvider extends React.Component<LocaleProviderProps, any> {
static propTypes = {
locale: PropTypes.object,
2016-07-13 11:14:24 +08:00
};
2018-01-13 17:53:25 +08:00
static defaultProps = {
locale: {},
};
static childContextTypes = {
antLocale: PropTypes.object,
2016-07-13 11:14:24 +08:00
};
2016-03-03 16:28:02 +08:00
getChildContext() {
return {
antLocale: {
...this.props.locale,
exist: true,
},
2016-03-03 16:28:02 +08:00
};
}
componentWillMount() {
2018-01-13 17:53:25 +08:00
setMomentLocale(this.props.locale);
this.componentDidUpdate();
}
2018-01-13 17:53:25 +08:00
componentWillReceiveProps(nextProps: LocaleProviderProps) {
const { locale } = this.props;
const nextLocale = nextProps.locale;
2018-01-14 01:30:21 +08:00
if (locale !== nextLocale) {
2018-01-13 17:53:25 +08:00
setMomentLocale(nextProps.locale);
}
}
componentDidUpdate() {
const { locale } = this.props;
changeConfirmLocale(locale && locale.Modal);
}
2018-01-13 17:53:25 +08:00
componentWillUnmount() {
changeConfirmLocale();
}
2016-03-03 16:28:02 +08:00
render() {
2016-03-05 16:39:27 +08:00
return React.Children.only(this.props.children);
2016-03-03 16:28:02 +08:00
}
}