11import React from 'react' ;
22
3- import {
4- type ButtonProps ,
5- type PopupProps ,
6- type TextInputProps ,
7- useForkRef ,
8- } from '@gravity-ui/uikit' ;
9-
10- import type { CalendarProps } from '../../Calendar' ;
11- import { type DateFieldProps , useDateFieldProps } from '../../DateField' ;
3+ import { useFocusWithin , useForkRef } from '@gravity-ui/uikit' ;
4+ import type { ButtonProps , PopupProps , TextInputProps } from '@gravity-ui/uikit' ;
5+
6+ import type { Calendar , CalendarInstance } from '../../Calendar' ;
7+ import { useDateFieldProps } from '../../DateField' ;
8+ import type { DateFieldProps } from '../../DateField' ;
129import { getButtonSizeForInput } from '../../utils/getButtonSizeForInput' ;
1310import { mergeProps } from '../../utils/mergeProps' ;
1411import type { RelativeDatePickerProps } from '../RelativeDatePicker' ;
@@ -22,13 +19,13 @@ interface InnerRelativeDatePickerProps {
2219 modeSwitcherProps : ButtonProps ;
2320 calendarButtonProps : ButtonProps ;
2421 popupProps : PopupProps ;
25- calendarProps : CalendarProps ;
22+ calendarProps : React . ComponentProps < typeof Calendar > ;
2623 timeInputProps : DateFieldProps ;
2724}
2825
2926export function useRelativeDatePickerProps (
3027 state : RelativeDatePickerState ,
31- props : RelativeDatePickerProps ,
28+ { onFocus , onBlur , ... props } : RelativeDatePickerProps ,
3229) : InnerRelativeDatePickerProps {
3330 const { mode, setMode, datePickerState, dateFieldState, relativeDateState} = state ;
3431
@@ -47,6 +44,15 @@ export function useRelativeDatePickerProps(
4744 setFocusedDate ( datePickerState . dateValue ) ;
4845 }
4946 }
47+ const { focusWithinProps} = useFocusWithin ( {
48+ onFocusWithin : onFocus ,
49+ onBlurWithin : onBlur ,
50+ onFocusWithinChange ( isFocusWithin ) {
51+ if ( ! isFocusWithin ) {
52+ state . setActive ( isFocusWithin ) ;
53+ }
54+ } ,
55+ } ) ;
5056
5157 const [ isOpen , setOpen ] = React . useState ( false ) ;
5258 if ( ! state . isActive && isOpen ) {
@@ -61,7 +67,11 @@ export function useRelativeDatePickerProps(
6167 }
6268 }
6369
64- const commonInputProps : TextInputProps = { } ;
70+ const commonInputProps : TextInputProps = {
71+ onFocus : ( ) => {
72+ state . setActive ( true ) ;
73+ } ,
74+ } ;
6575
6676 const { inputProps} = useDateFieldProps ( dateFieldState , {
6777 ...props ,
@@ -98,9 +108,12 @@ export function useRelativeDatePickerProps(
98108 mode === 'relative' ? relativeDateProps . controlRef : inputProps . controlRef ,
99109 ) ;
100110
111+ const calendarRef = React . useRef < CalendarInstance > ( null ) ;
112+
101113 return {
102114 groupProps : {
103115 role : 'group' ,
116+ ...focusWithinProps ,
104117 } ,
105118 fieldProps : mergeProps (
106119 commonInputProps ,
@@ -142,8 +155,14 @@ export function useRelativeDatePickerProps(
142155 wasActiveBeforeClickRef . current = state . isActive ;
143156 } ,
144157 onClick : ( ) => {
158+ state . setActive ( true ) ;
145159 if ( wasActiveBeforeClickRef . current ) {
146160 setOpen ( ! isOpen ) ;
161+ if ( ! isOpen ) {
162+ setTimeout ( ( ) => {
163+ calendarRef . current ?. focus ( ) ;
164+ } ) ;
165+ }
147166 }
148167 wasActiveBeforeClickRef . current = state . isActive ;
149168 } ,
@@ -156,6 +175,7 @@ export function useRelativeDatePickerProps(
156175 restoreFocus : true ,
157176 } ,
158177 calendarProps : {
178+ ref : calendarRef ,
159179 size : props . size === 's' ? 'm' : props . size ,
160180 readOnly : props . readOnly ,
161181 value : state . selectedDate ,
0 commit comments