Skip to content

Commit

Permalink
Prevents settings menu from closing while clicking on calendar
Browse files Browse the repository at this point in the history
calendar is considered as "outside" of the settings menu
- State added for watching calendar "display" state
- Allow menu closing (on any click) when calendar is not opened
and prevents it from closing when calendar is open
  • Loading branch information
BenoistP committed Dec 27, 2024
1 parent d31c2ab commit 07b3c24
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/components/layouts/SettingsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ const RealtimeRentMenuItem: FC = () => {
)
}

const RealtimeRentMenuSelectDate: FC = () => {
const RealtimeRentMenuSelectDate: FC<{ isCalendarOpen: boolean, setIsCalendarOpen: (open: boolean) => void }> = ({ isCalendarOpen, setIsCalendarOpen }) => {
const dispatch = useDispatch()
const rentCalculation = useSelector(selectUserRentCalculation)

const { i18n, t } = useTranslation('common', { keyPrefix: 'settings' })

if (rentCalculation.state !== RentCalculationState.Realtime) return null
Expand All @@ -162,28 +161,30 @@ const RealtimeRentMenuSelectDate: FC = () => {
}),
)
}
const toggleIsCalendarOpen = () => setIsCalendarOpen(!isCalendarOpen)

return (
<>
<Menu.Label pb={0}>{t('date')}</Menu.Label>
<DatePickerInput
p={5}
onClick={() => toggleIsCalendarOpen()}
locale={i18n.language}
valueFormat={t('dateFormat')}
value={new Date(rentCalculation.date)}
onChange={(value) => handleDateChange(value as Date)}
onChange={(value) => {handleDateChange(value as Date) ; toggleIsCalendarOpen()}}
defaultDate={new Date()}
/>
<Menu.Divider />
</>
)
}

const RealtimeRentMenu = () => {
const RealtimeRentMenu: FC<{ isCalendarOpen: boolean, setIsCalendarOpen: (open: boolean) => void }> = ({ isCalendarOpen, setIsCalendarOpen }) => {
return (
<>
<RealtimeRentMenuItem />
<RealtimeRentMenuSelectDate />
<RealtimeRentMenuSelectDate isCalendarOpen={isCalendarOpen} setIsCalendarOpen={setIsCalendarOpen} />
<Menu.Divider />
</>
)
Expand Down Expand Up @@ -341,10 +342,13 @@ const RefreshDataButton: FC = () => {
export const SettingsMenu: FC = () => {
const [isOpen, handlers] = useDisclosure(false)
const version = useSelector(selectVersion)
// Prevent menu from closing when the calendar is open
const [isCalendarOpen, setIsCalendarOpen] = useState(false)

return (
<Menu
closeOnItemClick={false}
closeOnClickOutside={!isCalendarOpen}
opened={isOpen}
onOpen={handlers.open}
onClose={handlers.close}
Expand All @@ -359,7 +363,7 @@ export const SettingsMenu: FC = () => {
<Menu.Divider />
<CurrencySelect />
<Menu.Divider />
<RealtimeRentMenu />
<RealtimeRentMenu isCalendarOpen={isCalendarOpen} setIsCalendarOpen={setIsCalendarOpen} />
<ColorSchemeMenuItem />
<Menu.Divider />
<FetchDataSettings />
Expand Down

0 comments on commit 07b3c24

Please sign in to comment.