You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.6 KiB

import * as React from 'react';
import * as PropTypes from 'prop-types';
import * as moment from 'moment';
import interopDefault from '../_util/interopDefault';
7 years ago
import { ModalLocale, changeConfirmLocale } from '../modal/locale';
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;
}
export interface LocaleProviderProps {
locale: Locale;
6 years ago
children?: React.ReactNode;
}
function setMomentLocale(locale: Locale) {
if (locale && locale.locale) {
interopDefault(moment).locale(locale.locale);
} else {
interopDefault(moment).locale('en');
}
}
export default class LocaleProvider extends React.Component<LocaleProviderProps, any> {
static propTypes = {
locale: PropTypes.object,
};
static defaultProps = {
locale: {},
};
static childContextTypes = {
antLocale: PropTypes.object,
};
constructor(props: LocaleProviderProps) {
super(props);
setMomentLocale(props.locale);
changeConfirmLocale(props.locale && props.locale.Modal);
}
getChildContext() {
return {
antLocale: {
...this.props.locale,
exist: true,
},
};
}
componentDidUpdate(prevProps: LocaleProviderProps) {
const { locale } = this.props;
if (prevProps.locale !== locale) {
setMomentLocale(locale);
}
changeConfirmLocale(locale && locale.Modal);
}
componentWillUnmount() {
changeConfirmLocale();
}
render() {
return React.Children.only(this.props.children);
}
}