Skip to content

Commit

Permalink
fix: week range disabled rule (#177)
Browse files Browse the repository at this point in the history
* fix: week range disabled rule

* add test

* update test

* update test

* semantic refactor
  • Loading branch information
AlanCutFlim authored Nov 23, 2020
1 parent 5b98b5c commit f358f6b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
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

1 comment on commit f358f6b

@vercel
Copy link

@vercel vercel bot commented on f358f6b Nov 23, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.