Skip to content

Commit

Permalink
feat: remove calendarDate from use date picker
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminic committed Oct 2, 2024
1 parent 2996cc5 commit 67fa84a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/hooks/useDatePicker.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Intl } from '@js-temporal/polyfill'
import { Intl, Temporal } from '@js-temporal/polyfill'
import { render } from '@testing-library/react'
import { renderHook } from '@testing-library/react-hooks'
import React from 'react'
import { SupportedCalendar } from '../types'
import { formatDate } from '../utils'
import localisationHelpers from '../utils/localisationHelpers'
import { useDatePicker, UseDatePickerReturn } from './useDatePicker'

Expand Down Expand Up @@ -271,13 +272,21 @@ describe('useDatePicker hook', () => {
})
describe('highlighting today', () => {
const getDayByDate: (
calendarWeekDays: { calendarDate: string; isToday: boolean }[][],
calendarWeekDays: {
isToday: boolean
zdt: Temporal.ZonedDateTime
}[][],
dayToFind: string
) => { calendarDate: string; isToday: boolean }[] = (
calendarWeekDays,
dayToFind
) => {
const days = calendarWeekDays.flatMap((week) => week)
const days = calendarWeekDays
.flatMap((week) => week)
.map((day) => ({
...day,
calendarDate: formatDate(day.zdt, undefined, 'YYYY-MM-DD'),
}))

return days.filter((day) => day.calendarDate === dayToFind)
}
Expand Down Expand Up @@ -485,7 +494,12 @@ describe('clicking a day', () => {

// find and click the day passed to the calendar
for (let i = 0; i < days.length; i++) {
if (days[i].calendarDate === date) {
const formattedDate = formatDate(
days[i].zdt,
undefined,
'YYYY-MM-DD'
)
if (formattedDate === date) {
days[i].onClick()
break
}
Expand Down
3 changes: 0 additions & 3 deletions src/hooks/useDatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ export type UseDatePickerReturn = UseNavigationReturnType & {
calendarWeekDays: {
zdt: Temporal.ZonedDateTime
label: string | number
calendarDate: string
onClick: () => void
isSelected: boolean | undefined
isToday: boolean
isInCurrentMonth: boolean
}[][]
}

type UseDatePickerHookType = (options: DatePickerOptions) => UseDatePickerReturn
type ValidatedDate = Temporal.YearOrEraAndEraYear &
Temporal.MonthOrMonthCode & {
Expand Down Expand Up @@ -194,7 +192,6 @@ export const useDatePicker: UseDatePickerHookType = ({
calendarWeekDays: calendarWeekDaysZdts.map((week) =>
week.map((weekDayZdt) => ({
zdt: weekDayZdt,
calendarDate: formatDate(weekDayZdt, undefined, date.format),
label: localisationHelpers.localiseWeekLabel(
weekDayZdt.withCalendar(localeOptions.calendar),
localeOptions
Expand Down

0 comments on commit 67fa84a

Please sign in to comment.