@@ -6,6 +6,7 @@ import DatePicker from "react-datepicker";
66import NoteInfo from "services/NoteInfo" ;
77import { Portal } from "@material-ui/core" ;
88import { CalendarProps } from "cdm/DatabaseModel" ;
9+ import { c } from "helpers/StylesHelper" ;
910
1011const CalendarPortal = ( calendarProps : CalendarProps ) => {
1112 const { column, cellProperties } = calendarProps ;
@@ -15,14 +16,14 @@ const CalendarPortal = (calendarProps: CalendarProps) => {
1516 // Selector popper state
1617 /** state of cell value */
1718 const { contextValue, setContextValue } = useContext ( CellContext ) ;
18-
19+ const [ showDatePicker , setShowDatePicker ] = useState ( false ) ;
1920 /** Note info of current Cell */
2021 const note : NoteInfo = ( cellProperties . row . original as any ) . note ;
21- const [ calendarState , setCalendarState ] = useState (
22- DateTime . isDateTime ( contextValue . value )
23- ? contextValue . value . toJSDate ( )
24- : null
25- ) ;
22+
23+ function handleOnClick ( event : any ) {
24+ event . preventDefault ( ) ;
25+ setShowDatePicker ( true ) ;
26+ }
2627
2728 function handleCalendarChange ( date : Date ) {
2829 const newValue = DateTime . fromJSDate ( date ) ;
@@ -31,33 +32,43 @@ const CalendarPortal = (calendarProps: CalendarProps) => {
3132 type : ActionTypes . UPDATE_CELL ,
3233 file : note . getFile ( ) ,
3334 key : column . key ,
34- value : DateTime . fromJSDate ( date ) . toFormat ( "yyyy-MM-dd" ) ,
35+ value : newValue . toFormat ( "yyyy-MM-dd" ) ,
3536 row : cellProperties . row ,
3637 columnId : column . id ,
3738 } ) ;
3839
39- setCalendarState ( date ) ;
4040 setContextValue ( {
4141 value : newValue ,
4242 update : true ,
4343 } ) ;
44+ setShowDatePicker ( false ) ;
4445 }
4546
4647 const CalendarContainer = ( containerProps : any ) => {
4748 const el = document . getElementById ( "popper-container" ) ;
4849 return < Portal container = { el } > { containerProps . children } </ Portal > ;
4950 } ;
5051
51- return (
52- < div className = "data-input calendar" >
53- < DatePicker
54- dateFormat = "yyyy-MM-dd"
55- selected = { calendarState }
56- onChange = { handleCalendarChange }
57- popperContainer = { CalendarContainer }
58- placeholderText = "Pick a date..."
59- />
60- </ div >
52+ return showDatePicker ? (
53+ < DatePicker
54+ dateFormat = "yyyy-MM-dd"
55+ selected = {
56+ DateTime . isDateTime ( contextValue . value )
57+ ? contextValue . value . toJSDate ( )
58+ : null
59+ }
60+ onChange = { handleCalendarChange }
61+ popperContainer = { CalendarContainer }
62+ onBlur = { ( ) => setShowDatePicker ( false ) }
63+ autoFocus
64+ className = "data-input calendar"
65+ />
66+ ) : (
67+ < span className = { `data-input ${ c ( "calendar" ) } ` } onClick = { handleOnClick } >
68+ { DateTime . isDateTime ( contextValue . value )
69+ ? contextValue . value . toFormat ( "yyyy-MM-dd" )
70+ : "Pick a date..." }
71+ </ span >
6172 ) ;
6273} ;
6374
0 commit comments