60 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-08-06 12:04:08 +03:00
import * as React from "react";
import { Checkbox, CheckboxProps, Form } from "semantic-ui-react";
import { Weekday, WEEKDAYS } from "@common/sprinklersRpc";
export interface WeekdaysViewProps {
2018-09-02 02:57:55 -06:00
weekdays: Weekday[];
editing: boolean;
onChange?: (newWeekdays: Weekday[]) => void;
2018-08-06 12:04:08 +03:00
}
export default class WeekdaysView extends React.Component<WeekdaysViewProps> {
2018-09-02 02:57:55 -06:00
render() {
const { weekdays, editing } = this.props;
let node: React.ReactNode;
if (editing) {
node = WEEKDAYS.map(weekday => {
const checked = weekdays.find(wd => wd === weekday) != null;
const name = Weekday[weekday];
2018-08-06 12:10:34 +03:00
return (
2018-09-02 02:57:55 -06:00
<Form.Field
control={Checkbox}
x-weekday={weekday}
label={name}
checked={checked}
key={weekday}
onChange={this.toggleWeekday}
/>
2018-08-06 12:10:34 +03:00
);
2018-09-02 02:57:55 -06:00
});
} else {
node = weekdays.map(weekday => Weekday[weekday]).join(", ");
2018-08-06 12:04:08 +03:00
}
2018-09-02 02:57:55 -06:00
return (
<Form.Group inline>
<label>On</label> {node}
</Form.Group>
);
}
private toggleWeekday = (
event: React.FormEvent<HTMLInputElement>,
data: CheckboxProps
) => {
const { weekdays, onChange } = this.props;
if (!onChange) {
return;
2018-08-06 12:04:08 +03:00
}
2018-09-02 02:57:55 -06:00
const weekday: Weekday = Number(
event.currentTarget.getAttribute("x-weekday")
);
if (data.checked) {
const newWeekdays = weekdays.concat([weekday]);
newWeekdays.sort();
onChange(newWeekdays);
} else {
onChange(weekdays.filter(wd => wd !== weekday));
}
};
2018-08-06 12:04:08 +03:00
}