@@ -11,8 +11,9 @@ export const DateEditor = ({
1111 api,
1212 autoOpenLastRow
1313} ) => {
14+ // Handle initial value properly - use null if value is falsy
1415 const [ selectedDate , setSelectedDate ] = useState (
15- value ? parseISO ( value ) : null
16+ value && value !== 'YYYY-MM-DD' ? parseISO ( value ) : null
1617 )
1718 const [ isOpen , setIsOpen ] = useState ( ( ) => {
1819 if ( ! autoOpenLastRow ) return false
@@ -31,18 +32,32 @@ export const DateEditor = ({
3132 }
3233 }
3334
34- document . addEventListener ( 'mousedown' , handleClickOutside )
35+ // Use the more cross-browser compatible approach
36+ document . addEventListener ( 'mousedown' , handleClickOutside , {
37+ passive : true
38+ } )
3539 return ( ) => {
3640 document . removeEventListener ( 'mousedown' , handleClickOutside )
3741 }
3842 } , [ ] )
3943
44+ // Fixed updateValue function to handle null values properly
4045 const updateValue = ( val ) => {
41- if ( val ) {
42- val = new Date ( val . getFullYear ( ) , val . getMonth ( ) , val . getDate ( ) )
46+ // If val is null or undefined, set it as null explicitly
47+ if ( val === null || val === undefined ) {
48+ setSelectedDate ( null )
49+ onValueChange ( null )
50+ return
4351 }
44- setSelectedDate ( val )
45- onValueChange ( val === null ? null : format ( val , 'yyyy-MM-dd' ) )
52+
53+ // Normalize the date to avoid timezone issues (common cross-browser problem)
54+ const normalizedDate = new Date (
55+ val . getFullYear ( ) ,
56+ val . getMonth ( ) ,
57+ val . getDate ( )
58+ )
59+ setSelectedDate ( normalizedDate )
60+ onValueChange ( format ( normalizedDate , 'yyyy-MM-dd' ) )
4661 }
4762
4863 const handleDatePickerOpen = ( ) => {
@@ -53,17 +68,29 @@ export const DateEditor = ({
5368 setIsOpen ( false )
5469 }
5570
71+ // Improved event handlers for better cross-browser support
5672 const stopPropagation = ( e ) => {
57- e . stopPropagation ( )
73+ if ( e && e . stopPropagation ) {
74+ e . stopPropagation ( )
75+ }
76+ if ( e && e . preventDefault ) {
77+ e . preventDefault ( )
78+ }
79+ return false
5880 }
5981
6082 // Handler for the icon click that forces the calendar to open
6183 const handleIconClick = ( e ) => {
62- e . stopPropagation ( )
63- e . preventDefault ( )
84+ stopPropagation ( e )
6485 setIsOpen ( true )
6586 }
6687
88+ // Explicit handler for clearing the date
89+ const handleClear = ( ) => {
90+ setSelectedDate ( null )
91+ onValueChange ( null )
92+ }
93+
6794 return (
6895 < div
6996 ref = { containerRef }
@@ -77,7 +104,8 @@ export const DateEditor = ({
77104 top : 0 ,
78105 left : 0 ,
79106 right : 0 ,
80- bottom : 0
107+ bottom : 0 ,
108+ zIndex : isOpen ? 1000 : 'auto'
81109 } }
82110 >
83111 < DatePicker
@@ -89,6 +117,7 @@ export const DateEditor = ({
89117 slotProps = { {
90118 field : {
91119 clearable : true ,
120+ onClear : handleClear ,
92121 sx : {
93122 width : '100%' ,
94123 '& .MuiInputBase-root' : {
@@ -97,12 +126,28 @@ export const DateEditor = ({
97126 } ,
98127 '& .MuiOutlinedInput-notchedOutline' : {
99128 border : 'none'
129+ } ,
130+ '& .MuiIconButton-root' : {
131+ padding : '2px' ,
132+ touchAction : 'manipulation'
100133 }
101134 }
102135 } ,
103- popper : { placement : 'bottom-start' } ,
136+ popper : {
137+ placement : 'bottom-start' ,
138+ modifiers : [
139+ {
140+ name : 'preventOverflow' ,
141+ options : {
142+ boundary : document . body
143+ }
144+ }
145+ ]
146+ } ,
104147 // Handle icon click specifically to open the calendar
105- openPickerButton : { onClick : handleIconClick }
148+ openPickerButton : { onClick : handleIconClick } ,
149+ // Explicitly handle the clear button
150+ clearButton : { onClick : handleClear }
106151 } }
107152 value = { selectedDate }
108153 onChange = { updateValue }
@@ -119,6 +164,14 @@ export const DateEditor = ({
119164 '& .MuiInputBase-root' : {
120165 padding : '0 5px' ,
121166 width : '100%'
167+ } ,
168+ '& .MuiButtonBase-root' : {
169+ WebkitTouchCallout : 'none' ,
170+ WebkitUserSelect : 'none' ,
171+ KhtmlUserSelect : 'none' ,
172+ MozUserSelect : 'none' ,
173+ msUserSelect : 'none' ,
174+ userSelect : 'none'
122175 }
123176 } }
124177 />
0 commit comments