diff --git a/ui/v2/src/components/Shared/DurationInput.tsx b/ui/v2/src/components/Shared/DurationInput.tsx new file mode 100644 index 000000000..6f9c15383 --- /dev/null +++ b/ui/v2/src/components/Shared/DurationInput.tsx @@ -0,0 +1,127 @@ +import React, { FunctionComponent, useState, useEffect } from "react"; +import { InputGroup, ButtonGroup, Button, IInputGroupProps, HTMLInputProps, ControlGroup } from "@blueprintjs/core"; +import { TextUtils } from "../../utils/text"; +import { FIXED, NUMERIC_INPUT } from "@blueprintjs/core/lib/esm/common/classes"; + +interface IProps { + disabled?: boolean + numericValue: number + onValueChange(valueAsNumber: number): void + onReset?(): void +} + +export const DurationInput: FunctionComponent = (props: IProps) => { + const [value, setValue] = useState(secondsToString(props.numericValue)); + + useEffect(() => { + setValue(secondsToString(props.numericValue)); + }, [props.numericValue]); + + function secondsToString(seconds : number) { + let ret = TextUtils.secondsToTimestamp(seconds); + + if (ret.startsWith("00:")) { + ret = ret.substr(3); + + if (ret.startsWith("0")) { + ret = ret.substr(1); + } + } + + return ret; + } + + function stringToSeconds(v : string) { + if (!v) { + return 0; + } + + let splits = v.split(":"); + + if (splits.length > 3) { + return 0; + } + + let seconds = 0; + let factor = 1; + while(splits.length > 0) { + let thisSplit = splits.pop(); + if (thisSplit == undefined) { + return 0; + } + + let thisInt = parseInt(thisSplit, 10); + if (isNaN(thisInt)) { + return 0; + } + + seconds += factor * thisInt; + factor *= 60; + } + + return seconds; + } + + function increment() { + let seconds = stringToSeconds(value); + seconds += 1; + props.onValueChange(seconds); + } + + function decrement() { + let seconds = stringToSeconds(value); + seconds -= 1; + props.onValueChange(seconds); + } + + function renderButtons() { + return ( + +