sprinklers3/app/components/DurationInput.tsx

58 lines
2.0 KiB
TypeScript
Raw Normal View History

2017-10-13 16:11:37 -06:00
import * as classNames from "classnames";
import * as React from "react";
2017-09-10 15:27:32 -06:00
import { Input, InputProps } from "semantic-ui-react";
2017-10-10 16:34:02 -06:00
import { Duration } from "@common/Duration";
export default class DurationInput extends React.Component<{
duration: Duration,
2017-10-13 16:11:37 -06:00
onDurationChange: (newDuration: Duration) => void,
className?: string,
2017-08-29 22:42:56 -06:00
}> {
render() {
const duration = this.props.duration;
2017-10-13 16:11:37 -06:00
const className = classNames("field", "durationInput", this.props.className);
// const editing = this.props.onDurationChange != null;
2017-09-08 09:49:30 -06:00
return (
2017-10-13 16:11:37 -06:00
<div className={className}>
2017-09-08 09:49:30 -06:00
<label>Duration</label>
2017-10-31 17:23:27 -06:00
<div className="ui two fields">
2017-09-08 09:49:30 -06:00
<Input
type="number"
className="field durationInput--minutes"
value={duration.minutes}
onChange={this.onMinutesChange}
label="M"
labelPosition="right"
/>
<Input
type="number"
className="field durationInput--seconds"
value={duration.seconds}
onChange={this.onSecondsChange}
max="60"
label="S"
labelPosition="right"
/>
</div>
</div>
2017-09-08 09:49:30 -06:00
);
}
2017-09-10 15:27:32 -06:00
private onMinutesChange: InputProps["onChange"] = (e, { value }) => {
2017-08-29 22:42:56 -06:00
if (value.length === 0 || isNaN(Number(value))) {
return;
}
const newMinutes = parseInt(value, 10);
this.props.onDurationChange(this.props.duration.withMinutes(newMinutes));
}
2017-09-10 15:27:32 -06:00
private onSecondsChange: InputProps["onChange"] = (e, { value }) => {
2017-08-29 22:42:56 -06:00
if (value.length === 0 || isNaN(Number(value))) {
return;
}
const newSeconds = parseInt(value, 10);
this.props.onDurationChange(this.props.duration.withSeconds(newSeconds));
}
}