ant-design-vue/components/locale-provider/LocaleReceiver.jsx

43 lines
1.2 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../_util/vue-types';
import defaultLocaleData from './default';
2018-02-23 08:14:59 +00:00
export default {
props: {
componentName: PropTypes.string.def('global'),
2019-01-12 03:33:27 +00:00
defaultLocale: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
2018-02-23 08:14:59 +00:00
children: PropTypes.func,
},
inject: {
2019-01-28 13:09:13 +00:00
localeData: { default: () => ({}) },
2018-02-23 08:14:59 +00:00
},
methods: {
2019-01-12 03:33:27 +00:00
getLocale() {
const { componentName, defaultLocale } = this;
const locale = defaultLocale || defaultLocaleData[componentName || 'global'];
const { antLocale } = this.localeData;
2019-01-12 03:33:27 +00:00
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
2018-02-23 08:14:59 +00:00
return {
...(typeof locale === 'function' ? locale() : locale),
2018-02-23 08:14:59 +00:00
...(localeFromContext || {}),
2019-01-12 03:33:27 +00:00
};
2018-02-23 08:14:59 +00:00
},
2019-01-12 03:33:27 +00:00
getLocaleCode() {
const { antLocale } = this.localeData;
const localeCode = antLocale && antLocale.locale;
2018-02-23 08:14:59 +00:00
// Had use LocaleProvide but didn't set locale
if (antLocale && antLocale.exist && !localeCode) {
2019-01-12 03:33:27 +00:00
return defaultLocaleData.locale;
2018-02-23 08:14:59 +00:00
}
2019-01-12 03:33:27 +00:00
return localeCode;
2018-02-23 08:14:59 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { $scopedSlots } = this;
const children = this.children || $scopedSlots.default;
return children(this.getLocale(), this.getLocaleCode());
2018-02-23 08:14:59 +00:00
},
2019-01-12 03:33:27 +00:00
};