From 05236fff1447d8e915b1a56c99893be404c15b2a Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 12 Feb 2022 15:40:45 -0500 Subject: [PATCH] Add month navigation --- src/App.tsx | 16 ++++++------ src/Calendar.scss | 28 +++++++++++++++++++-- src/Calendar.tsx | 62 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 08fa28c..51c77f8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,14 +1,16 @@ -import React from 'react'; +import React, {useState} from 'react'; import './App.css'; import {Calendar} from './Calendar'; -import {gregorianJDN} from "./dates"; +import {gregorianJDN, Month} from "./dates"; function App() { - const today = new Date(); - const todayJDN = gregorianJDN(today.getFullYear(), today.getMonth() + 1, today.getDay()); - return ( - - ); + const today = new Date(); + const todayJDN = gregorianJDN(today.getFullYear(), today.getMonth() + 1, today.getDay()); + const [yearMonth, setYearMonth] = useState([230, 5]); + return ( + setYearMonth([year, month])}/> + ); } export default App; diff --git a/src/Calendar.scss b/src/Calendar.scss index c99c17e..db70794 100644 --- a/src/Calendar.scss +++ b/src/Calendar.scss @@ -1,12 +1,36 @@ @import 'bootstrap/scss/functions'; @import 'bootstrap/scss/variables'; @import 'bootstrap/scss/mixins'; -@import 'bootstrap/scss/containers'; +@import 'bootstrap/scss/grid'; +@import 'bootstrap/scss/buttons'; -.Month { +.Calendar { @include make-container(); } +.Calendar-head { + display: flex; + font-size: 2em; + font-weight: 600; + + .btn { + margin: 0 1px; + } +} + +.Calendar-prev, .Calendar-next { + flex: 0 70px; +} + +.Calendar-prev { + text-align: right; +} + +.Calendar-month-name { + flex: 1; + text-align: center; +} + .Month-decadeHead { display: none; } diff --git a/src/Calendar.tsx b/src/Calendar.tsx index c5f220e..539a636 100644 --- a/src/Calendar.tsx +++ b/src/Calendar.tsx @@ -1,6 +1,6 @@ import React from 'react'; import './Calendar.scss'; -import {Day, decadeNames, frJDN, jdnGregorian, jdnLongCount, Month} from './dates'; +import {Day, decadeNames, frJDN, jdnGregorian, jdnLongCount, Month, monthName} from './dates'; type MonthProps = { year: number; @@ -11,11 +11,9 @@ type DateProps = MonthProps & { day: Day; }; -export type CalendarProps = DateProps & { - year: number; - month: Month; +export type CalendarProps = MonthProps & { todayJDN: number; - onSwitch?: (year: number, month: Month, day: Day) => void, + onSwitch?: (year: number, month: Month) => void, }; type CalendarState = {}; @@ -33,7 +31,6 @@ function DayDetail({jdn}: { jdn: number }): JSX.Element { function NormalDay({year, month, day, todayJDN}: DateProps & { todayJDN: number }): JSX.Element { const jdn = frJDN(year, month, day); - console.log(year, month, day, jdn, todayJDN); return
{day}
{decadeNames[(day - 1) % 10]}
@@ -57,7 +54,58 @@ function NormalMonth({year, month, todayJDN}: MonthProps & { todayJDN: number }) } export class Calendar extends React.Component { + private goToNormalized(year: number, month: number) { + if (month < 0) { + --year; + month += 13; + } + + if (month > 13) { + ++year; + month -= 13; + } + + this.props.onSwitch && this.props.onSwitch(year, month as Month); + } + + prevYear() { + this.goToNormalized(this.props.year - 1, this.props.month); + } + + prevMonth() { + this.goToNormalized(this.props.year, this.props.month - 1); + } + + nextYear() { + this.goToNormalized(this.props.year + 1, this.props.month); + } + + nextMonth() { + this.goToNormalized(this.props.year, this.props.month + 1); + } + render(): JSX.Element { - return ; + return
+
+
+ + +
+
{monthName(this.props.month)} {this.props.year}
+
+ + +
+
+ +
; } }