Skip to content

Commit 8270119

Browse files
authored
feat(RelativePickers): add onOpenChange event handler (#118)
1 parent fcab830 commit 8270119

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

src/components/RelativeDatePicker/RelativeDatePicker.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export interface RelativeDatePickerProps
4444
StyleProps,
4545
AccessibilityProps {
4646
children?: (props: CalendarProps) => React.ReactNode;
47+
/** Handler that is called when the popup's open state changes. */
48+
onOpenChange?: (open: boolean) => void;
4749
}
4850

4951
export function RelativeDatePicker(props: RelativeDatePickerProps) {

src/components/RelativeDatePicker/__stories__/RelativeDatePicker.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const meta: Meta<typeof RelativeDatePicker> = {
1919
args: {
2020
onFocus: action('onFocus'),
2121
onBlur: action('onBlur'),
22+
onOpenChange: action('onOpenChange'),
2223
},
2324
};
2425

src/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import {useFocusWithin, useForkRef} from '@gravity-ui/uikit';
3+
import {useControlledState, useFocusWithin, useForkRef} from '@gravity-ui/uikit';
44
import type {ButtonProps, PopupProps, TextInputProps} from '@gravity-ui/uikit';
55

66
import type {Calendar, CalendarInstance} from '../../Calendar';
@@ -59,7 +59,7 @@ export function useRelativeDatePickerProps(
5959
},
6060
});
6161

62-
const [isOpen, setOpen] = React.useState(false);
62+
const [isOpen, setOpen] = useControlledState<boolean>(undefined, false, props.onOpenChange);
6363
if (!state.isActive && isOpen) {
6464
setOpen(false);
6565
}

src/components/RelativeRangeDatePicker/RelativeRangeDatePicker.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import React from 'react';
44

55
import type {DateTime} from '@gravity-ui/date-utils';
66
import {Calendar as CalendarIcon} from '@gravity-ui/icons';
7-
import {Button, Icon, TextInput, useFocusWithin, useMobile} from '@gravity-ui/uikit';
7+
import {
8+
Button,
9+
Icon,
10+
TextInput,
11+
useControlledState,
12+
useFocusWithin,
13+
useMobile,
14+
} from '@gravity-ui/uikit';
815

916
import {block} from '../../utils/cn';
1017
import {HiddenInput} from '../HiddenInput/HiddenInput';
@@ -61,6 +68,8 @@ export interface RelativeRangeDatePickerProps
6168
getRangeTitle?: (value: RangeValue<Value | null> | null, timeZone: string) => string;
6269
/** Sets the CSS className for the popup element. */
6370
popupClassName?: string;
71+
/** Handler that is called when the popup's open state changes. */
72+
onOpenChange?: (open: boolean) => void;
6473
}
6574

6675
export function RelativeRangeDatePicker(props: RelativeRangeDatePickerProps) {
@@ -72,7 +81,7 @@ export function RelativeRangeDatePicker(props: RelativeRangeDatePickerProps) {
7281
const inputRef = React.useRef<HTMLInputElement>(null);
7382

7483
const [isActive, setIsActive] = React.useState(false);
75-
const [open, setOpen] = React.useState(false);
84+
const [open, setOpen] = useControlledState<boolean>(undefined, false, props.onOpenChange);
7685

7786
const {focusWithinProps} = useFocusWithin({
7887
isDisabled: props.disabled,

src/components/RelativeRangeDatePicker/__stories__/RelativeRangeDatePiker.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const meta: Meta<typeof RelativeRangeDatePicker> = {
1414
args: {
1515
onFocus: action('onFocus'),
1616
onBlur: action('onBlur'),
17+
onOpenChange: action('onOpenChange'),
1718
},
1819
};
1920

0 commit comments

Comments
 (0)