latest work
This commit is contained in:
1 parent
651ba865b1
commit
240f8faef7
13 files changed
+323
-57
No files matched your search
@@ -0,0 +1,148 @@
|
||||
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 { 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 {
|
||||
label: string;
|
||||
value?: AutomationTime;
|
||||
onChange: (v?: AutomationTimeObject) => void;
|
||||
}
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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>
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import InputLabel from '@mui/material/InputLabel';
|
||||
import MenuItem from '@mui/material/MenuItem';
|
||||
import FormControl from '@mui/material/FormControl';
|
||||
import Select from '@mui/material/Select';
|
||||
import { prettyName } from "utils/formatting";
|
||||
|
||||
export interface Props<T extends string> {
|
||||
current: T;
|
||||
@@ -12,25 +13,26 @@ export interface Props<T extends string> {
|
||||
label: string;
|
||||
id?: string;
|
||||
className?: string;
|
||||
prettyOptionLabels?: boolean;
|
||||
}
|
||||
export const InputList: FC<Props<any>> = ({
|
||||
export function InputList<T extends string>({
|
||||
current,
|
||||
options,
|
||||
onChange,
|
||||
label,
|
||||
id,
|
||||
className,
|
||||
}) => {
|
||||
prettyOptionLabels,
|
||||
}: Props<T>) {
|
||||
const invalidCurrent = current && !options.includes(current);
|
||||
return <FormControl variant="filled" sx={{ marginRight: "0.25em", minWidth: `${label.length * 0.75}em` }}>
|
||||
<InputLabel id="demo-simple-select-filled-label">{label}</InputLabel>
|
||||
return <FormControl className={className} variant="filled" sx={{ marginRight: "0.25em", minWidth: `${label.length * 0.75}em` }}>
|
||||
<InputLabel>{label}</InputLabel>
|
||||
<Select
|
||||
className={className}
|
||||
autoWidth
|
||||
id={id}
|
||||
value={current}
|
||||
onChange={(e) => {
|
||||
onChange(e.target.value)
|
||||
onChange(e.target.value as T)
|
||||
}}
|
||||
label={label}
|
||||
variant="filled"
|
||||
@@ -43,7 +45,7 @@ export const InputList: FC<Props<any>> = ({
|
||||
key={i}
|
||||
value={t}
|
||||
>
|
||||
{t}
|
||||
{prettyOptionLabels ? prettyName(t) : t}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface Props {
|
||||
additionalTooltipFilters?: Record<string, string>;
|
||||
multiline?: boolean;
|
||||
maxRows?: number;
|
||||
className?: string;
|
||||
}
|
||||
export default function InputText({
|
||||
label,
|
||||
@@ -23,10 +24,11 @@ export default function InputText({
|
||||
multiline = false,
|
||||
maxRows = 4,
|
||||
children,
|
||||
className,
|
||||
}: PropsWithChildren<Props>) {
|
||||
const tooltip = useToolTip();
|
||||
const [isFocused, setIsFocused] = useState(false)
|
||||
return <InputWrapper label="">
|
||||
return <InputWrapper className={className} label="">
|
||||
<TextField multiline={multiline} maxRows={maxRows} fullWidth inputMode="text" variant="filled" focused={isFocused} label={label} value={value}
|
||||
onKeyDown={e => {
|
||||
if (e.key === 'Enter') {
|
||||
|
||||
@@ -24,6 +24,7 @@ export interface Props {
|
||||
label: string;
|
||||
value?: AutomationTime;
|
||||
onChange: (v?: AutomationTimeObject) => void;
|
||||
className?: string;
|
||||
}
|
||||
export const InputTime: FC<Props> = (props) => {
|
||||
|
||||
@@ -122,8 +123,8 @@ export const InputTime: FC<Props> = (props) => {
|
||||
}, [disabled, displayValue])
|
||||
|
||||
|
||||
return <div className="input-time">
|
||||
<InputLabel >{props.label}</InputLabel>
|
||||
return <div className={["input-time", props.className ?? ""].join(" ")}>
|
||||
{/* <InputLabel >{props.label}</InputLabel> */}
|
||||
<div className="input-time--inner">
|
||||
{fields.map(name =>
|
||||
<TextField
|
||||
|
||||
@@ -11,12 +11,14 @@ export interface Props<T extends {}> {
|
||||
value: T;
|
||||
onChange: (v: T) => void;
|
||||
error?: JSX.Element | string,
|
||||
className?: string;
|
||||
}
|
||||
export default function InputYaml<T>({
|
||||
export default function InputYaml<T extends {}>({
|
||||
label,
|
||||
error: incmError,
|
||||
value = {} as any,
|
||||
onChange,
|
||||
className,
|
||||
}: Props<T>) {
|
||||
const [{ text, error }, setState] = useState<{
|
||||
text: string,
|
||||
@@ -50,7 +52,7 @@ export default function InputYaml<T>({
|
||||
}
|
||||
}
|
||||
}, [text], 1000)
|
||||
return <InputWrapper label={label} error={error}>
|
||||
return <InputWrapper className={className} label={label} error={error}>
|
||||
<CodeMirror
|
||||
theme={"dark"}
|
||||
value={text}
|
||||
|
||||
Reference in new issue
Block a user