Skip to content

fix: correctly close relative pickers dialogs #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/components/DatePicker/hooks/useDatePickerProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
? state.dateFieldState.displayValue
: state.dateFieldState.displayValue.start,
);
const [prevDateValue, setPrevDateValue] = React.useState<any>(

Check warning on line 41 in src/components/DatePicker/hooks/useDatePickerProps.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
state.dateFieldState.displayValue,
);

Expand Down Expand Up @@ -150,6 +150,7 @@
);
},
modal: !props.disableFocusTrap,
returnFocus: !props.disableFocusTrap,
disablePortal: props.disablePortal,
placement: props.popupPlacement,
offset: props.popupOffset,
Expand Down
21 changes: 18 additions & 3 deletions src/components/RelativeDateField/RelativeDateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ export function RelativeDateField(props: RelativeDateFieldProps) {
const [anchor, setAnchor] = React.useState<HTMLElement | null>(null);

const [isOpen, setOpen] = React.useState(false);
const dialogClosing = React.useRef(false);

const {focusWithinProps} = useFocusWithin({
onFocusWithinChange: (isFocusWithin) => {
setOpen(isFocusWithin);
if (!dialogClosing.current) {
setOpen(isFocusWithin);
}
},
isDisabled: isMobile,
});
Expand Down Expand Up @@ -86,6 +89,12 @@ export function RelativeDateField(props: RelativeDateFieldProps) {
setOpen(true);
}
}}
controlProps={{
...inputProps.controlProps,
onClick: () => {
setOpen(true);
},
}}
/>
<HiddenInput
name={props.name}
Expand All @@ -100,10 +109,16 @@ export function RelativeDateField(props: RelativeDateFieldProps) {
<Popup
anchorElement={anchor}
open={isOpen}
onOpenChange={(_open, _event, reason) => {
if (reason === 'escape-key') {
onOpenChange={(open) => {
if (!open) {
setOpen(false);
}
dialogClosing.current = !open;
}}
onTransitionOutComplete={() => {
setTimeout(() => {
dialogClosing.current = false;
});
}}
placement={props.popupPlacement}
offset={props.popupOffset}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export function useRelativeDatePickerProps(
offset: props.popupOffset,
className: props.popupClassName,
style: props.popupStyle,
returnFocus: false,
},
calendarProps: {
ref: calendarRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ export function RelativeRangeDatePicker(props: RelativeRangeDatePickerProps) {
const isMobile = useMobile();

const [anchor, setAnchor] = React.useState<HTMLDivElement | null>(null);
const inputRef = React.useRef<HTMLInputElement>(null);
const dialogClosing = React.useRef(false);

const [isActive, setIsActive] = React.useState(false);
const [open, setOpen] = useControlledState<boolean>(undefined, false, props.onOpenChange);

const {focusWithinProps} = useFocusWithin({
isDisabled: props.disabled,
onFocusWithin: (e) => {
if (!isActive) {
props.onFocus?.(e);
}
},
onBlurWithin: (e) => {
if (!open) {
setIsActive(false);
props.onBlur?.(e);
onFocusWithin: props.onFocus,
onBlurWithin: props.onBlur,
onFocusWithinChange: (isFocusWithin) => {
if (
isFocusWithin &&
!dialogClosing.current &&
!props.renderControl &&
document.activeElement?.tagName !== 'BUTTON'
) {
setOpen(true);
}
},
});
Expand All @@ -54,16 +54,13 @@ export function RelativeRangeDatePicker(props: RelativeRangeDatePickerProps) {
props={props}
state={state}
open={open}
setOpen={setOpen}
isMobile={isMobile}
ref={inputRef}
onClick={() => {
if (props.disabled) {
return;
}
if (!open) {
setIsActive(true);
setOpen(true);
}
setOpen(true);
}}
onKeyDown={(e) => {
if (props.disabled) {
Expand All @@ -75,15 +72,8 @@ export function RelativeRangeDatePicker(props: RelativeRangeDatePickerProps) {
}
}}
onClickCalendar={() => {
setIsActive(true);
setOpen(!open);
}}
onFocus={() => {
if (!isActive) {
setIsActive(true);
setOpen(true);
}
}}
onUpdate={(v: string) => {
if (!props.readOnly && !v) {
state.setValue(null, 'default');
Expand Down Expand Up @@ -134,16 +124,18 @@ export function RelativeRangeDatePicker(props: RelativeRangeDatePickerProps) {
onUpdate={state.setValue}
timeZone={state.timeZone}
open={open}
onClose={(reason) => {
onClose={() => {
setOpen(false);
if (reason === 'escape-key') {
setTimeout(() => {
inputRef.current?.focus({preventScroll: true});
});
}
dialogClosing.current = true;
}}
onRemove={() => {
setTimeout(() => {
dialogClosing.current = false;
});
}}
anchor={anchor}
modal
returnFocus
popupClassName={props.popupClassName}
popupStyle={props.popupStyle}
popupPlacement={props.popupPlacement}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export type ControlProps = {
props: RelativeRangeDatePickerProps;
state: RelativeRangeDatePickerState;
open: boolean;
setOpen: (open: boolean) => void;
isMobile?: boolean;
onClick: (e: React.MouseEvent<HTMLElement>) => void;
onKeyDown: (e: React.KeyboardEvent<HTMLElement>) => void;
onFocus: (e: React.FocusEvent<HTMLElement>) => void;
onFocus?: (e: React.FocusEvent<HTMLElement>) => void;
onClickCalendar: (e: React.MouseEvent<HTMLElement>) => void;
onUpdate: (value: string) => void;
};
Expand All @@ -29,7 +30,18 @@ const b = block('relative-range-date-picker-control');

export const Control = React.forwardRef<HTMLInputElement, ControlProps>(
(
{props, state, open, isMobile, onClick, onKeyDown, onFocus, onClickCalendar, onUpdate},
{
props,
state,
open,
setOpen,
isMobile,
onClick,
onKeyDown,
onFocus,
onClickCalendar,
onUpdate,
},
ref,
) => {
const {alwaysShowAsAbsolute, presetTabs, getRangeTitle} = props;
Expand Down Expand Up @@ -75,6 +87,7 @@ export const Control = React.forwardRef<HTMLInputElement, ControlProps>(
validationState,
errorMessage,
open,
setOpen,
triggerProps,
})
) : (
Expand Down Expand Up @@ -111,6 +124,7 @@ export const Control = React.forwardRef<HTMLInputElement, ControlProps>(
aria-expanded={open}
aria-label={i18n('Range date picker')}
onClick={onClickCalendar}
tabIndex={-1}
>
<Icon data={CalendarIcon} />
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ export interface PickerDialogProps extends PickerFormProps, PopupStyleProps {
open: boolean;
/** Handles popup close event */
onClose: (reason?: Parameters<NonNullable<PopupProps['onOpenChange']>>[2] | 'apply') => void;
/** Handles popup remove from DOM event */
onRemove?: () => void;
/** If true `Popup` act like a modal dialog */
modal?: boolean;
/** Element which focus should be returned to */
returnFocus?: PopupProps['returnFocus'];
/** If true `Sheet` is used instead of `Popup` */
isMobile?: boolean;
}
Expand All @@ -36,6 +40,8 @@ export function PickerDialog({
popupPlacement,
popupOffset,
modal,
returnFocus = false,
onRemove,
...props
}: PickerDialogProps) {
if (isMobile) {
Expand All @@ -44,6 +50,9 @@ export function PickerDialog({
visible={open}
onClose={() => {
onClose('outside-press');
setTimeout(() => {
onRemove?.();
});
}}
contentClassName={b('content', {mobile: true, size: 'xl'}, popupClassName)}
>
Expand Down Expand Up @@ -73,6 +82,8 @@ export function PickerDialog({
className={b('content', {size: props.size}, popupClassName)}
style={popupStyle}
modal={modal}
returnFocus={returnFocus}
onTransitionOutComplete={onRemove}
>
<PickerForm
{...props}
Expand Down
1 change: 1 addition & 0 deletions src/components/RelativeRangeDatePicker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface RelativeRangeDatePickerRenderControlProps {
errorMessage?: React.ReactNode;
validationState?: 'invalid';
open: boolean;
setOpen: (open: boolean) => void;
triggerProps: RelativeRangeDatePickerTriggerProps;
}

Expand Down
Loading