abstracted away input time entity
This commit is contained in:
1 parent
3a44057a40
commit
184734ee70
7 files changed
+160
-72
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import "./InputTimeEntity.css";
|
||||
|
||||
import { FC, ReactNode } from "react";
|
||||
|
||||
import AccessTimeOutlinedIcon from "@mui/icons-material/AccessTimeOutlined";
|
||||
import MoreTimeOutlinedIcon from "@mui/icons-material/MoreTimeOutlined";
|
||||
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import {
|
||||
convertObjectToAutomationTimeString,
|
||||
isAutomationTimeString24Hours,
|
||||
} from "utils/time";
|
||||
import { InputEntity } from "./InputEntities";
|
||||
import { InputTime } from "./InputTime";
|
||||
|
||||
export type InputTimeEntityProps = {
|
||||
value: string;
|
||||
onChange: (v: string) => void;
|
||||
className?: string;
|
||||
};
|
||||
export const InputTimeEntity: FC<InputTimeEntityProps> = ({
|
||||
value,
|
||||
onChange,
|
||||
className = "",
|
||||
}) => {
|
||||
const is24Hour = isAutomationTimeString24Hours(value);
|
||||
let editor: ReactNode;
|
||||
if (is24Hour) {
|
||||
editor = (
|
||||
<InputTime
|
||||
label=""
|
||||
value={value ?? "00:00:00"}
|
||||
restrictEmpty
|
||||
onChange={(t) =>
|
||||
onChange(t ? convertObjectToAutomationTimeString(t) : "00:00:00")
|
||||
}
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
editor = (
|
||||
<InputEntity
|
||||
label="Time Entity"
|
||||
restrictMode="or"
|
||||
restrictedDeviceClass={["timestamp"]}
|
||||
restrictToDomain={["input_datetime"]}
|
||||
value={value}
|
||||
multiple={false}
|
||||
onChange={(t) => onChange(t ?? "")}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={"input-time-entity " + className}>
|
||||
{editor}{" "}
|
||||
<ButtonIcon
|
||||
icon={is24Hour ? <AccessTimeOutlinedIcon /> : <MoreTimeOutlinedIcon />}
|
||||
onClick={() => {
|
||||
if (is24Hour) {
|
||||
onChange("");
|
||||
} else {
|
||||
onChange("00:00:00");
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user