11import { TableColumn , TableDataType } from "cdm/FolderModel" ;
22import { CellContext } from "components/contexts/CellContext" ;
3- import { ActionTypes , DataTypes , StyleVariables } from "helpers/Constants" ;
3+ import { ActionTypes } from "helpers/Constants" ;
44import React , { useContext , useState } from "react" ;
5- import Calendar from "react-calendar" ;
6- import ReactDOM from "react-dom" ;
75import { DateTime } from "luxon" ;
8- import { usePopper } from "react-popper" ;
96import { Cell } from "react-table" ;
7+ import DatePicker from "react-datepicker" ;
108import NoteInfo from "services/NoteInfo" ;
11- import { DataviewService } from "services/DataviewService" ;
12- import { Literal } from "obsidian-dataview/lib/data-model/value" ;
9+ import { Portal } from "@material-ui/core" ;
1310
1411type CalendarProps = {
1512 intialState : TableDataType ;
@@ -19,102 +16,55 @@ type CalendarProps = {
1916const CalendarPortal = ( calendarProps : CalendarProps ) => {
2017 const { column, cellProperties } = calendarProps ;
2118 const dataDispatch = ( cellProperties as any ) . dataDispatch ;
22- const [ showCalendar , setShowCalendar ] = useState ( false ) ;
2319 // Selector reference state
24- const [ calendarRef , setCalendarRef ] = useState ( null ) ;
20+
2521 // Selector popper state
26- const [ selectPop , setSelectPop ] = useState ( null ) ;
27- const { styles, attributes } = usePopper ( calendarRef , selectPop ) ;
2822 /** state of cell value */
2923 const { contextValue, setContextValue } = useContext ( CellContext ) ;
3024
3125 /** Note info of current Cell */
3226 const note : NoteInfo = ( cellProperties . row . original as any ) . note ;
33- const [ calendarState , setCalendarState ] = useState ( null ) ;
27+ const [ calendarState , setCalendarState ] = useState (
28+ DateTime . isDateTime ( contextValue . value )
29+ ? contextValue . value . toJSDate ( )
30+ : null
31+ ) ;
3432
3533 function handleCalendarChange ( date : Date ) {
3634 const newValue = DateTime . fromJSDate ( date ) ;
3735 // save on disk
3836 dataDispatch ( {
3937 type : ActionTypes . UPDATE_CELL ,
4038 file : note . getFile ( ) ,
41- key : ( cellProperties . column as any ) . key ,
39+ key : column . key ,
4240 value : DateTime . fromJSDate ( date ) . toFormat ( "yyyy-MM-dd" ) ,
4341 row : cellProperties . row ,
44- columnId : ( cellProperties . column as any ) . id ,
42+ columnId : column . id ,
4543 } ) ;
4644
4745 setCalendarState ( date ) ;
48- setShowCalendar ( false ) ;
4946 setContextValue ( {
5047 value : newValue ,
5148 update : true ,
5249 } ) ;
5350 }
5451
55- function handlerOnClick ( e : any ) {
56- if ( ! column . isMetadata ) {
57- const portalActive = document . getElementById ( "unique-calendar-portal" ) ;
58- if ( ! showCalendar && portalActive !== null ) {
59- // Hacky way to trigger focus event on opened calendar
60- // react-calendar has a bug where it doesn't trigger focus event
61- portalActive . setAttribute ( "tabindex" , "-1" ) ;
62- portalActive . focus ( ) ;
63- portalActive . blur ( ) ;
64- }
65- // Check value when click on calendar to parse it if its not a date
66- setCalendarState (
67- DateTime . isDateTime ( contextValue . value )
68- ? contextValue . value . toJSDate ( )
69- : new Date ( )
70- ) ;
71- setShowCalendar ( ! showCalendar ) ;
72- }
73- }
74-
75- function displayCalendar ( ) {
76- return (
77- < div
78- className = "menu"
79- ref = { setSelectPop }
80- { ...attributes . popper }
81- style = { {
82- ...styles . popper ,
83- zIndex : 4 ,
84- minWidth : 200 ,
85- maxWidth : 320 ,
86- padding : "0.5rem" ,
87- background : StyleVariables . BACKGROUND_SECONDARY ,
88- } }
89- onMouseLeave = { ( ) => setShowCalendar ( false ) }
90- onBlur = { ( ) => setShowCalendar ( false ) }
91- id = { "unique-calendar-portal" }
92- >
93- < Calendar onChange = { handleCalendarChange } value = { calendarState } />
94- </ div >
95- ) ;
96- }
52+ const CalendarContainer = ( containerProps : any ) => {
53+ const el = document . getElementById ( "popper-container" ) ;
54+ return < Portal container = { el } > { containerProps . children } </ Portal > ;
55+ } ;
9756
9857 return (
99- < >
100- < div
101- className = "data-input calendar"
102- ref = { setCalendarRef }
103- onClick = { handlerOnClick }
104- >
105- < span >
106- { DataviewService . parseLiteral (
107- contextValue . value as Literal ,
108- DataTypes . TEXT
109- ) }
110- </ span >
111- </ div >
112- { showCalendar &&
113- ReactDOM . createPortal (
114- displayCalendar ( ) ,
115- document . getElementById ( "popper-container" )
116- ) }
117- </ >
58+ < div className = "data-input calendar" >
59+ < DatePicker
60+ dateFormat = "yyyy-MM-dd"
61+ selected = { calendarState }
62+ onChange = { handleCalendarChange }
63+ popperContainer = { CalendarContainer }
64+ placeholderText = "Pick a date..."
65+ />
66+ </ div >
11867 ) ;
11968} ;
69+
12070export default CalendarPortal ;
0 commit comments