fixed up inputtime
This commit is contained in:
1 parent
a3e9d19c45
commit
651ba865b1
11 files changed
+194
-59
No files matched your search
@@ -23,6 +23,7 @@
|
||||
"react-cookie": "^4.1.1",
|
||||
"react-dom": "^18.1.0",
|
||||
"react-flow-renderer": "^10.1.2",
|
||||
"react-imask": "^6.4.2",
|
||||
"react-jss": "^10.8.2",
|
||||
"react-router-dom": "6",
|
||||
"react-scripts": "5.0.1",
|
||||
|
||||
@@ -5,7 +5,7 @@ import InputText from "components/Inputs/InputText";
|
||||
import { InputEntity } from "components/Inputs/InputEntities";
|
||||
import { ConditionNode } from "./ConditionNode";
|
||||
import { genUpdateMethods } from "./nestedUpdater";
|
||||
import InputTime from "components/Inputs/InputTime";
|
||||
import { InputTime } from "components/Inputs/InputTime";
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
.input-time>.input-time--inner {
|
||||
display: flex;
|
||||
gap: 0;
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.input-time>.input-time--inner>.MuiFormControl-root {
|
||||
width: 4em;
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { Page } from "components/Page";
|
||||
import { useState } from "react";
|
||||
import { AutomationTime } from "types/automations/common";
|
||||
|
||||
import InputTime from "./InputTime";
|
||||
import { InputTime } from "./InputTime";
|
||||
|
||||
export default {
|
||||
title: 'Inputs/InputTime',
|
||||
@@ -16,11 +16,19 @@ const Template: ComponentStory<typeof InputTime> = args => {
|
||||
|
||||
const [value, setValue] = useState<AutomationTime | undefined>(undefined);
|
||||
return <Page>
|
||||
<InputTime {...args} value={value} onChange={setValue} />
|
||||
<div style={{
|
||||
padding: '1em'
|
||||
}}>
|
||||
<InputTime {...args} value={value} onChange={t => {
|
||||
setValue(t)
|
||||
args.onChange(t)
|
||||
}} />
|
||||
</div>
|
||||
</Page>
|
||||
}
|
||||
|
||||
export const SimpleText = Template.bind({})
|
||||
SimpleText.args = {
|
||||
label: "Time Stamp",
|
||||
value: "10"
|
||||
}
|
||||
@@ -1,60 +1,148 @@
|
||||
import { PropsWithChildren, useState } from "react";
|
||||
import "./InputTime.css";
|
||||
import { useState, useEffect, FC } from "react";
|
||||
import TextField from '@mui/material/TextField';
|
||||
import EditIcon from '@mui/icons-material/Edit';
|
||||
import CloseIcon from "@mui/icons-material/Close";
|
||||
import IconButton from "@mui/material/IconButton"
|
||||
import { AutomationTime, AutomationTimeObject } from "types/automations/common";
|
||||
import { milisecondsToObject } from "utils/time";
|
||||
import InputWrapper from "./InputWrapper";
|
||||
import { useInputTimeStyles } from "./styles";
|
||||
import { convertStringToAutomationTimeObject } from 'utils/time';
|
||||
import { convertAutomationTimeToTimeObject, convertObjectToAutomationTimeString, milisecondsToObject } from "utils/time";
|
||||
import { convertStringToAutomationTimeObject, convertAutomationTimeToTimeString } from 'utils/time';
|
||||
import { prettyName } from "utils/formatting";
|
||||
import FormControl from "@mui/material/FormControl";
|
||||
import InputLabel from '@mui/material/InputLabel';
|
||||
import Input from "@mui/material/Input";
|
||||
import FormHelperText from '@mui/material/FormHelperText';
|
||||
|
||||
// constants
|
||||
const fields: Array<keyof AutomationTimeObject> = [
|
||||
'hours',
|
||||
'minutes',
|
||||
'seconds',
|
||||
'milliseconds'
|
||||
]
|
||||
export interface Props {
|
||||
textBoxFor?: string;
|
||||
label: string;
|
||||
value?: AutomationTime;
|
||||
onChange: (v?: AutomationTimeObject) => void;
|
||||
onEnter?: () => void;
|
||||
}
|
||||
export default function InputTime({
|
||||
label,
|
||||
textBoxFor,
|
||||
value = {},
|
||||
onChange,
|
||||
onEnter = () => { },
|
||||
children,
|
||||
}: PropsWithChildren<Props>) {
|
||||
let cleanValue = '';
|
||||
if (value) {
|
||||
if (typeof value === 'string') {
|
||||
value = convertStringToAutomationTimeObject(value);
|
||||
export const InputTime: FC<Props> = (props) => {
|
||||
|
||||
|
||||
// state
|
||||
const [disabled, setDisabled] = useState(!props.value)
|
||||
const [displayValue, setDisplayValue] = useState(convertAutomationTimeToTimeObject(props.value))
|
||||
|
||||
// process display input
|
||||
const updateOne = (k: keyof AutomationTimeObject) => (v: string) => {
|
||||
if (Number(v) < 0) {
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
[k]: 0
|
||||
})
|
||||
} else if (k === "hours") {
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
[k]: Number(v)
|
||||
})
|
||||
} else if (k === "minutes") {
|
||||
let minutes = Number(v);
|
||||
let { hours = 0 } = displayValue
|
||||
if (minutes > 59) {
|
||||
const additonalHours = Math.floor(minutes / 60)
|
||||
hours += additonalHours
|
||||
minutes = minutes - additonalHours * 60
|
||||
}
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
minutes,
|
||||
hours,
|
||||
})
|
||||
} else if (k === "seconds") {
|
||||
let seconds = Number(v);
|
||||
let { hours = 0, minutes = 0 } = displayValue
|
||||
if (seconds > 59) {
|
||||
const additonalHours = Math.floor(seconds / (60 * 60))
|
||||
hours += additonalHours
|
||||
seconds = seconds - additonalHours * 60
|
||||
}
|
||||
if (seconds > 59) {
|
||||
const additionalMinutes = Math.floor(seconds / 60)
|
||||
minutes += additionalMinutes
|
||||
seconds = seconds - additionalMinutes * 60
|
||||
}
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
minutes,
|
||||
hours,
|
||||
seconds,
|
||||
})
|
||||
} else if (k === "milliseconds") {
|
||||
let milliseconds = Number(v);
|
||||
let { hours = 0, minutes = 0, seconds = 0 } = displayValue
|
||||
if (milliseconds > 1000) {
|
||||
const additonalHours = Math.floor(milliseconds / (60 * 60 * 1000))
|
||||
hours += additonalHours
|
||||
milliseconds = milliseconds - additonalHours * 60 * 60 * 1000
|
||||
}
|
||||
if (milliseconds > 1000) {
|
||||
const additionalMinutes = Math.floor(milliseconds / (60 * 1000))
|
||||
minutes += additionalMinutes
|
||||
milliseconds = milliseconds - additionalMinutes * 60 * 1000
|
||||
}
|
||||
if (milliseconds > 1000) {
|
||||
const additionalSeconds = Math.floor(milliseconds / 1000)
|
||||
seconds += additionalSeconds
|
||||
milliseconds = milliseconds - additionalSeconds * 1000
|
||||
}
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
minutes,
|
||||
hours,
|
||||
seconds,
|
||||
milliseconds,
|
||||
})
|
||||
}
|
||||
const {
|
||||
hours = 0,
|
||||
minutes = 0,
|
||||
seconds = 0,
|
||||
milliseconds = 0,
|
||||
} = value;
|
||||
cleanValue = [hours, minutes, seconds].map(x => String(x).padStart(2, '0')).join(':') + `.${String(milliseconds).padStart(3, '0')}`;
|
||||
}
|
||||
const { classes } = useInputTimeStyles({});
|
||||
const [isFocused, setIsFocused] = useState(false)
|
||||
return <InputWrapper className={classes.wrapper} label={label} labelSize={(value === '') && !isFocused ? 'normal' : 'small'}>
|
||||
<input
|
||||
className={classes.input}
|
||||
type="time"
|
||||
step={0.001}
|
||||
value={cleanValue}
|
||||
onKeyDown={e => {
|
||||
if (e.key === 'Enter') {
|
||||
onEnter();
|
||||
}
|
||||
}}
|
||||
onChange={e => {
|
||||
e.preventDefault();
|
||||
onChange(milisecondsToObject(e.target.valueAsNumber))
|
||||
}}
|
||||
onBlur={e => {
|
||||
setIsFocused(false)
|
||||
}}
|
||||
/>
|
||||
<button onClick={() => onChange()} className={classes.cancel}>X</button>
|
||||
{children}
|
||||
</InputWrapper>
|
||||
|
||||
|
||||
// effect
|
||||
useEffect(() => {
|
||||
if (disabled && !props.value) {
|
||||
return
|
||||
}
|
||||
if (disabled) {
|
||||
props.onChange(undefined)
|
||||
} else {
|
||||
const pVal = convertAutomationTimeToTimeString(props.value);
|
||||
const cVal = convertObjectToAutomationTimeString(displayValue);
|
||||
if (pVal !== cVal) {
|
||||
props.onChange(displayValue)
|
||||
}
|
||||
}
|
||||
}, [disabled, displayValue])
|
||||
|
||||
|
||||
return <div className="input-time">
|
||||
<InputLabel >{props.label}</InputLabel>
|
||||
<div className="input-time--inner">
|
||||
{fields.map(name =>
|
||||
<TextField
|
||||
key={name}
|
||||
variant="filled"
|
||||
label={prettyName(name)}
|
||||
defaultValue="00"
|
||||
InputProps={{
|
||||
disabled,
|
||||
type: "number",
|
||||
title: prettyName(name),
|
||||
}}
|
||||
value={displayValue[name] ?? 0}
|
||||
onChange={e => updateOne(name)(e.target.value)}
|
||||
/>
|
||||
)}
|
||||
<IconButton onClick={() => setDisabled(!disabled)}>
|
||||
{disabled ? <EditIcon /> : <CloseIcon />}
|
||||
</IconButton>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -3,8 +3,7 @@ import { AutomationTriggerNumericState } from 'types/automations/triggers';
|
||||
import InputText from 'components/Inputs/InputText';
|
||||
import { InputEntity } from 'components/Inputs/InputEntities';
|
||||
import InputNumber from 'components/Inputs/InputNumber';
|
||||
import InputTime from 'components/Inputs/InputTime';
|
||||
import InputYaml from 'components/Inputs/InputYaml';
|
||||
import { InputTime } from 'components/Inputs/InputTime'; import InputYaml from 'components/Inputs/InputYaml';
|
||||
|
||||
|
||||
export const TriggerNumericState: OptionManager<AutomationTriggerNumericState> = {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerState } from 'types/automations/triggers';
|
||||
import InputText from 'components/Inputs/InputText';
|
||||
import { InputEntity } from 'components/Inputs/InputEntities';
|
||||
import InputTime from 'components/Inputs/InputTime';
|
||||
import { InputTime } from 'components/Inputs/InputTime';
|
||||
|
||||
|
||||
export const TriggerState: OptionManager<AutomationTriggerState> = {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerTemplate } from 'types/automations/triggers';
|
||||
import InputText from 'components/Inputs/InputText';
|
||||
import InputTime from 'components/Inputs/InputTime';
|
||||
import { InputTime } from 'components/Inputs/InputTime';
|
||||
|
||||
|
||||
export const TriggerTemplate: OptionManager<AutomationTriggerTemplate> = {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerTime } from 'types/automations/triggers';
|
||||
import InputTime from 'components/Inputs/InputTime';
|
||||
import { InputTime } from 'components/Inputs/InputTime';
|
||||
import { convertObjectToAutomationTimeString } from 'utils/time';
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AutomationTimeObject, AutomationTimeString } from "types/automations/common";
|
||||
import { AutomationTime, AutomationTimeObject, AutomationTimeString } from "types/automations/common";
|
||||
|
||||
const cleanTime = (t: number) => Math.max(0, Math.floor(t))
|
||||
|
||||
@@ -28,4 +28,21 @@ export const convertObjectToAutomationTimeString = (t: AutomationTimeObject): Au
|
||||
seconds = 0
|
||||
} = t
|
||||
return [hours, minutes, seconds].map(x => String(x).padStart(2, '0')).join(':')
|
||||
}
|
||||
|
||||
export const convertAutomationTimeToTimeString = (t: AutomationTime | undefined): AutomationTimeString => {
|
||||
let obj: AutomationTimeObject;
|
||||
if (typeof t === 'string') {
|
||||
obj = convertStringToAutomationTimeObject(t)
|
||||
} else if (!t) {
|
||||
return "00:00:00"
|
||||
} else {
|
||||
obj = t
|
||||
}
|
||||
return convertObjectToAutomationTimeString(obj);
|
||||
}
|
||||
|
||||
|
||||
export const convertAutomationTimeToTimeObject = (t: AutomationTime | undefined): AutomationTimeObject => {
|
||||
return convertStringToAutomationTimeObject(convertAutomationTimeToTimeString(t))
|
||||
}
|
||||
@@ -8806,6 +8806,11 @@ ignore@^5.2.0:
|
||||
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
|
||||
integrity sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ==
|
||||
|
||||
imask@^6.4.2:
|
||||
version "6.4.2"
|
||||
resolved "https://registry.yarnpkg.com/imask/-/imask-6.4.2.tgz#f1c67e6961640bf5b09d7b4a34dcf4f9a3df7e22"
|
||||
integrity sha512-xvEgbTdk6y2dW2UAysq0NRPmO6PuaXM5NHIt4TXEJEwXUHj26M0p/fXqyrSJdNXFaGVOtqYjPRnNdrjQQhDuuA==
|
||||
|
||||
immer@^9.0.7:
|
||||
version "9.0.14"
|
||||
resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.14.tgz#e05b83b63999d26382bb71676c9d827831248a48"
|
||||
@@ -12762,6 +12767,14 @@ react-flow-renderer@^10.1.2:
|
||||
d3-zoom "^3.0.0"
|
||||
zustand "^3.7.2"
|
||||
|
||||
react-imask@^6.4.2:
|
||||
version "6.4.2"
|
||||
resolved "https://registry.yarnpkg.com/react-imask/-/react-imask-6.4.2.tgz#0bd1a4c6cc8838abfbbc340eab9f311831a57c37"
|
||||
integrity sha512-xBGZyM0RwQ/ujX7f2ccaYxE41eB81q5Yqek6SYXAq6XT88+TdCSmIpgh5GeygRedPJu6tnnN8OXrUMTc2uFNSw==
|
||||
dependencies:
|
||||
imask "^6.4.2"
|
||||
prop-types "^15.7.2"
|
||||
|
||||
react-inspector@^5.1.0:
|
||||
version "5.1.1"
|
||||
resolved "https://registry.yarnpkg.com/react-inspector/-/react-inspector-5.1.1.tgz#58476c78fde05d5055646ed8ec02030af42953c8"
|
||||
|
||||
Reference in new issue
Block a user