mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-11 13:59:11 +08:00
7743967bf6
close #18731
88 lines
1.9 KiB
TypeScript
88 lines
1.9 KiB
TypeScript
import * as React from 'react';
|
|
import * as PropTypes from 'prop-types';
|
|
import * as moment from 'moment';
|
|
import interopDefault from '../_util/interopDefault';
|
|
import { ModalLocale, changeConfirmLocale } from '../modal/locale';
|
|
import warning from '../_util/warning';
|
|
|
|
export const ANT_MARK = 'internalMark';
|
|
|
|
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;
|
|
children?: React.ReactNode;
|
|
_ANT_MARK__?: string;
|
|
}
|
|
|
|
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);
|
|
|
|
warning(
|
|
props._ANT_MARK__ === ANT_MARK,
|
|
'LocaleProvider',
|
|
'`LocaleProvider` is deprecated. Please use `locale` with `ConfigProvider` instead: http://u.ant.design/locale',
|
|
);
|
|
}
|
|
|
|
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 this.props.children;
|
|
}
|
|
}
|