108 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-08-06 12:04:08 +03:00
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 {
2018-09-02 02:57:55 -06:00
date: DateOfYear | null | undefined;
label: string | React.ReactNode | undefined;
editing: boolean | undefined;
onChange: (newDate: DateOfYear | null) => void;
2018-08-06 12:04:08 +03:00
}
interface ScheduleDateState {
2018-09-02 02:57:55 -06:00
rawValue: string | "";
lastDate: DateOfYear | null | undefined;
2018-08-06 12:04:08 +03:00
}
2018-09-02 02:57:55 -06:00
export default class ScheduleDate extends React.Component<
ScheduleDateProps,
ScheduleDateState
> {
static getDerivedStateFromProps(
props: ScheduleDateProps,
state: ScheduleDateState
): Partial<ScheduleDateState> {
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 };
2018-08-06 12:04:08 +03:00
}
2018-09-02 02:57:55 -06:00
return {};
}
2018-08-06 12:04:08 +03:00
2018-09-02 02:57:55 -06:00
constructor(p: ScheduleDateProps) {
super(p);
this.state = { rawValue: "", lastDate: undefined };
}
2018-08-06 12:04:08 +03:00
2018-09-02 02:57:55 -06:00
render() {
const { date, label, editing } = this.props;
2018-08-06 12:04:08 +03:00
2018-09-02 02:57:55 -06:00
let dayNode: React.ReactNode;
if (editing) {
// tslint:disable-line:prefer-conditional-expression
let clearIcon: React.ReactNode | undefined;
if (date) {
clearIcon = <Icon name="ban" link onClick={this.onClear} />;
}
dayNode = (
<Input
type="date"
icon={clearIcon}
value={this.state.rawValue}
onChange={this.onChange}
/>
);
} 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 = <span>{dayString}</span>;
2018-08-06 12:04:08 +03:00
}
2018-09-02 02:57:55 -06:00
let labelNode: React.ReactNode = null;
if (typeof label === "string") {
labelNode = <label>{label}</label>;
} else if (label != null) {
labelNode = label;
2018-08-06 12:04:08 +03:00
}
2018-09-02 02:57:55 -06:00
return (
<Form.Field inline>
{labelNode}
{dayNode}
</Form.Field>
);
}
private onChange = (
e: React.SyntheticEvent<HTMLInputElement>,
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);
};
2018-08-06 12:04:08 +03:00
}