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 all 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;
},
getWeekFirstDate: (locale, date) => {
return startOfWeek(date, { locale: Locale[dealLocal(locale)] });
},
getWeek: (locale, date) => {
return getWeek(date, { locale: Locale[dealLocal(locale)] });
},
Expand Down
1 change: 1 addition & 0 deletions src/generate/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const generateConfig: GenerateConfig<Dayjs> = {
.locale(parseLocale(locale))
.localeData()
.firstDayOfWeek(),
getWeekFirstDate: (locale, date) => date.locale(parseLocale(locale)).weekday(0),
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;
getWeekFirstDate: (locale: string, value: DateType) => DateType;
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();
},
getWeekFirstDate: (locale, date) => {
const clone = date.clone();
const result = clone.locale(locale);
return result.weekday(0);
},
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 weekFirstDate(date: DateType) {
return generateConfig.locale.getWeekFirstDate(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 weekFirstDate(date) > weekFirstDate(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 weekFirstDate(date) < weekFirstDate(startDate);
default:
return (
!isSameDate(generateConfig, date, startDate) &&
Expand Down
14 changes: 14 additions & 0 deletions tests/generate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ describe('Picker.Generate', () => {
});
});

it('getWeekFirstDayValue', () => {
const formatStr = name === 'date-fns' ? 'yyyy-MM-dd' : 'YYYY-MM-DD';
const usDate = generateConfig.locale.getWeekFirstDate(
'en_US',
generateConfig.locale.parse('en_US', '2020-12-30', [formatStr]),
);
const cnDate = generateConfig.locale.getWeekFirstDate(
'zh_CN',
generateConfig.locale.parse('zh_CN', '2020-12-30', [formatStr]),
);
expect(generateConfig.locale.format('en_US', usDate, formatStr)).toEqual('2020-12-27');
expect(generateConfig.locale.format('zh_CN', cnDate, formatStr)).toEqual('2020-12-28');
});

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