1
1
import React from 'react' ;
2
2
3
- import { TextInput , useFocusWithin , useMobile } from '@gravity-ui/uikit' ;
3
+ import { Calendar as CalendarIcon } from '@gravity-ui/icons' ;
4
+ import { Button , Icon , Popup , TextInput , useMobile } from '@gravity-ui/uikit' ;
4
5
5
- import type { CalendarProps } from '../Calendar' ;
6
- import { useDateFieldProps , useDateFieldState } from '../DateField' ;
6
+ import { Calendar , type CalendarProps } from '../Calendar' ;
7
+ import { DateField } from '../DateField' ;
7
8
import type {
8
9
AccessibilityProps ,
9
10
DateFieldBase ,
@@ -14,8 +15,8 @@ import type {
14
15
TextInputProps ,
15
16
} from '../types' ;
16
17
17
- import { DesktopCalendar , DesktopCalendarButton } from './DesktopCalendar' ;
18
18
import { MobileCalendar , MobileCalendarIcon } from './MobileCalendar' ;
19
+ import { useDatePickerProps } from './hooks/useDatePickerProps' ;
19
20
import { useDatePickerState } from './hooks/useDatePickerState' ;
20
21
import { b } from './utils' ;
21
22
@@ -32,16 +33,7 @@ export interface DatePickerProps
32
33
children ?: ( props : CalendarProps ) => React . ReactNode ;
33
34
}
34
35
35
- export function DatePicker ( {
36
- value,
37
- defaultValue,
38
- onUpdate,
39
- className,
40
- onFocus,
41
- onBlur,
42
- children,
43
- ...props
44
- } : DatePickerProps ) {
36
+ export function DatePicker ( { value, defaultValue, onUpdate, className, ...props } : DatePickerProps ) {
45
37
const anchorRef = React . useRef < HTMLDivElement > ( null ) ;
46
38
47
39
const state = useDatePickerState ( {
@@ -51,70 +43,44 @@ export function DatePicker({
51
43
onUpdate,
52
44
} ) ;
53
45
54
- const [ isMobile ] = useMobile ( ) ;
55
-
56
- const [ isActive , setActive ] = React . useState ( false ) ;
57
- const { focusWithinProps} = useFocusWithin ( {
58
- onFocusWithin : onFocus ,
59
- onBlurWithin : onBlur ,
60
- onFocusWithinChange ( isFocusWithin ) {
61
- setActive ( isFocusWithin ) ;
62
- } ,
63
- } ) ;
46
+ const { groupProps, fieldProps, calendarButtonProps, popupProps, calendarProps, timeInputProps} =
47
+ useDatePickerProps ( state , props ) ;
64
48
65
- const fieldState = useDateFieldState ( {
66
- value : state . value ,
67
- onUpdate : state . setValue ,
68
- disabled : state . disabled ,
69
- readOnly : state . readOnly ,
70
- validationState : props . validationState ,
71
- minValue : props . minValue ,
72
- maxValue : props . maxValue ,
73
- isDateUnavailable : props . isDateUnavailable ,
74
- format : state . format ,
75
- placeholderValue : props . placeholderValue ,
76
- timeZone : props . timeZone ,
77
- } ) ;
78
-
79
- const { inputProps} = useDateFieldProps ( fieldState , props ) ;
49
+ const [ isMobile ] = useMobile ( ) ;
80
50
81
51
return (
82
- // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
83
- < div
84
- ref = { anchorRef }
85
- className = { b ( null , className ) }
86
- style = { props . style }
87
- { ...focusWithinProps }
88
- role = "group"
89
- aria-disabled = { state . disabled || undefined }
90
- onKeyDown = { ( e ) => {
91
- if ( e . altKey && ( e . key === 'ArrowDown' || e . key === 'ArrowUp' ) ) {
92
- e . preventDefault ( ) ;
93
- e . stopPropagation ( ) ;
94
- state . setOpen ( true ) ;
95
- }
96
- } }
97
- >
52
+ < div className = { b ( null , className ) } { ...groupProps } >
98
53
{ isMobile ? (
99
54
< MobileCalendar props = { props } state = { state } />
100
55
) : (
101
- < DesktopCalendar
102
- anchorRef = { anchorRef }
103
- props = { props }
104
- state = { state }
105
- renderCalendar = { children }
106
- />
56
+ < div ref = { anchorRef } className = { b ( 'popup-anchor' ) } >
57
+ < Popup anchorRef = { anchorRef } { ...popupProps } >
58
+ < div className = { b ( 'popup-content' ) } >
59
+ { typeof props . children === 'function' ? (
60
+ props . children ( calendarProps )
61
+ ) : (
62
+ < Calendar { ...calendarProps } />
63
+ ) }
64
+ { state . hasTime && (
65
+ < div className = { b ( 'time-field-wrapper' ) } >
66
+ < DateField { ...timeInputProps } />
67
+ </ div >
68
+ ) }
69
+ </ div >
70
+ </ Popup >
71
+ </ div >
107
72
) }
108
73
< TextInput
109
- { ...inputProps }
110
- value = { fieldState . isEmpty && ! isActive && props . placeholder ? '' : inputProps . value }
74
+ { ...fieldProps }
111
75
className = { b ( 'field' , { mobile : isMobile } ) }
112
- hasClear = { ! isMobile && inputProps . hasClear }
76
+ hasClear = { ! isMobile && fieldProps . hasClear }
113
77
rightContent = {
114
78
isMobile ? (
115
79
< MobileCalendarIcon props = { props } state = { state } />
116
80
) : (
117
- < DesktopCalendarButton props = { props } state = { state } />
81
+ < Button { ...calendarButtonProps } >
82
+ < Icon data = { CalendarIcon } />
83
+ </ Button >
118
84
)
119
85
}
120
86
/>
0 commit comments