Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

日期组件如何全局设置format为DD/MM/YYYY? #45416

Closed
liubin595338764 opened this issue Oct 19, 2023 · 12 comments
Closed

日期组件如何全局设置format为DD/MM/YYYY? #45416

liubin595338764 opened this issue Oct 19, 2023 · 12 comments

Comments

@liubin595338764
Copy link

What problem does this feature solve?

日期组件如何全局设置format为DD/MM/YYYY?

What does the proposed API look like?

期望上可以配置RangePicker,DatePicker的默认的format格式

@Wxh16144
Copy link
Member

Wxh16144 commented Oct 19, 2023

CP 组件的 locale 字段可以统一配置

验证了一下 CP 的 locale 不支持配置,我说怎么有印象, 在 #42119 这个 issue 里面被确认是一个 bug。等修好了应该就可以配置了

@afc163

This comment was marked as off-topic.

@afc163 afc163 closed this as completed Oct 19, 2023
@liubin595338764
Copy link
Author

你需要 moment.locale('xx')

@afc163 大佬,没明白,moment.locale('xx')是可以配置吗,没找到他可以配置全局格式的地方,我是想把

现在的默认格式:YYYY-MM-DD
image

期望可以全局设置成:DD/MM/YYYY
image

@Wxh16144
Copy link
Member

我认为 @afc163 的回答是错误的,正如我前面说的,应该通过 CP 的 locale 去统一修改,但是 #42119 反馈目前 CP 修改是有 bug 的,我追溯了一下原因,错误是在 #20023 引入的,尽管现在单元测试用例还存在在最新代码中, 但是用例是错的。

最新的测试用例:

describe('Picker format by locale', () => {
const myLocale = {
...locale,
DatePicker: {
...locale.DatePicker,
dateFormat: 'YYYY 年 M 月 D 日',
dateTimeFormat: 'YYYY 年 M 月 D 日 H 时 m 分 s 秒',
weekFormat: 'YYYY 年 W 周',
monthFormat: 'YYYY 年 M 月',
},
};
const date = dayjs('2000-01-01', 'YYYY-MM-DD');
function matchPicker(name: string, Picker: typeof MonthPicker | typeof WeekPicker, props?: any) {
it(name, () => {
const { container } = render(
<ConfigProvider locale={myLocale as Locale}>
<Picker value={date} {...props} />
</ConfigProvider>,
);
expect(container.firstChild).toMatchSnapshot();
});
}
matchPicker('date', DatePicker);
matchPicker('dateTime', DatePicker, { showTime: true });
matchPicker('week', WeekPicker);
matchPicker('month', MonthPicker);
});

错误引入地方:https://github.com/ant-design/ant-design/pull/20023/files#diff-f33147a574a8e8a0d4a378156aa71ae0e6720e5f384715168600a340e32082d1

解决方案要么等 antd 这边修复,要么自己二次开发一下

@Wxh16144 Wxh16144 reopened this Oct 19, 2023
@Wxh16144 Wxh16144 self-assigned this Oct 19, 2023
@zombieJ
Copy link
Member

zombieJ commented Oct 19, 2023

类似的需求 #23902

@Wxh16144
Copy link
Member

我这两天修复一下这个问题~

@liubin595338764
Copy link
Author

@Wxh16144 有道理,我刚才试了moment.locale('xx')确实不行

@liubin595338764
Copy link
Author

我这两天修复一下这个问题~

修复后可以也更新在V4的版本吗?

@Wxh16144
Copy link
Member

我这两天修复一下这个问题~

修复后可以也更新在V4的版本吗?

当然,4.x 有 bug 还是会继续维护的

@yoyo837
Copy link
Contributor

yoyo837 commented Oct 19, 2023

Duplicate of #23902

@yoyo837 yoyo837 marked this as a duplicate of #23902 Oct 19, 2023
@linhf123
Copy link
Contributor

应该是下面这段逻辑丢失导致的

const LOCALE_FORMAT_MAPPING: PickerMap = {
date: 'dateFormat',
dateTime: 'dateTimeFormat',
week: 'weekFormat',
month: 'monthFormat',
};

renderPicker = (locale: any, localeCode: string) => {
const { format, showTime } = this.props;
const mergedPickerType = showTime ? `${pickerType}Time` : pickerType;
const mergedFormat =
format ||
locale[LOCALE_FORMAT_MAPPING[mergedPickerType]] ||
DEFAULT_FORMAT[mergedPickerType];

@Wxh16144
Copy link
Member

😢 这个问题看起来已经过时了,v5 重构了... (v4 已经不在维护了),更多回复

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants