Skip to content

Commit

Permalink
Fix/add locale option (#43)
Browse files Browse the repository at this point in the history
* chore: sort import names

* fix: add locale option to Calendar

* feat: pass locale to date-fns option

* feat: remove weekStartsOn default value in useCalendar

* feat: pass weekStartsOn to option for weekDateSelection in Calendar
  • Loading branch information
tomchentw authored May 25, 2022
1 parent 7d10841 commit 474bf02
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function Calendar({
blockFuture: false,
start: value?.start || new Date(),
months,
locale,
weekStartsOn,
})

Expand All @@ -74,7 +75,10 @@ export function Calendar({
}

if (weekDateSelection) {
return onSelectDate({ start: startOfWeek(date), end: endOfWeek(date) })
return onSelectDate({
start: startOfWeek(date, { locale, weekStartsOn }),
end: endOfWeek(date, { locale, weekStartsOn }),
})
}

if (
Expand Down
2 changes: 1 addition & 1 deletion src/month-week.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Weekdays = {
}

function weekdays({ weekdayFormat = 'E', locale, weekStartsOn }: Weekdays) {
const start = startOfWeek(new Date(), { weekStartsOn })
const start = startOfWeek(new Date(), { locale, weekStartsOn })
return [...Array(7).keys()].map(i =>
format(addDays(start, i), weekdayFormat, { locale })
)
Expand Down
20 changes: 13 additions & 7 deletions src/useCalendar.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as React from 'react'
import {
addMonths,
eachDayOfInterval,
endOfMonth,
endOfWeek,
isSameMonth,
Locale,
startOfMonth,
eachDayOfInterval,
addMonths,
startOfWeek,
isSameMonth,
subMonths,
endOfWeek,
} from 'date-fns'
import type { CalendarDate } from './types'

Expand All @@ -20,6 +21,7 @@ export type UseCalendar = {
blockFuture?: boolean
allowOutsideDays?: boolean
months?: number
locale?: Locale
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6
}

Expand All @@ -28,7 +30,8 @@ export function useCalendar({
months = 1,
blockFuture,
allowOutsideDays,
weekStartsOn = 0,
locale,
weekStartsOn,
}: UseCalendar) {
const initialState = blockFuture ? subMonths(start, 1) : start
const [date, setDate] = React.useState<CalendarDate>(initialState)
Expand All @@ -45,8 +48,11 @@ export function useCalendar({

const startDateOfMonth = startOfMonth(month)
const endDateOfMonth = endOfMonth(month)
const startWeek = startOfWeek(startDateOfMonth, { weekStartsOn })
const endWeek = endOfWeek(endDateOfMonth, { weekStartsOn })
const startWeek = startOfWeek(startDateOfMonth, {
locale,
weekStartsOn,
})
const endWeek = endOfWeek(endDateOfMonth, { locale, weekStartsOn })
const days = eachDayOfInterval({ start: startWeek, end: endWeek })

return {
Expand Down

0 comments on commit 474bf02

Please sign in to comment.