Skip to content

Commit

Permalink
fix(fields): normalize calendar header title in DatePicker & DateRang…
Browse files Browse the repository at this point in the history
…ePicker
  • Loading branch information
ivangabriele committed Dec 21, 2022
1 parent ff877e9 commit 611f811
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 70 deletions.
37 changes: 20 additions & 17 deletions src/fields/DatePicker/CalendarPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useRef } from 'react'
import { DatePicker as RsuiteDatePicker } from 'rsuite'
import { CustomProvider as RsuiteCustomProvider, DatePicker as RsuiteDatePicker } from 'rsuite'
import rsuiteFrFr from 'rsuite/locales/fr_FR'
import styled from 'styled-components'

import { useForceUpdate } from '../../hooks/useForceUpdate'
Expand Down Expand Up @@ -46,22 +47,24 @@ export function CalendarPicker({ defaultValue, isHistorical, isOpen, onChange }:
}, [forceUpdate])

return (
<Box ref={boxRef as any} onClick={stopMouseEventPropagation}>
{boxRef.current && (
<RsuiteDatePicker
container={boxRef.current}
disabledDate={disabledDate}
format="yyyy-MM-dd"
locale={RSUITE_CALENDAR_LOCALE}
oneTap
onSelect={handleSelect}
open={isOpen}
// `defaultValue` seems to be immediatly cancelled so we come down to using a controlled `value`
ranges={[]}
value={defaultValue}
/>
)}
</Box>
<RsuiteCustomProvider locale={rsuiteFrFr}>
<Box ref={boxRef as any} onClick={stopMouseEventPropagation}>
{boxRef.current && (
<RsuiteDatePicker
container={boxRef.current}
disabledDate={disabledDate}
format="yyyy-MM-dd"
locale={RSUITE_CALENDAR_LOCALE}
oneTap
onSelect={handleSelect}
open={isOpen}
// `defaultValue` seems to be immediatly cancelled so we come down to using a controlled `value`
ranges={[]}
value={defaultValue}
/>
)}
</Box>
</RsuiteCustomProvider>
)
}

Expand Down
40 changes: 19 additions & 21 deletions src/fields/DateRangePicker/RangeCalendarPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useCallback, useEffect, useMemo, useRef } from 'react'
import { DateRangePicker as RsuiteDateRangePicker } from 'rsuite'
import { CustomProvider as RsuiteCustomProvider, DateRangePicker as RsuiteDateRangePicker } from 'rsuite'
import rsuiteFrFr from 'rsuite/locales/fr_FR'
import styled from 'styled-components'

import { useForceUpdate } from '../../hooks/useForceUpdate'
import { capitalizeFirstLetter } from '../../utils/capitalizeFirstLetter'
import { dayjs } from '../../utils/dayjs'
import { getUtcDayjs } from '../../utils/getUtcDayjs'
import { getUtcizedDayjs } from '../../utils/getUtcizedDayjs'
import { sortDates } from '../../utils/sortDates'
Expand Down Expand Up @@ -57,31 +56,30 @@ export function RangeCalendarPicker({ defaultValue, isHistorical, isOpen, onChan
[onChange]
)

const renderTitle = useCallback((date: Date) => capitalizeFirstLetter(dayjs(date).format('MMMM YYYY')), [])

useEffect(() => {
// We wait for the <Box /> to render so that `boxRef` is defined
// and can be used as a container for <RsuiteDateRangePicker />
forceUpdate()
}, [forceUpdate])

return (
<Box ref={boxRef as any} onClick={stopMouseEventPropagation}>
{boxRef.current && (
<RsuiteDateRangePicker
container={boxRef.current}
disabledDate={disabledDate}
format="yyyy-MM-dd"
locale={RSUITE_CALENDAR_LOCALE}
onSelect={handleSelect}
open={isOpen}
ranges={[]}
renderTitle={renderTitle}
// `defaultValue` seems to be immediatly cancelled so we come down to using a controlled `value`
value={controlledValue}
/>
)}
</Box>
<RsuiteCustomProvider locale={rsuiteFrFr}>
<Box ref={boxRef as any} onClick={stopMouseEventPropagation}>
{boxRef.current && (
<RsuiteDateRangePicker
container={boxRef.current}
disabledDate={disabledDate}
format="yyyy-MM-dd"
locale={RSUITE_CALENDAR_LOCALE}
onSelect={handleSelect}
open={isOpen}
ranges={[]}
// `defaultValue` seems to be immediatly cancelled so we come down to using a controlled `value`
value={controlledValue}
/>
)}
</Box>
</RsuiteCustomProvider>
)
}

Expand Down
6 changes: 1 addition & 5 deletions src/fields/DateRangePicker/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fr from 'date-fns/locale/fr'

import type { DateRangePickerLocale } from 'rsuite'

export const HOURS_AS_OPTIONS = new Array(24).fill(undefined).map((_, index) => ({
Expand All @@ -8,10 +6,8 @@ export const HOURS_AS_OPTIONS = new Array(24).fill(undefined).map((_, index) =>
}))

export const RSUITE_CALENDAR_LOCALE: DateRangePickerLocale = {
// TODO Why this doesn't work?
dateLocale: fr,
formattedDayPattern: 'dd/MM/yyyy',
formattedMonthPattern: 'MM/yyyy',
formattedMonthPattern: 'MMMM yyyy',
friday: 'V',
hours: 'Heures',
minutes: 'Minutes',
Expand Down
21 changes: 0 additions & 21 deletions src/utils/__tests__/capitalizeFirstLetter.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/utils/capitalizeFirstLetter.ts

This file was deleted.

0 comments on commit 611f811

Please sign in to comment.