#750 auto close date picker after select a date on mobile

This commit is contained in:
lethemanh
2026-04-09 14:14:59 +07:00
parent 1e5e1abb61
commit ae620f96cd
3 changed files with 38 additions and 7 deletions
+11 -2
View File
@@ -30,6 +30,7 @@ jest.mock('@linagora/twake-mui', () => {
}) })
const scrollIntoViewMock = jest.fn() const scrollIntoViewMock = jest.fn()
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalScrollIntoView = window.HTMLElement.prototype.scrollIntoView const originalScrollIntoView = window.HTMLElement.prototype.scrollIntoView
beforeAll(() => { beforeAll(() => {
@@ -63,20 +64,27 @@ describe('DatePickerMobile', () => {
const setup = async ( const setup = async (
initialDate: Date initialDate: Date
): Promise< ): Promise<
RenderResult & { calendarApi: CalendarApi; onDateChange: jest.Mock } RenderResult & {
calendarApi: CalendarApi
onDateChange: jest.Mock
onCloseDatePicker: jest.Mock
}
> => { > => {
const calendarApi = makeCalendarApi(initialDate) const calendarApi = makeCalendarApi(initialDate)
const onDateChange = jest.fn() const onDateChange = jest.fn()
const onCloseDatePicker = jest.fn()
const renderResult = await act(() => const renderResult = await act(() =>
render( render(
<DatePickerMobile <DatePickerMobile
calendarRef={{ current: calendarApi }} calendarRef={{ current: calendarApi }}
currentDate={initialDate} currentDate={initialDate}
onDateChange={onDateChange} onDateChange={onDateChange}
onCloseDatePicker={onCloseDatePicker}
/> />
) )
) )
return { ...renderResult, calendarApi, onDateChange } return { ...renderResult, calendarApi, onDateChange, onCloseDatePicker }
} }
const expectNavigatedDate = ( const expectNavigatedDate = (
@@ -160,6 +168,7 @@ describe('DatePickerMobile', () => {
calendarRef={{ current: calendarApi }} calendarRef={{ current: calendarApi }}
currentDate={newDate} currentDate={newDate}
onDateChange={onDateChange} onDateChange={onDateChange}
onCloseDatePicker={jest.fn()} // Need to pass dummy fn for required prop
/> />
) )
}) })
+13 -2
View File
@@ -36,6 +36,16 @@ export const MobileMenubar: React.FC<MobileMenubarProps> = ({
const monthName = t(`months.standalone.${monthIndex}`) const monthName = t(`months.standalone.${monthIndex}`)
const dateLabel = `${monthName} ${year}` const dateLabel = `${monthName} ${year}`
const onToggleDatePicker = (): void => {
setOpenDatePicker(prev => {
const newState = !prev
setTimeout(() => {
calendarRef.current?.updateSize?.()
}, 0)
return newState
})
}
return ( return (
<> <>
<header className="menubar"> <header className="menubar">
@@ -53,13 +63,13 @@ export const MobileMenubar: React.FC<MobileMenubarProps> = ({
<Stack direction="row" className="current-date-time"> <Stack direction="row" className="current-date-time">
<Typography <Typography
sx={{ lineHeight: 'unset' }} sx={{ lineHeight: 'unset' }}
onClick={() => setOpenDatePicker(prev => !prev)} onClick={onToggleDatePicker}
> >
{dateLabel} {dateLabel}
</Typography> </Typography>
<IconButton <IconButton
size="small" size="small"
onClick={() => setOpenDatePicker(prev => !prev)} onClick={onToggleDatePicker}
aria-label={ aria-label={
openDatePicker openDatePicker
? t('menubar.hideDatePicker') ? t('menubar.hideDatePicker')
@@ -89,6 +99,7 @@ export const MobileMenubar: React.FC<MobileMenubarProps> = ({
calendarRef={calendarRef} calendarRef={calendarRef}
currentDate={currentDate} currentDate={currentDate}
onDateChange={onDateChange} onDateChange={onDateChange}
onCloseDatePicker={() => setOpenDatePicker(false)}
/> />
) : null} ) : null}
</> </>
@@ -13,12 +13,14 @@ export type DatePickerMobileProps = {
calendarRef: React.RefObject<CalendarApi | null> calendarRef: React.RefObject<CalendarApi | null>
currentDate: Date currentDate: Date
onDateChange?: (date: Date) => void onDateChange?: (date: Date) => void
onCloseDatePicker: () => void
} }
export const DatePickerMobile: React.FC<DatePickerMobileProps> = ({ export const DatePickerMobile: React.FC<DatePickerMobileProps> = ({
calendarRef, calendarRef,
currentDate, currentDate,
onDateChange onDateChange,
onCloseDatePicker
}) => { }) => {
const { t, lang } = useI18n() const { t, lang } = useI18n()
const theme = useTheme() const theme = useTheme()
@@ -40,6 +42,12 @@ export const DatePickerMobile: React.FC<DatePickerMobileProps> = ({
onChangeDate(dayjs(new Date(year, monthIndex, clampedDay))) onChangeDate(dayjs(new Date(year, monthIndex, clampedDay)))
} }
const handlePickDate = (newDate: PickerValue): void => {
onChangeDate(newDate)
onCloseDatePicker()
calendarRef.current?.updateSize?.()
}
return ( return (
<LocalizationProvider <LocalizationProvider
key={lang} key={lang}
@@ -53,10 +61,13 @@ export const DatePickerMobile: React.FC<DatePickerMobileProps> = ({
> >
<StaticDatePicker <StaticDatePicker
value={dayjs(currentDate)} value={dayjs(currentDate)}
onChange={onChangeDate} onChange={handlePickDate}
showDaysOutsideCurrentMonth showDaysOutsideCurrentMonth
slots={{ calendarHeader: () => null, actionBar: () => null }} slots={{ calendarHeader: () => null, actionBar: () => null }}
sx={{ width: '100%', marginTop: '10px' }} sx={{
width: '100%',
marginTop: '10px'
}}
slotProps={{ slotProps={{
toolbar: { hidden: true }, toolbar: { hidden: true },
layout: { layout: {