-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat(Calendar): Add firstDayOfWeek
prop
#7363
Changes from 35 commits
8ecbca1
3819daa
4675901
b84969e
afa1c0f
509257c
b03d491
68b9f48
c9f80eb
15a047a
c4f7e5f
4020fb2
ab374a8
b554178
237cdfb
11c86e3
c16d42e
b7f7185
e4a342b
158341e
363b9be
94241a4
37b329a
0f48109
60b00a3
4e373d7
6d255c0
932fac8
cdfc45d
48c3dc0
d9f6bd1
80409f7
38c0ed2
a6bf5a1
ad1a352
22d398f
779505b
6d061df
34a3442
e631c51
32f9e25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,13 +180,30 @@ export function getMinimumDayInMonth(date: AnyCalendarDate) { | |
return 1; | ||
} | ||
|
||
const DAY_MAP = { | ||
sun: 0, | ||
mon: 1, | ||
tue: 2, | ||
wed: 3, | ||
thu: 4, | ||
fri: 5, | ||
sat: 6 | ||
}; | ||
|
||
type DayOfWeek = 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be in the @react-types package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the above, I didn't think it was worth exporting, but open to changing if that would be better. |
||
|
||
/** Returns the first date of the week for the given date and locale. */ | ||
export function startOfWeek(date: ZonedDateTime, locale: string): ZonedDateTime; | ||
export function startOfWeek(date: CalendarDateTime, locale: string): CalendarDateTime; | ||
export function startOfWeek(date: CalendarDate, locale: string): CalendarDate; | ||
export function startOfWeek(date: DateValue, locale: string): DateValue; | ||
export function startOfWeek(date: DateValue, locale: string): DateValue { | ||
export function startOfWeek(date: ZonedDateTime, locale: string, firstDayOfWeek?: DayOfWeek): ZonedDateTime; | ||
export function startOfWeek(date: CalendarDateTime, locale: string, firstDayOfWeek?: DayOfWeek): CalendarDateTime; | ||
export function startOfWeek(date: CalendarDate, locale: string, firstDayOfWeek?: DayOfWeek): CalendarDate; | ||
export function startOfWeek(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): DateValue; | ||
export function startOfWeek(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): DateValue { | ||
let dayOfWeek = getDayOfWeek(date, locale); | ||
if (firstDayOfWeek) { | ||
let offset = (DAY_MAP[firstDayOfWeek] - getWeekStart(locale) + 7) % 7; | ||
let diff = (7 + dayOfWeek - offset) % 7; | ||
return date.subtract({days: diff}); | ||
} | ||
return date.subtract({days: dayOfWeek}); | ||
} | ||
|
||
|
@@ -225,17 +242,21 @@ function getRegion(locale: string): string | undefined { | |
return part === 'u' ? undefined : part; | ||
} | ||
|
||
function getWeekStart(locale: string): number { | ||
export function getWeekStart(locale: string): number { | ||
// TODO: use Intl.Locale for this once browsers support the weekInfo property | ||
// https://github.com/tc39/proposal-intl-locale-info | ||
let region = getRegion(locale); | ||
return region ? weekStartData[region] || 0 : 0; | ||
} | ||
|
||
/** Returns the number of weeks in the given month and locale. */ | ||
export function getWeeksInMonth(date: DateValue, locale: string): number { | ||
let days = date.calendar.getDaysInMonth(date); | ||
return Math.ceil((getDayOfWeek(startOfMonth(date), locale) + days) / 7); | ||
export function getWeeksInMonth(date: DateValue, locale: string, firstDayOfWeek?: DayOfWeek): number { | ||
let monthStart = startOfMonth(date); | ||
let firstVisibleDay = startOfWeek(monthStart, locale, firstDayOfWeek); | ||
let monthEnd = endOfMonth(date); | ||
let lastVisibleDay = startOfWeek(monthEnd, locale, firstDayOfWeek).add({days: 6}); | ||
reidbarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let totalDays = lastVisibleDay.calendar.toJulianDay(lastVisibleDay) - firstVisibleDay.calendar.toJulianDay(firstVisibleDay) + 1; | ||
return Math.ceil(totalDays / 7); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same logic as before should work if you pass |
||
} | ||
|
||
/** Returns the lesser of the two provider dates. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be exported to be used https://github.com/adobe/react-spectrum/pull/7363/files#diff-b598cd9f6e4569b26a010dc1b345368a20953c412f842b0c035f3cd661890792R24?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think it was worth exporting since they're different packages and it would be public. Open to changing this if we think that's better though.