Skip to content

Commit

Permalink
refactor: remove calendarDate from onDateSelect callback
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Oct 3, 2024
1 parent 371a08f commit 91efe0e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
29 changes: 10 additions & 19 deletions src/hooks/useDatePicker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { convertToIso8601, formatDate } from '../utils'
import localisationHelpers from '../utils/localisationHelpers'
import { useDatePicker, UseDatePickerReturn } from './useDatePicker'

Expand Down Expand Up @@ -510,34 +510,21 @@ describe('clicking a day', () => {
}
it('should call the callback with correct info for Gregorian calendar', () => {
const date = '2018-01-22'
const { calendarDate, calendarDateString } = renderForClick({
const { calendarDateString } = renderForClick({
calendar: 'gregory',
date,
})
expect(calendarDate.toString()).toEqual(
'2018-01-22T00:00:00+02:00[Africa/Khartoum][u-ca=gregory]'
)
expect(calendarDateString).toEqual('2018-01-22')
})
it('should call the callback with correct info for Ethiopic calendar', () => {
const date = '2015-13-02'
const { calendarDate, calendarDateString } = renderForClick({
const { calendarDateString } = renderForClick({
calendar: 'ethiopic',
date,
})
expect(calendarDateString).toEqual('2015-13-02')
expect(
calendarDate.withCalendar('iso8601').toLocaleString('en-GB')
).toMatch('07/09/2023')

expect(
calendarDate.toLocaleString('en-GB', {
month: 'long',
year: 'numeric',
day: 'numeric',
calendar: 'ethiopic',
})
).toEqual('2 Pagumen 2015 ERA1')
const calendarDate = convertToIso8601(calendarDateString, 'ethiopic')
expect(calendarDate).toEqual({ day: 7, month: 9, year: 2023 })
})
it('should call the callback with correct info for a custom (Nepali) calendar', () => {
const date = '2077-12-30'
Expand All @@ -548,7 +535,11 @@ describe('clicking a day', () => {
expect(calendarDateString).toEqual('2077-12-30')
expect(
localisationHelpers.localiseMonth(
calendarDate,
{
year: 20777,
month: 12,
day: 30,
},
{
locale: 'en-NP',
calendar: 'nepali',
Expand Down
3 changes: 0 additions & 3 deletions src/hooks/useDatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ type DatePickerOptions = {
date: string
options: PickerOptions
onDateSelect: ({
calendarDate,
calendarDateString,
}: {
calendarDate: Temporal.ZonedDateTime
calendarDateString: string
}) => void
minDate?: string
Expand Down Expand Up @@ -150,7 +148,6 @@ export const useDatePicker: UseDatePickerHookType = ({
const selectDate = useCallback(
(zdt: Temporal.ZonedDateTime) => {
onDateSelect({
calendarDate: zdt,
calendarDateString: formatDate(zdt, undefined, date.format),
})
},
Expand Down
6 changes: 5 additions & 1 deletion src/utils/localisationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ const localiseWeekLabel = (
}

const localiseMonth = (
zdt: Temporal.ZonedDateTime | Temporal.PlainYearMonth | Temporal.PlainDate,
zdt:
| Temporal.ZonedDateTime
| Temporal.PlainYearMonth
| Temporal.PlainDate
| Temporal.PlainDateLike,
localeOptions: PickerOptions,
format: Intl.DateTimeFormatOptions
) => {
Expand Down

0 comments on commit 91efe0e

Please sign in to comment.