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

45 lines
1.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
export interface LocaleReceiverProps {
componentName: string;
defaultLocale: object | Function;
children: (locale: object, localeCode?: string) => React.ReactElement<any>;
}
export interface LocaleReceiverContext {
antLocale?: { [key: string]: any };
}
export default class LocaleReceiver extends React.Component<LocaleReceiverProps> {
static contextTypes = {
antLocale: PropTypes.object,
};
context: LocaleReceiverContext;
getLocale() {
const { componentName, defaultLocale } = this.props;
const { antLocale } = this.context;
const localeFromContext = antLocale && antLocale[componentName];
return {
...(typeof defaultLocale === 'function' ? defaultLocale() : defaultLocale),
...(localeFromContext || {}),
};
}
getLocaleCode() {
const { antLocale } = this.context;
const localeCode = antLocale && antLocale.locale;
// Had use LocaleProvide but didn't set locale
if (antLocale && antLocale.exist && !localeCode) {
return 'en-us';
}
return localeCode;
}
render() {
return this.props.children(this.getLocale(), this.getLocaleCode());
}
}