tag editor

This commit is contained in:
ari committed 2022-07-27 21:09:30 -04:00
1 parent 5d9c04072b
commit 104882cb25
10 files changed
+186 -19

No files matched your search

@@ -6,7 +6,6 @@ import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
import { TagDB } from "./TagDB";
import DeleteIcon from "@mui/icons-material/ClearTwoTone";
import { InputTextView } from "components/Inputs/InputTextView";
import { useHAEntities } from "haService/HAEntities";
export const MetadataBox: FC<{
metadata: AutomationMetadata;
@@ -3,6 +3,7 @@ import "./index.css";
import { FC, useState } from "react";
import { MetadataBox } from "../MetadataBox";
import { AutomationListAutoUpdatable, AutomationListItem } from "../types";
import { TagDB } from "../TagDB";
export type ListBoxGroupProps = AutomationListItem & {
onTagUpdate: (t: Record<string, string>, aid: string) => void;
@@ -14,6 +15,7 @@ export type ListBoxGroupProps = AutomationListItem & {
onAutomationDelete: (aid: string) => void;
onSelect: (aid: string) => void;
initialOpenState?: boolean;
tagsDB: TagDB;
};
export const ListBoxGroup: FC<ListBoxGroupProps> = ({
title,
@@ -21,6 +23,7 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
data,
isSelected,
initialOpenState,
tagsDB,
...events
}) => {
const [open, setOpen] = useState(initialOpenState ?? false);
@@ -58,6 +61,7 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
<MetadataBox
key={auto.id}
{...auto}
tagsDB={tagsDB}
onDelete={() => events.onAutomationDelete(auto.id)}
onSelect={() => events.onSelect(auto.id)}
onTagUpdate={(t) => events.onTagUpdate(t, auto.id)}
@@ -94,7 +98,7 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
/>
))
: data.map((group, i) => (
<ListBoxGroup {...events} {...group} key={i} />
<ListBoxGroup {...events} {...group} tagsDB={tagsDB} key={i} />
))}
</div>
</div>
@@ -1,17 +1,90 @@
import { FC } from "react";
import { FC, ReactNode, useState } from "react";
import DeleteIcon from "@mui/icons-material/ClearTwoTone";
import EditIcon from "@mui/icons-material/EditOutlined";
import { cleanUpUndefined } from "utils/helpers";
import { Modal } from "components/Modal";
import { Button } from "components/Inputs/Button";
import InputAutoText from "components/Inputs/InputAutoText";
import { TagDB } from "../TagDB";
export type TagsProps = {
tags: Record<string, string>;
onUpdate: (t: Record<string, string>) => void;
tagsDB: TagDB;
};
export const Tags: FC<TagsProps> = ({ tags, onUpdate }) => {
export type TagModalState =
| {
open: false;
}
| {
open: true;
tag: [string, string];
isNew: boolean;
};
export const Tags: FC<TagsProps> = ({ tags, onUpdate, tagsDB }) => {
const [modalState, setModal] = useState<TagModalState>({ open: false });
let innerModal: ReactNode = <></>;
if (modalState.open) {
const validTag =
modalState.tag[0].trim().length > 0 &&
modalState.tag[1].trim().length > 0;
innerModal = (
<div className="metadatabox--tags--modal">
<InputAutoText
className="name"
options={tagsDB.getTagNames(Object.keys(tags))}
label="Tag Name"
value={modalState.tag[0]}
onChange={(v) =>
setModal({
...modalState,
tag: [v ?? "", modalState.tag[1]],
})
}
/>
<InputAutoText
className="value"
options={tagsDB.getTagValues(modalState.tag[0])}
label="Tag Value"
value={modalState.tag[1]}
onChange={(v) =>
setModal({
...modalState,
tag: [modalState.tag[0], v ?? ""],
})
}
/>
<div className="buttons">
<Button onClick={() => setModal({ open: false })} color="info">
Close
</Button>
<Button
disabled={!validTag}
onClick={() => {
onUpdate({
...tags,
[modalState.tag[0]]: modalState.tag[1],
});
setModal({ open: false });
}}
color="success"
>
{modalState.isNew ? "Create" : "Save"}
</Button>
</div>
</div>
);
}
return (
<div className="metadatabox--tags">
<Modal open={modalState.open}>{innerModal}</Modal>
{Object.entries(tags).map(([tagName, tagValue]) => (
<span key={tagName} className="tag">
<DeleteIcon
@@ -25,10 +98,30 @@ export const Tags: FC<TagsProps> = ({ tags, onUpdate }) => {
}
className="delete-tag"
/>
<b>{tagName}</b>: {tagValue}
<span
className="inner"
onClick={() =>
setModal({
open: true,
isNew: false,
tag: [tagName, tagValue],
})
}
>
<b>{tagName}</b>: {tagValue} <EditIcon className="edit-icon" />
</span>
</span>
))}
<span className="tag-add" onClick={() => {}}>
<span
className="tag-add"
onClick={() =>
setModal({
open: true,
isNew: true,
tag: ["", ""],
})
}
>
+
</span>
</div>
@@ -115,4 +115,38 @@
color: var(--mui-error-light);
outline: 1px solid var(--mui-error-dark);
border-radius: 20px;
}
.automation-list--metadatabox .metadatabox--tags .tag .inner {
font-size: 1em;
display: flex;
justify-content: center;
align-items: center;
cursor: text;
user-select: none;
}
.automation-list--metadatabox .metadatabox--tags .tag .inner .edit-icon {
height: 0.7em;
width: 0.7em;
padding: 0.15em;
}
.metadatabox--tags--modal {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 0.25em;
}
.metadatabox--tags--modal .input-autotext {
min-width: calc(max(10em, 100%));
}
.metadatabox--tags--modal .buttons {
display: flex;
gap: 0.5em;
justify-content: space-evenly;
}
@@ -2,6 +2,7 @@ import "styles/root.css";
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { MetadataBox } from ".";
import { makeTagDB } from "../TagDB";
export default {
title: "App/AutomationList/MetadataBox",
@@ -22,7 +23,7 @@ export default {
const Template: ComponentStory<typeof MetadataBox> = (args) => {
return (
<Page>
<MetadataBox {...args} />
<MetadataBox {...args} tagsDB={makeTagDB([])} />
</Page>
);
};
@@ -9,9 +9,9 @@ import SelectIcon from "@mui/icons-material/HighlightAltSharp";
import { InputTextView } from "components/Inputs/InputTextView";
import { ButtonIcon } from "components/Icons/ButtonIcons";
import { cleanUpUndefined } from "utils/helpers";
import { AutomationListAuto } from "../types";
import { Tags } from "./Tags";
import { TagDB } from "../TagDB";
export type MetadataBoxProps = AutomationListAuto & {
onDelete: () => void;
@@ -20,6 +20,7 @@ export type MetadataBoxProps = AutomationListAuto & {
onTagUpdate: (t: Record<string, string>) => void;
onTitleUpdate: (t: string) => void;
onDescriptionUpdate: (t: string) => void;
tagsDB: TagDB;
};
export const MetadataBox: FC<MetadataBoxProps> = (props) => {
const isValidState = ["on", "off"].includes(props.state);
@@ -57,7 +58,11 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
value={props.description}
onChange={props.onDescriptionUpdate}
/>
<Tags tags={props.tags} onUpdate={props.onTagUpdate} />
<Tags
tags={props.tags}
onUpdate={props.onTagUpdate}
tagsDB={props.tagsDB}
/>
{props.issue ? (
<Alert severity="warning" className="issue">
{props.issue}
@@ -84,6 +84,7 @@ export const AutomationList: FC<AutomationListProps> = ({
key={i}
onSelect={setSelectedAutomationId}
{...events}
tagsDB={tagsDB}
initialOpenState={true}
/>
))}
+3 -1
View File
@@ -1,5 +1,5 @@
import { FC, ReactNode } from "react";
import MuiButton from "@mui/material/Button";
import MuiButton, { ButtonProps as MuiButtonProps } from "@mui/material/Button";
export const Button: FC<{
className?: string;
@@ -7,6 +7,7 @@ export const Button: FC<{
disabled?: boolean;
children?: ReactNode;
onClick?: () => void;
color?: MuiButtonProps["color"];
}> = (props) => {
return (
<MuiButton
@@ -16,6 +17,7 @@ export const Button: FC<{
onClick={props.onClick}
variant="outlined"
children={props.children}
color={props.color}
/>
);
};
@@ -0,0 +1,29 @@
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import InputAutoText from "./InputAutoText";
export default {
title: "Inputs/InputAutoText",
component: InputAutoText,
parameters: { actions: { argTypesRegex: "^on.*" } },
args: {
label: "Something",
value: "",
options: ["test", "usa", "canada"],
},
} as ComponentMeta<typeof InputAutoText>;
const Template: ComponentStory<typeof InputAutoText> = (args) => {
return (
<Page>
<InputAutoText {...args} />
</Page>
);
};
export const Basic = Template.bind({});
export const WithError = Template.bind({});
WithError.args = {
...WithError.args,
error: "Oh no!",
};
@@ -11,14 +11,12 @@ export interface InputAutoTextProps {
required?: boolean;
endAdornment?: ReactNode;
onChange: (v: string) => void;
onEnter?: () => void;
}
export default function InputAutoText({
value = "",
label,
options = [],
onChange,
onEnter = () => {},
className,
required,
endAdornment,
@@ -27,9 +25,10 @@ export default function InputAutoText({
return (
<Autocomplete
freeSolo
className={className}
className={["input-autotext", className ?? ""].join(" ")}
value={value}
options={options}
disableClearable
onChange={(_e, v) => onChange(v === null ? "" : v)}
renderInput={(params) => (
<TextField
@@ -41,12 +40,12 @@ export default function InputAutoText({
helperText={error}
fullWidth
InputProps={{
endAdornment,
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
onEnter();
}
...params.InputProps,
endAdornment: (
<>
{endAdornment} {params.InputProps.endAdornment}
</>
),
}}
onChange={(e) => {
e.preventDefault();