Factor out abstract month-based calendar UI

This commit is contained in:
Quantum 2023-07-15 14:51:51 -04:00
parent 8c238c6625
commit f67bd1bbef
3 changed files with 241 additions and 221 deletions

View file

@ -0,0 +1,153 @@
import React from 'react';
export type CalendarProps<Year, Month> = {
year: Year;
month: Month;
todayJDN: number;
onSwitch?: (year: Year, month: Month) => void,
};
type CalendarState = {
selecting: boolean,
yearStr: string,
};
export abstract class MonthBasedCalendar<Year, Month> extends React.Component<CalendarProps<Year, Month>, CalendarState> {
selection: React.RefObject<HTMLDivElement>;
protected constructor(props: CalendarProps<Year, Month>) {
super(props);
this.state = {
selecting: false,
yearStr: this.yearToString(props.year),
};
this.selection = React.createRef();
}
componentDidMount() {
document.addEventListener('click', this.handleClickOutside, true);
}
componentDidUpdate(prevProps: CalendarProps<Year, Month>) {
if (prevProps.year !== this.props.year) {
const yearStr = this.yearToString(this.props.year);
if (this.state.yearStr !== yearStr) {
this.setState({yearStr});
}
}
}
componentWillUnmount() {
document.removeEventListener('click', this.handleClickOutside, true);
}
goTo(year: Year, month: Month) {
this.props.onSwitch?.(year, month);
}
abstract parseYear(year: string): Year;
abstract parseMonth(month: string): Month;
abstract yearToString(year: Year): string;
abstract monthToString(month: Month): string;
abstract prevYear(): void;
abstract prevMonth(): void;
abstract nextYear(): void;
abstract nextMonth(): void;
abstract isValidYear(year: string): boolean;
abstract jdnLookup(jdn: number): {year: Year, month: Month};
abstract monthName(year: Year, month: Month): string;
startSelection = () => {
this.setState({selecting: true});
};
handleClickOutside = (event: any) => {
if (this.state.selecting && this.selection.current && !this.selection.current.contains(event.target))
this.setState({selecting: false});
};
handleKeyUp = (event: any) => {
if (event.key === 'Escape')
this.setState({selecting: false});
};
monthChange = (event: any) => {
this.goTo(this.props.year, this.parseMonth(event.target.value));
};
yearChange = (event: any) => {
if (this.isValidYear(event.target.value)) {
this.goTo(this.parseYear(event.target.value), this.props.month);
}
this.setState({yearStr: event.target.value});
};
goToToday = () => {
const {year, month} = this.jdnLookup(this.props.todayJDN);
this.goTo(year, month);
this.setState({selecting: false});
};
abstract renderMonthOptions(): JSX.Element[];
abstract renderBody(): JSX.Element;
renderPrevArrows(): JSX.Element {
return <div className="Calendar-prev">
<button type="button" className="btn btn-secondary" title="Previous year" onClick={this.prevYear}>«</button>
<button type="button" className="btn btn-secondary" title="Previous month" onClick={this.prevMonth}>
</button>
</div>;
}
renderNextArrows(): JSX.Element {
return <div className="Calendar-next">
<button type="button" className="btn btn-secondary" title="Next month" onClick={this.nextMonth}></button>
<button type="button" className="btn btn-secondary" title="Next year" onClick={this.nextYear}>»</button>
</div>;
}
renderMonthName(): JSX.Element {
const {year, month} = this.props;
return <div className="Calendar-month-name" onClick={this.startSelection}>{this.monthName(year, month)}</div>;
}
renderMonthSelection(): JSX.Element {
return <div className="Calendar-month-name input-group" ref={this.selection} onKeyUp={this.handleKeyUp}>
<select className="Calendar-month-input form-control" onChange={this.monthChange}
value={this.monthToString(this.props.month)}>
{this.renderMonthOptions()}
</select>
<input type="number" className="Calendar-year-input form-control" value={this.state.yearStr}
onChange={this.yearChange}/>
<button type="button" className="form-control btn btn-primary Calendar-today-button"
onClick={this.goToToday}>Today
</button>
</div>;
}
renderHead(): JSX.Element {
return <div className="Calendar-head">
{this.renderPrevArrows()}
{this.state.selecting ? this.renderMonthSelection() : this.renderMonthName()}
{this.renderNextArrows()}
</div>;
}
render(): JSX.Element {
return <div className="Calendar">
{this.renderHead()}
{this.renderBody()}
</div>;
}
}

View file

@ -4,21 +4,24 @@ import {
dateName, dateName,
dateRuralName, dateRuralName,
decadeNames, decadeNames,
frEndYear,
FrenchDay, FrenchDay,
FrenchMonth, FrenchMonth,
frEndYear,
frIsLeap, frIsLeap,
frJDN, frJDN,
frStartYear,
jdnFrench, jdnFrench,
monthName, monthName,
frStartYear,
} from '@common/french'; } from '@common/french';
import {jdnDate} from '@common/gregorian'; import {jdnDate} from '@common/gregorian';
import {jdnLongCount} from '@common/longCount'; import {jdnLongCount} from '@common/longCount';
import {useMobileTooltipProps} from '@common/MobileTooltip'; import {useMobileTooltipProps} from '@common/MobileTooltip';
import {MonthBasedCalendar} from '@common/MonthBasedCalendar';
type FrenchYear = number;
type MonthProps = { type MonthProps = {
year: number; year: FrenchYear;
month: FrenchMonth; month: FrenchMonth;
}; };
@ -73,7 +76,7 @@ function ComplementaryDay({year, month, day, todayJDN}: DateProps & { todayJDN:
</div>; </div>;
} }
function ComplementaryDays({year, todayJDN}: { year: number, todayJDN: number }): JSX.Element { function ComplementaryDays({year, todayJDN}: { year: FrenchYear, todayJDN: number }): JSX.Element {
const leap = frIsLeap(year); const leap = frIsLeap(year);
return <div className="ComplementaryDays">{ return <div className="ComplementaryDays">{
Array.from(Array(6).keys()).map(i => <React.Fragment key={i}> Array.from(Array(6).keys()).map(i => <React.Fragment key={i}>
@ -84,43 +87,30 @@ function ComplementaryDays({year, todayJDN}: { year: number, todayJDN: number })
}</div>; }</div>;
} }
export type CalendarProps = MonthProps & { export class Calendar extends MonthBasedCalendar<FrenchYear, FrenchMonth> {
todayJDN: number; override parseYear(year: string): FrenchYear {
onSwitch?: (year: number, month: FrenchMonth) => void, return +year;
};
type CalendarState = {
selecting: boolean,
yearStr: string,
};
export class Calendar extends React.Component<CalendarProps, CalendarState> {
selection: React.RefObject<HTMLDivElement>;
constructor(props: CalendarProps) {
super(props);
this.state = {
selecting: false,
yearStr: this.props.year.toString(),
};
this.selection = React.createRef();
} }
componentDidMount() { override parseMonth(month: string): FrenchMonth {
document.addEventListener('click', this.handleClickOutside, true); return +month as FrenchMonth;
} }
componentWillUnmount() { override yearToString(year: FrenchYear): string {
document.removeEventListener('click', this.handleClickOutside, true); return year.toString();
}
override monthToString(month: FrenchMonth): string {
return month.toString();
} }
private goToNormalized(year: number, month: number) { private goToNormalized(year: number, month: number) {
if (month < 1) { while (month < 1) {
--year; --year;
month += 13; month += 13;
} }
if (month > 13) { while (month > 13) {
++year; ++year;
month -= 13; month -= 13;
} }
@ -133,105 +123,49 @@ export class Calendar extends React.Component<CalendarProps, CalendarState> {
month = 13; month = 13;
} }
this.props.onSwitch && this.props.onSwitch(year, month as FrenchMonth); this.goTo(year, month as FrenchMonth);
} }
prevYear = () => { override prevYear = () => {
this.goToNormalized(this.props.year - 1, this.props.month); this.goToNormalized(this.props.year - 1, this.props.month);
} };
prevMonth = () => { override prevMonth = () => {
this.goToNormalized(this.props.year, this.props.month - 1); this.goToNormalized(this.props.year, this.props.month - 1);
} };
nextYear = () => { override nextYear = () => {
this.goToNormalized(this.props.year + 1, this.props.month); this.goToNormalized(this.props.year + 1, this.props.month);
} };
nextMonth = () => { override nextMonth = () => {
this.goToNormalized(this.props.year, this.props.month + 1); this.goToNormalized(this.props.year, this.props.month + 1);
};
override isValidYear(year: string): boolean {
return /^-?\d+/.test(year);
} }
startSelection = () => { override jdnLookup(jdn: number): { year: FrenchYear; month: FrenchMonth } {
this.setState({selecting: true}); return jdnFrench(jdn);
} }
handleClickOutside = (event: any) => { override monthName(year: FrenchYear, month: FrenchMonth): string {
if (this.state.selecting && this.selection.current && !this.selection.current.contains(event.target)) return month === 13 ? year.toString() : `${monthName(month)} ${year}`;
this.setState({selecting: false});
} }
handleKeyUp = (event: any) => { override renderMonthOptions(): JSX.Element[] {
if (event.key === 'Escape') return Array.from(Array(13).keys()).map(i => {
this.setState({selecting: false});
}
monthChange = (event: any) => {
this.goToNormalized(this.props.year, +event.target.value as FrenchMonth);
}
yearChange = (event: any) => {
if (/^-?\d+/.test(event.target.value)) {
this.goToNormalized(+event.target.value, this.props.month);
}
this.setState({yearStr: event.target.value});
}
goToToday = () => {
const {year, month} = jdnFrench(this.props.todayJDN);
this.goToNormalized(year, month);
this.setState({selecting: false});
}
componentDidUpdate(prevProps: CalendarProps) {
if (prevProps.year !== this.props.year) {
const yearStr = this.props.year.toString();
if (this.state.yearStr !== yearStr) {
this.setState({
yearStr: yearStr,
});
}
}
}
render(): JSX.Element {
return <div className="Calendar">
<div className="Calendar-head">
<div className="Calendar-prev">
<button type="button" className="btn btn-secondary" title="Previous year" onClick={this.prevYear}>«
</button>
<button type="button" className="btn btn-secondary" title="Previous month"
onClick={this.prevMonth}>
</button>
</div>
{!this.state.selecting && <div className="Calendar-month-name" onClick={this.startSelection}>
{this.props.month < 13 && monthName(this.props.month)} {this.props.year}
</div>}
{this.state.selecting && <div className="Calendar-month-name input-group" ref={this.selection}
onKeyUp={this.handleKeyUp}>
<select className="Calendar-month-input form-control" onChange={this.monthChange}
value={this.props.month}>{
Array.from(Array(13).keys()).map(i => {
const month = i + 1 as FrenchMonth; const month = i + 1 as FrenchMonth;
return <option key={i} value={month}>{monthName(month)}</option>; return <option key={i} value={month}>{monthName(month)}</option>;
}) });
}</select> }
<input type="number" className="Calendar-year-input form-control" value={this.state.yearStr}
onChange={this.yearChange} min={frStartYear} max={frEndYear}/> override renderBody(): JSX.Element {
<button type="button" className="form-control btn btn-primary Calendar-today-button" if (this.props.month < 13) {
onClick={this.goToToday}>Today return <NormalMonth year={this.props.year} month={this.props.month} todayJDN={this.props.todayJDN}/>;
</button> } else {
</div>} return <ComplementaryDays year={this.props.year} todayJDN={this.props.todayJDN}/>;
<div className="Calendar-next"> }
<button type="button" className="btn btn-secondary" title="Next month" onClick={this.nextMonth}>
</button>
<button type="button" className="btn btn-secondary" title="Next year" onClick={this.nextYear}>»
</button>
</div>
</div>
{this.props.month < 13 &&
<NormalMonth year={this.props.year} month={this.props.month} todayJDN={this.props.todayJDN}/>}
{this.props.month === 13 && <ComplementaryDays year={this.props.year} todayJDN={this.props.todayJDN}/>}
</div>;
} }
} }

View file

@ -5,9 +5,12 @@ import {jdnLongCount} from '@common/longCount';
import {jdnJulian, julianJDN, julianMonthDays} from '@common/julian'; import {jdnJulian, julianJDN, julianMonthDays} from '@common/julian';
import {frDateFormat, frEndJD, frStartJD, jdnFrench} from '@common/french'; import {frDateFormat, frEndJD, frStartJD, jdnFrench} from '@common/french';
import {useMobileTooltipProps} from '@common/MobileTooltip'; import {useMobileTooltipProps} from '@common/MobileTooltip';
import {MonthBasedCalendar} from '@common/MonthBasedCalendar';
type JulianYear = number;
type MonthProps = { type MonthProps = {
year: number; year: JulianYear;
month: JulianMonth; month: JulianMonth;
}; };
@ -72,144 +75,74 @@ function Month({year, month, todayJDN}: MonthProps & { todayJDN: number }): JSX.
</div>; </div>;
} }
export type CalendarProps = MonthProps & { export class Calendar extends MonthBasedCalendar<JulianYear, JulianMonth> {
todayJDN: number; override parseYear(year: string): JulianYear {
onSwitch?: (year: number, month: JulianMonth) => void, return +year;
};
type CalendarState = {
selecting: boolean,
yearStr: string,
};
export class Calendar extends React.Component<CalendarProps, CalendarState> {
selection: React.RefObject<HTMLDivElement>;
constructor(props: CalendarProps) {
super(props);
this.state = {
selecting: false,
yearStr: this.props.year.toString(),
};
this.selection = React.createRef();
} }
componentDidMount() { override parseMonth(month: string): JulianMonth {
document.addEventListener('click', this.handleClickOutside, true); return +month as JulianMonth;
} }
componentWillUnmount() { override yearToString(year: JulianYear): string {
document.removeEventListener('click', this.handleClickOutside, true); return year.toString();
}
override monthToString(month: JulianMonth): string {
return month.toString();
} }
private goToNormalized(year: number, month: number) { private goToNormalized(year: number, month: number) {
if (month < 1) { while (month < 1) {
--year; --year;
month += 12; month += 12;
} }
if (month > 12) { while (month > 12) {
++year; ++year;
month -= 12; month -= 12;
} }
this.props.onSwitch && this.props.onSwitch(year, month as JulianMonth); this.goTo(year, month as JulianMonth);
} }
prevYear = () => { override prevYear = () => {
this.goToNormalized(this.props.year - 1, this.props.month); this.goToNormalized(this.props.year - 1, this.props.month);
}; };
prevMonth = () => { override prevMonth = () => {
this.goToNormalized(this.props.year, this.props.month - 1); this.goToNormalized(this.props.year, this.props.month - 1);
}; };
nextYear = () => { override nextYear = () => {
this.goToNormalized(this.props.year + 1, this.props.month); this.goToNormalized(this.props.year + 1, this.props.month);
}; };
nextMonth = () => { override nextMonth = () => {
this.goToNormalized(this.props.year, this.props.month + 1); this.goToNormalized(this.props.year, this.props.month + 1);
}; };
startSelection = () => { override isValidYear(year: string): boolean {
this.setState({selecting: true}); return /^-?\d+/.test(year);
};
handleClickOutside = (event: any) => {
if (this.state.selecting && this.selection.current && !this.selection.current.contains(event.target))
this.setState({selecting: false});
};
handleKeyUp = (event: any) => {
if (event.key === 'Escape')
this.setState({selecting: false});
};
monthChange = (event: any) => {
this.goToNormalized(this.props.year, +event.target.value as JulianMonth);
};
yearChange = (event: any) => {
if (/^-?\d+/.test(event.target.value)) {
this.goToNormalized(+event.target.value, this.props.month);
}
this.setState({yearStr: event.target.value});
};
goToToday = () => {
const [year, month] = jdnJulian(this.props.todayJDN);
this.goToNormalized(year, month);
this.setState({selecting: false});
};
componentDidUpdate(prevProps: CalendarProps) {
if (prevProps.year !== this.props.year) {
const yearStr = this.props.year.toString();
if (this.state.yearStr !== yearStr) {
this.setState({
yearStr: yearStr,
});
}
}
} }
render(): JSX.Element { override jdnLookup(jdn: number): { year: JulianYear; month: JulianMonth } {
return <div className="Calendar"> const [year, month] = jdnJulian(jdn);
<div className="Calendar-head"> return {year, month};
<div className="Calendar-prev"> }
<button type="button" className="btn btn-secondary" title="Previous year" onClick={this.prevYear}>«
</button> override monthName(year: JulianYear, month: JulianMonth): string {
<button type="button" className="btn btn-secondary" title="Previous month" return `${monthName(month)} ${year}`;
onClick={this.prevMonth}> }
</button>
</div> override renderMonthOptions(): JSX.Element[] {
{!this.state.selecting && <div className="Calendar-month-name" onClick={this.startSelection}> return Array.from(Array(12).keys()).map(i => {
{monthName(this.props.month)} {this.props.year}
</div>}
{this.state.selecting && <div className="Calendar-month-name input-group" ref={this.selection}
onKeyUp={this.handleKeyUp}>
<select className="Calendar-month-input form-control" onChange={this.monthChange}
value={this.props.month}>{
Array.from(Array(12).keys()).map(i => {
const month = i + 1 as JulianMonth; const month = i + 1 as JulianMonth;
return <option key={i} value={month}>{monthName(month)}</option>; return <option key={i} value={month}>{monthName(month)}</option>;
}) });
}</select> }
<input type="number" className="Calendar-year-input form-control" value={this.state.yearStr}
onChange={this.yearChange}/> override renderBody(): JSX.Element {
<button type="button" className="form-control btn btn-primary Calendar-today-button" return <Month year={this.props.year} month={this.props.month} todayJDN={this.props.todayJDN}/>
onClick={this.goToToday}>Today
</button>
</div>}
<div className="Calendar-next">
<button type="button" className="btn btn-secondary" title="Next month" onClick={this.nextMonth}>
</button>
<button type="button" className="btn btn-secondary" title="Next year" onClick={this.nextYear}>»
</button>
</div>
</div>
<Month year={this.props.year} month={this.props.month} todayJDN={this.props.todayJDN}/>
</div>;
} }
} }