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

fix: week range disabled rule #177

Merged
merged 5 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/generate/dateFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isAfter,
isValid,
getWeek,
startOfWeek,
format as formatDate,
parse as parseDate,
} from 'date-fns';
Expand Down Expand Up @@ -71,6 +72,9 @@ const generateConfig: GenerateConfig<Date> = {
const clone = Locale[dealLocal(locale)];
return clone.options.weekStartsOn;
},
getWeekFirstDayValue: (locale, date) => {
return startOfWeek(date, { locale: Locale[dealLocal(locale)] }).valueOf();
},
getWeek: (locale, date) => {
return getWeek(date, { locale: Locale[dealLocal(locale)] });
},
Expand Down
5 changes: 5 additions & 0 deletions src/generate/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ const generateConfig: GenerateConfig<Dayjs> = {
.locale(parseLocale(locale))
.localeData()
.firstDayOfWeek(),
getWeekFirstDayValue: (locale, date) =>
date
.locale(parseLocale(locale))
.weekday(0)
.valueOf(),
getWeek: (locale, date) => date.locale(parseLocale(locale)).week(),
getShortWeekDays: locale =>
dayjs()
Expand Down
1 change: 1 addition & 0 deletions src/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface GenerateConfig<DateType> {

locale: {
getWeekFirstDay: (locale: string) => number;
getWeekFirstDayValue: (locale: string, value: DateType) => number;
getWeek: (locale: string, value: DateType) => number;

format: (locale: string, date: DateType, format: string) => string;
Expand Down
5 changes: 5 additions & 0 deletions src/generate/moment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ const generateConfig: GenerateConfig<Moment> = {
const date = moment().locale(locale);
return date.localeData().firstDayOfWeek();
},
getWeekFirstDayValue: (locale, date) => {
const clone = date.clone();
const result = clone.locale(locale);
return result.weekday(0).valueOf();
},
getWeek: (locale, date) => {
const clone = date.clone();
const result = clone.locale(locale);
Expand Down
10 changes: 4 additions & 6 deletions src/hooks/useRangeDisabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ export default function useRangeDisabled<DateType>(
const startDate = getValue(selectedValue, 0);
const endDate = getValue(selectedValue, 1);

function weekNumber(date: DateType) {
const year = generateConfig.getYear(date);
const week = generateConfig.locale.getWeek(locale.locale, date);
return year * 100 + week;
function weekFirstDayValue(date: DateType) {
return generateConfig.locale.getWeekFirstDayValue(locale.locale, date);
}

function monthNumber(date: DateType) {
Expand Down Expand Up @@ -63,7 +61,7 @@ export default function useRangeDisabled<DateType>(
case 'month':
return monthNumber(date) > monthNumber(endDate);
case 'week':
return weekNumber(date) > weekNumber(endDate);
return weekFirstDayValue(date) > weekFirstDayValue(endDate);
default:
return (
!isSameDate(generateConfig, date, endDate) && generateConfig.isAfter(date, endDate)
Expand Down Expand Up @@ -97,7 +95,7 @@ export default function useRangeDisabled<DateType>(
case 'month':
return monthNumber(date) < monthNumber(startDate);
case 'week':
return weekNumber(date) < weekNumber(startDate);
return weekFirstDayValue(date) < weekFirstDayValue(startDate);
default:
return (
!isSameDate(generateConfig, date, startDate) &&
Expand Down
16 changes: 16 additions & 0 deletions tests/generate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ describe('Picker.Generate', () => {
});
});

it('getWeekFirstDayValue', () => {
const formatStr = name === 'date-fns' ? 'yyyy-MM-dd' : 'YYYY-MM-DD';
expect(
generateConfig.locale.getWeekFirstDayValue(
'en_US',
generateConfig.locale.parse('en_US', '2020-12-30', [formatStr]),
),
).toEqual(1609027200000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1609027200000 这个地方可以改一下,应该表示 2020-12-27 的 value吧。这样清楚一些。

This comment was marked as spam.

expect(
generateConfig.locale.getWeekFirstDayValue(
'zh_CN',
generateConfig.locale.parse('zh_CN', '2020-12-30', [formatStr]),
),
).toEqual(1609113600000);
});

it('Parse format Wo', () => {
if (name !== 'date-fns') {
expect(
Expand Down