import * as moment from "moment"; import * as React from "react"; import { Form, Icon, Input, InputOnChangeData } from "semantic-ui-react"; import { DateOfYear } from "@common/sprinklersRpc"; const HTML_DATE_INPUT_FORMAT = "YYYY-MM-DD"; export interface ScheduleDateProps { date: DateOfYear | null | undefined; label: string | React.ReactNode | undefined; editing: boolean | undefined; onChange: (newDate: DateOfYear | null) => void; } interface ScheduleDateState { rawValue: string | ""; lastDate: DateOfYear | null | undefined; } export default class ScheduleDate extends React.Component { static getDerivedStateFromProps(props: ScheduleDateProps, state: ScheduleDateState): Partial { if (!DateOfYear.equals(props.date, state.lastDate)) { const thisYear = moment().year(); const rawValue = props.date == null ? "" : moment(props.date).year(thisYear).format(HTML_DATE_INPUT_FORMAT); return { lastDate: props.date, rawValue }; } return {}; } constructor(p: ScheduleDateProps) { super(p); this.state = { rawValue: "", lastDate: undefined }; } render() { const { date, label, editing } = this.props; let dayNode: React.ReactNode; if (editing) { // tslint:disable-line:prefer-conditional-expression let clearIcon: React.ReactNode | undefined; if (date) { clearIcon = ; } dayNode = ; } else { const m = moment(date || ""); let dayString: string; if (m.isValid()) { const format = (m.year() === 0) ? "M/D" : "l"; dayString = m.format(format); } else { dayString = "N/A"; } dayNode = {dayString}; } let labelNode: React.ReactNode = null; if (typeof label === "string") { labelNode = ; } else if (label != null) { labelNode = label; } return {labelNode}{dayNode}; } private onChange = (e: React.SyntheticEvent, data: InputOnChangeData) => { const { onChange } = this.props; if (!onChange) return; const m = moment(data.value, HTML_DATE_INPUT_FORMAT); onChange(DateOfYear.fromMoment(m).with({ year: 0 })); } private onClear = () => { const { onChange } = this.props; if (!onChange) return; onChange(null); } }