fix(DatePicker): locale in DatePicker cannot override ConfigProvider locale (#30380)

* fix(DatePicker): locale in DatePicker cannot override ConfigProvider locale

close #30374

* fix: DatePicker.RangePicker

* test: add test
This commit is contained in:
Map1en_ 2021-05-05 22:28:56 +08:00 committed by GitHub
parent 836163adc0
commit 1699754bd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 30 deletions

View File

@ -4,6 +4,9 @@ import moment from 'moment';
import DatePicker from '..';
import LocaleProvider from '../../locale-provider';
import locale from '../../locale-provider/zh_CN';
import ConfigProvider from '../../config-provider';
import jaJP from '../../locale/ja_JP';
import zhTW from '../locale/zh_TW';
const { MonthPicker, WeekPicker } = DatePicker;
@ -53,3 +56,23 @@ describe('MonthPicker and WeekPicker', () => {
expect(render(wrapper.find('Trigger').instance().getComponent())).toMatchSnapshot();
});
});
describe('Override locale setting of the ConfigProvider', () => {
it('DatePicker', () => {
const wrapper = mount(
<ConfigProvider locale={jaJP}>
<DatePicker locale={zhTW} />
</ConfigProvider>,
);
expect(wrapper.find('input').props().placeholder).toEqual('請選擇日期');
});
it('RangePicker', () => {
const wrapper = mount(
<ConfigProvider locale={jaJP}>
<DatePicker.RangePicker locale={zhTW} />
</ConfigProvider>,
);
expect(wrapper.find('input').at(0).props().placeholder).toEqual('開始日期');
expect(wrapper.find('input').at(1).props().placeholder).toEqual('結束日期');
});
});

View File

@ -35,20 +35,8 @@ export default function generateRangePicker<DateType>(
}
};
getDefaultLocale = () => {
const { locale } = this.props;
const result = {
...enUS,
...locale,
};
result.lang = {
...result.lang,
...((locale || {}) as PickerLocale).lang,
};
return result;
};
renderPicker = (locale: PickerLocale) => {
renderPicker = (contextLocale: PickerLocale) => {
const locale = { ...contextLocale, ...this.props.locale };
const { getPrefixCls, direction, getPopupContainer } = this.context;
const {
prefixCls: customizePrefixCls,
@ -117,7 +105,7 @@ export default function generateRangePicker<DateType>(
render() {
return (
<LocaleReceiver componentName="DatePicker" defaultLocale={this.getDefaultLocale}>
<LocaleReceiver componentName="DatePicker" defaultLocale={enUS}>
{this.renderPicker}
</LocaleReceiver>
);

View File

@ -58,20 +58,8 @@ export default function generatePicker<DateType>(generateConfig: GenerateConfig<
}
};
getDefaultLocale = () => {
const { locale } = this.props;
const result = {
...enUS,
...locale,
};
result.lang = {
...result.lang,
...((locale || {}) as PickerLocale).lang,
};
return result;
};
renderPicker = (locale: PickerLocale) => {
renderPicker = (contextLocale: PickerLocale) => {
const locale = { ...contextLocale, ...this.props.locale };
const { getPrefixCls, direction, getPopupContainer } = this.context;
const {
prefixCls: customizePrefixCls,
@ -148,7 +136,7 @@ export default function generatePicker<DateType>(generateConfig: GenerateConfig<
render() {
return (
<LocaleReceiver componentName="DatePicker" defaultLocale={this.getDefaultLocale}>
<LocaleReceiver componentName="DatePicker" defaultLocale={enUS}>
{this.renderPicker}
</LocaleReceiver>
);