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,21 @@
|
||||
.input-time-entity {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 0.5em;
|
||||
}
|
||||
|
||||
.input-time-entity button.MuiButtonBase-root {
|
||||
border-color: var(--mui-info-light);
|
||||
background-color: var(--mui-info-main);
|
||||
color: var(--mui-info-contrastText);
|
||||
}
|
||||
|
||||
.input-time-entity button.MuiButtonBase-root:hover {
|
||||
background-color: var(--mui-info-light);
|
||||
}
|
||||
|
||||
.input-time-entity .input-autocomplete {
|
||||
min-width: 10em;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { useState } from "react";
|
||||
|
||||
import { InputTimeEntity } from "./InputTimeEntity";
|
||||
|
||||
export default {
|
||||
title: "Inputs/InputTimeEntity",
|
||||
component: InputTimeEntity,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
} as ComponentMeta<typeof InputTimeEntity>;
|
||||
|
||||
const Template: ComponentStory<typeof InputTimeEntity> = (args) => {
|
||||
const [value, setValue] = useState("");
|
||||
return (
|
||||
<Page>
|
||||
<div
|
||||
style={{
|
||||
padding: "1em",
|
||||
}}
|
||||
>
|
||||
<InputTimeEntity
|
||||
{...args}
|
||||
value={value}
|
||||
onChange={(t) => {
|
||||
setValue(t);
|
||||
args.onChange(t);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const SimpleText = Template.bind({});
|
||||
@@ -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