moved control for tag update to tag db
renamed list to manager
This commit is contained in:
1 parent
05d09830bb
commit
02ec81c16a
26 files changed
+669
-383
No files matched your search
@@ -1,152 +0,0 @@
|
||||
.automation-list--metadatabox {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: space-around;
|
||||
gap: 0.25em;
|
||||
|
||||
padding: 0.25em;
|
||||
background-color: var(--mui-background-paper);
|
||||
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-color: transparent;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox:hover, .automation-list--metadatabox.selected {
|
||||
border-color: var(--mui-action-focus);
|
||||
background-color: var(--mui-action-focus);
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--title .input-text-view {
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--title {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--title .title {
|
||||
font-size: 1.25em;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--title .issue {
|
||||
padding: 0 0.5em;
|
||||
user-select: none;
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--title .description {
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .delete:hover {
|
||||
cursor: pointer;
|
||||
color: var(--mui-error-light);
|
||||
background-color: var(--mui-error-dark);
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .select:hover,
|
||||
.automation-list--metadatabox.selected.automation-list--metadatabox .select {
|
||||
cursor: pointer;
|
||||
color: var(--mui-primary-light);
|
||||
background-color: var(--mui-primary-dark);
|
||||
}
|
||||
|
||||
.automation-list--metadatabox.selected.automation-list--metadatabox .select:hover {
|
||||
color: var(--mui-secondary-light);
|
||||
background-color: var(--mui-secondary-dark);
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--switch {
|
||||
display: flex;
|
||||
cursor: pointer;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
font-size: 0.75em;
|
||||
user-select: none;
|
||||
max-width: 5em;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--tags {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.25em;
|
||||
margin-top: 0.25em;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--tags .tag,
|
||||
.automation-list--metadatabox .metadatabox--tags .tag-add {
|
||||
display: flex;
|
||||
max-width: fit-content;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
|
||||
border-color: var(--mui-grey-400);
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-radius: 0.5em;
|
||||
padding: 0.1em 0.5em;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--tags .tag-add {
|
||||
cursor: pointer;
|
||||
color: var(--mui-success-light);
|
||||
border-color: var(--mui-success-main);
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--tags .tag-add:hover {
|
||||
background-color: var(--mui-success-light);
|
||||
color: var(--mui-success-contrastText);
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--tags .tag .delete-tag {
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
.automation-list--metadatabox .metadatabox--tags .tag .delete-tag:hover {
|
||||
cursor: pointer;
|
||||
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;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
.automation-list.loading {
|
||||
min-height: 100%;
|
||||
height: 100%;
|
||||
max-height: 100vh;
|
||||
margin-left: 5em;
|
||||
margin-right: 5em;
|
||||
}
|
||||
|
||||
.automation-list .automation-list-box--list {
|
||||
max-height: 100vh;
|
||||
overflow: auto;
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
export type TagDB = ReturnType<typeof makeTagDB>;
|
||||
export const makeTagDB = (
|
||||
automations: Array<{
|
||||
id: string;
|
||||
tags: Record<string, string>;
|
||||
}>
|
||||
) => {
|
||||
const allTags: Record<string, Set<string>> = {};
|
||||
const automationTags: Record<string, Record<string, string>> = {};
|
||||
automations.forEach(({ id, tags }) => {
|
||||
automationTags[id] = tags;
|
||||
Object.keys(tags).forEach((tagName) => {
|
||||
const tagValue = tags[tagName];
|
||||
if (!allTags[tagName]) {
|
||||
allTags[tagName] = new Set();
|
||||
}
|
||||
allTags[tagName].add(tagValue);
|
||||
});
|
||||
});
|
||||
const allTagNames = Object.keys(allTags);
|
||||
return {
|
||||
getTags(automationId: string) {
|
||||
return automationTags[automationId] ?? {};
|
||||
},
|
||||
getTagNames(myTags: string[]): string[] {
|
||||
return allTagNames.filter((n) => !myTags.includes(n)).sort();
|
||||
},
|
||||
getTagValues(tagName: string): string[] {
|
||||
return allTags[tagName]
|
||||
? Array.from(allTags[tagName].values()).sort()
|
||||
: [];
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,17 +0,0 @@
|
||||
import { ListData } from "apiService/types";
|
||||
import { HAEntitiesState } from "haService/HAEntities";
|
||||
import { FC } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
export type AutomationListProps = {
|
||||
haEntites: HAEntitiesState;
|
||||
configAutomations: ListData<AutomationData>;
|
||||
onUpdate: (i: number, auto: AutomationData) => void;
|
||||
onUpdateTags: (id: string, tags: Record<string, string>) => void;
|
||||
onAdd: (auto: AutomationData) => void;
|
||||
onRemove: (i: number) => void;
|
||||
};
|
||||
|
||||
export const AutomationList: FC<AutomationListProps> = (props) => {
|
||||
return <></>;
|
||||
};
|
||||
+7
-7
@@ -1,19 +1,19 @@
|
||||
.automation-list--list-box-group--title {
|
||||
.automation-manager--list-box-group--title {
|
||||
margin: 0.5em;
|
||||
}
|
||||
|
||||
.automation-list--list-box-group--title .icon {
|
||||
.automation-manager--list-box-group--title .icon {
|
||||
font-size: 1.2em;
|
||||
}
|
||||
|
||||
.automation-list--list-box-group--title:hover,
|
||||
.automation-list--list-box-group.selected .automation-list--list-box-group--title {
|
||||
.automation-manager--list-box-group--title:hover,
|
||||
.automation-manager--list-box-group.selected .automation-manager--list-box-group--title {
|
||||
color: var(--mui-primary-main);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.automation-list--list-box-group {
|
||||
.automation-manager--list-box-group {
|
||||
border-color: var(--mui-text-secondary);
|
||||
border-width: 0.125px;
|
||||
border-bottom-style: solid;
|
||||
@@ -21,13 +21,13 @@
|
||||
border-bottom-right-radius: 0;
|
||||
}
|
||||
|
||||
.automation-list--list-box-group--list.close {
|
||||
.automation-manager--list-box-group--list.close {
|
||||
height: 0;
|
||||
opacity: 0;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.automation-list--list-box-group .automation-list--list-box-group {
|
||||
.automation-manager--list-box-group .automation-manager--list-box-group {
|
||||
margin-left: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
border-left: 1px solid var(--mui-text-secondary);
|
||||
File renamed without changes.
+3
-5
@@ -6,7 +6,6 @@ import { AutomationListAutoUpdatable, AutomationListItem } from "../types";
|
||||
import { TagDB } from "../TagDB";
|
||||
|
||||
export type ListBoxGroupProps = AutomationListItem & {
|
||||
onTagUpdate: (t: Record<string, string>, aid: string) => void;
|
||||
onAutomationUpdate: (
|
||||
a: AutomationListAutoUpdatable,
|
||||
aid: string,
|
||||
@@ -38,13 +37,13 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"automation-list--list-box-group",
|
||||
"automation-manager--list-box-group",
|
||||
isSelected ? "selected" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<div
|
||||
onClick={() => setOpen(!open)}
|
||||
className="automation-list--list-box-group--title"
|
||||
className="automation-manager--list-box-group--title"
|
||||
>
|
||||
<b>{title}</b> <span className="icon">{openIcon}</span> (
|
||||
{totalAutomations} automation
|
||||
@@ -52,7 +51,7 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
|
||||
</div>
|
||||
<div
|
||||
className={[
|
||||
"automation-list--list-box-group--list",
|
||||
"automation-manager--list-box-group--list",
|
||||
open ? "open" : "close",
|
||||
].join(" ")}
|
||||
>
|
||||
@@ -64,7 +63,6 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
|
||||
tagsDB={tagsDB}
|
||||
onDelete={() => events.onAutomationDelete(auto.id)}
|
||||
onSelect={() => events.onSelect(auto.id)}
|
||||
onTagUpdate={(t) => events.onTagUpdate(t, auto.id)}
|
||||
onDescriptionUpdate={(description) =>
|
||||
events.onAutomationUpdate(
|
||||
{
|
||||
@@ -0,0 +1,123 @@
|
||||
.automation-manager--metadatabox .metadatabox--tags {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.25em;
|
||||
margin-top: 0.25em;
|
||||
padding: 0.25em;
|
||||
}
|
||||
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags.modified {
|
||||
border: 1px solid var(--mui-success-light);
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag,
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-add {
|
||||
display: flex;
|
||||
max-width: fit-content;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
|
||||
border-color: var(--mui-grey-400);
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-radius: 0.5em;
|
||||
padding: 0.1em 0.5em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-add {
|
||||
cursor: pointer;
|
||||
color: var(--mui-primary-light);
|
||||
border-color: var(--mui-primary-main);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-save {
|
||||
cursor: pointer;
|
||||
border-radius: 0.25em;
|
||||
color: var(--mui-success-main);
|
||||
height: 1em;
|
||||
padding: 0.1em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-add:hover {
|
||||
background-color: var(--mui-primary-light);
|
||||
color: var(--mui-primary-contrastText);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-save:hover {
|
||||
background-color: var(--mui-success-light);
|
||||
color: var(--mui-success-contrastText);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .delete-tag {
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .delete-tag:hover {
|
||||
cursor: pointer;
|
||||
color: var(--mui-error-light);
|
||||
outline: 1px solid var(--mui-error-dark);
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .inner {
|
||||
font-size: 1em;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
|
||||
cursor: text;
|
||||
user-select: none;
|
||||
|
||||
text-overflow: ellipsis;
|
||||
overflow: hidden;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .inner span,
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .inner b {
|
||||
font-size: 0.9em;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .edit-icon {
|
||||
height: 0.7em;
|
||||
width: 0.7em;
|
||||
padding: 0.15em;
|
||||
cursor: text;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag:hover .edit-icon {
|
||||
transform: rotate(10deg);
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
+50
-21
@@ -1,7 +1,10 @@
|
||||
import "./Tags.css";
|
||||
|
||||
import { FC, ReactNode, useState } from "react";
|
||||
|
||||
import DeleteIcon from "@mui/icons-material/ClearTwoTone";
|
||||
import EditIcon from "@mui/icons-material/EditOutlined";
|
||||
import SaveIcon from "@mui/icons-material/SaveOutlined";
|
||||
|
||||
import { cleanUpUndefined } from "utils/helpers";
|
||||
import { Modal } from "components/Modal";
|
||||
@@ -11,8 +14,7 @@ import InputAutoText from "components/Inputs/InputAutoText";
|
||||
import { TagDB } from "../TagDB";
|
||||
|
||||
export type TagsProps = {
|
||||
tags: Record<string, string>;
|
||||
onUpdate: (t: Record<string, string>) => void;
|
||||
automationId: string;
|
||||
tagsDB: TagDB;
|
||||
};
|
||||
|
||||
@@ -26,8 +28,11 @@ export type TagModalState =
|
||||
isNew: boolean;
|
||||
};
|
||||
|
||||
export const Tags: FC<TagsProps> = ({ tags, onUpdate, tagsDB }) => {
|
||||
export const Tags: FC<TagsProps> = ({ automationId, tagsDB }) => {
|
||||
const [modalState, setModal] = useState<TagModalState>({ open: false });
|
||||
const tags = tagsDB.getTags(automationId);
|
||||
const onUpdate = (t: Record<string, string>) =>
|
||||
tagsDB.update(automationId, t);
|
||||
|
||||
let innerModal: ReactNode = <></>;
|
||||
if (modalState.open) {
|
||||
@@ -83,10 +88,15 @@ export const Tags: FC<TagsProps> = ({ tags, onUpdate, tagsDB }) => {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="metadatabox--tags">
|
||||
<div
|
||||
className={[
|
||||
"metadatabox--tags",
|
||||
tagsDB.isModified(automationId) ? "modified" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<Modal open={modalState.open}>{innerModal}</Modal>
|
||||
{Object.entries(tags).map(([tagName, tagValue]) => (
|
||||
<span key={tagName} className="tag">
|
||||
<div key={tagName} className="tag">
|
||||
<DeleteIcon
|
||||
onClick={() =>
|
||||
onUpdate(
|
||||
@@ -98,7 +108,7 @@ export const Tags: FC<TagsProps> = ({ tags, onUpdate, tagsDB }) => {
|
||||
}
|
||||
className="delete-tag"
|
||||
/>
|
||||
<span
|
||||
<div
|
||||
className="inner"
|
||||
onClick={() =>
|
||||
setModal({
|
||||
@@ -108,22 +118,41 @@ export const Tags: FC<TagsProps> = ({ tags, onUpdate, tagsDB }) => {
|
||||
})
|
||||
}
|
||||
>
|
||||
<b>{tagName}</b>: {tagValue} <EditIcon className="edit-icon" />
|
||||
</span>
|
||||
</span>
|
||||
<b>{tagName}: </b>
|
||||
<span>{tagValue}</span>
|
||||
</div>
|
||||
<EditIcon
|
||||
className="edit-icon"
|
||||
onClick={() =>
|
||||
setModal({
|
||||
open: true,
|
||||
isNew: false,
|
||||
tag: [tagName, tagValue],
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
<span
|
||||
className="tag-add"
|
||||
onClick={() =>
|
||||
setModal({
|
||||
open: true,
|
||||
isNew: true,
|
||||
tag: ["", ""],
|
||||
})
|
||||
}
|
||||
>
|
||||
+
|
||||
</span>
|
||||
<div className="buttons">
|
||||
<span
|
||||
className="tag-add"
|
||||
onClick={() =>
|
||||
setModal({
|
||||
open: true,
|
||||
isNew: true,
|
||||
tag: ["", ""],
|
||||
})
|
||||
}
|
||||
>
|
||||
+
|
||||
</span>
|
||||
{tagsDB.isModified(automationId) && (
|
||||
<SaveIcon
|
||||
className="tag-save"
|
||||
onClick={() => tagsDB.save(automationId)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,74 @@
|
||||
.automation-manager--metadatabox {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: space-around;
|
||||
gap: 0.25em;
|
||||
|
||||
padding: 0.25em;
|
||||
background-color: var(--mui-background-paper);
|
||||
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-color: transparent;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox:hover, .automation-manager--metadatabox.selected {
|
||||
border-color: var(--mui-action-focus);
|
||||
background-color: var(--mui-action-focus);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .input-text-view {
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .title {
|
||||
font-size: 1.25em;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .issue {
|
||||
padding: 0 0.5em;
|
||||
user-select: none;
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .description {
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .delete:hover {
|
||||
cursor: pointer;
|
||||
color: var(--mui-error-light);
|
||||
background-color: var(--mui-error-dark);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .select:hover,
|
||||
.automation-manager--metadatabox.selected.automation-manager--metadatabox .select {
|
||||
cursor: pointer;
|
||||
color: var(--mui-primary-light);
|
||||
background-color: var(--mui-primary-dark);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox.selected.automation-manager--metadatabox .select:hover {
|
||||
color: var(--mui-secondary-light);
|
||||
background-color: var(--mui-secondary-dark);
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--switch {
|
||||
display: flex;
|
||||
cursor: pointer;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
font-size: 0.75em;
|
||||
user-select: none;
|
||||
max-width: 5em;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
.automation-manager--metadatabox.mobile .buttons {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5em;
|
||||
}
|
||||
+15
-2
@@ -2,13 +2,15 @@ import "styles/root.css";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { MetadataBox } from ".";
|
||||
import { makeTagDB } from "../TagDB";
|
||||
import { useTagDB } from "../TagDB";
|
||||
import { useState } from "react";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/MetadataBox",
|
||||
component: MetadataBox,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
id: "dummy",
|
||||
title: "Example",
|
||||
description: "Cool!",
|
||||
state: "on",
|
||||
@@ -21,9 +23,20 @@ export default {
|
||||
} as ComponentMeta<typeof MetadataBox>;
|
||||
|
||||
const Template: ComponentStory<typeof MetadataBox> = (args) => {
|
||||
const [autoTags, setTags] = useState([{ id: args.id, tags: args.tags }]);
|
||||
const tagsDB = useTagDB(autoTags, (aid, t) =>
|
||||
setTags(
|
||||
autoTags
|
||||
.filter(({ id }) => id !== aid)
|
||||
.concat({
|
||||
id: aid,
|
||||
tags: t,
|
||||
})
|
||||
)
|
||||
);
|
||||
return (
|
||||
<Page>
|
||||
<MetadataBox {...args} tagsDB={makeTagDB([])} />
|
||||
<MetadataBox {...args} tagsDB={tagsDB} />
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
+23
-18
@@ -1,4 +1,5 @@
|
||||
import "./index.css";
|
||||
import "./index.mobile.css";
|
||||
import { FC } from "react";
|
||||
|
||||
import Switch from "@mui/material/Switch";
|
||||
@@ -12,23 +13,26 @@ import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { AutomationListAuto } from "../types";
|
||||
import { Tags } from "./Tags";
|
||||
import { TagDB } from "../TagDB";
|
||||
import useWindowSize from "utils/useWindowSize";
|
||||
|
||||
export type MetadataBoxProps = AutomationListAuto & {
|
||||
onDelete: () => void;
|
||||
onSelect: () => void;
|
||||
onStateUpdate: (s: "on" | "off") => void;
|
||||
onTagUpdate: (t: Record<string, string>) => void;
|
||||
onTitleUpdate: (t: string) => void;
|
||||
onDescriptionUpdate: (t: string) => void;
|
||||
tagsDB: TagDB;
|
||||
isSelected: boolean;
|
||||
};
|
||||
export const MetadataBox: FC<MetadataBoxProps> = (props) => {
|
||||
const isValidState = ["on", "off"].includes(props.state);
|
||||
const { isMobile } = useWindowSize();
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"automation-list--metadatabox",
|
||||
"automation-manager--metadatabox",
|
||||
props.isSelected ? "selected" : "",
|
||||
isMobile ? "mobile" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<div
|
||||
@@ -45,7 +49,10 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
|
||||
/>
|
||||
{!isValidState && `Invalid state: '${props.state}'`}
|
||||
</div>
|
||||
<div className="metadatabox--title">
|
||||
<div
|
||||
className="metadatabox--title"
|
||||
title={`ID=${props.id}, EntityID=${props.entityId}`}
|
||||
>
|
||||
<InputTextView
|
||||
className="title"
|
||||
placeholder="Title"
|
||||
@@ -58,11 +65,7 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
|
||||
value={props.description}
|
||||
onChange={props.onDescriptionUpdate}
|
||||
/>
|
||||
<Tags
|
||||
tags={props.tags}
|
||||
onUpdate={props.onTagUpdate}
|
||||
tagsDB={props.tagsDB}
|
||||
/>
|
||||
<Tags automationId={props.id} tagsDB={props.tagsDB} />
|
||||
{props.issue ? (
|
||||
<Alert severity="warning" className="issue">
|
||||
{props.issue}
|
||||
@@ -71,16 +74,18 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
|
||||
<></>
|
||||
)}
|
||||
</div>
|
||||
<ButtonIcon
|
||||
className="select"
|
||||
icon={<SelectIcon />}
|
||||
onClick={props.onSelect}
|
||||
/>
|
||||
<ButtonIcon
|
||||
className="delete"
|
||||
icon={<DeleteForeverIcon />}
|
||||
onClick={props.onDelete}
|
||||
/>
|
||||
<div className="buttons">
|
||||
<ButtonIcon
|
||||
className="select"
|
||||
icon={<SelectIcon />}
|
||||
onClick={props.onSelect}
|
||||
/>
|
||||
<ButtonIcon
|
||||
className="delete"
|
||||
icon={<DeleteForeverIcon />}
|
||||
onClick={props.onDelete}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
.automation-manager--sidebar {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
--nav-height: 10em;
|
||||
--bottom-height: 3em;
|
||||
position: relative;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.automation-manager--sidebar-box--nav {
|
||||
max-height: var(--nav-height);
|
||||
}
|
||||
|
||||
.automation-manager--sidebar .automation-manager--sidebar-box--list {
|
||||
flex: 1;
|
||||
max-height: calc(100vh - var(--bottom-height) - var(--nav-height));
|
||||
min-height: calc(100vh - var(--bottom-height) - var(--nav-height));
|
||||
overflow: auto;
|
||||
box-shadow: 1px 1px var(--mui-primary-light);
|
||||
}
|
||||
|
||||
.automation-manager--sidebar-box--bottom-buttons {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
+40
-27
@@ -2,7 +2,7 @@ import "styles/root.css";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { AutomationListSidebar } from ".";
|
||||
import { makeTagDB } from "../TagDB";
|
||||
import { useTagDB } from "../TagDB";
|
||||
import { useState } from "react";
|
||||
import { AutomationListAuto } from "../types";
|
||||
|
||||
@@ -15,11 +15,32 @@ export default {
|
||||
|
||||
const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
const [automations, setAutomations] = useState(args.automations);
|
||||
const [selectedAutomationId, setSelAutoId] = useState(
|
||||
args.selectedAutomationId
|
||||
);
|
||||
const onTagUpdate = (tags: Record<string, string>, aid: string) => {
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...automations[index],
|
||||
tags,
|
||||
},
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<Page>
|
||||
<AutomationListSidebar
|
||||
tagsDB={makeTagDB(automations)}
|
||||
selectedAutomationId={selectedAutomationId}
|
||||
tagsDB={useTagDB(automations, (a, t) => onTagUpdate(t, a))}
|
||||
automations={automations}
|
||||
onSelectedAutomationId={(aid) => {
|
||||
args.onSelectedAutomationId(aid);
|
||||
setSelAutoId(aid);
|
||||
}}
|
||||
onAutomationDelete={(aid) => {
|
||||
args.onAutomationDelete(aid);
|
||||
setAutomations(automations.filter(({ id }) => id !== aid));
|
||||
@@ -38,20 +59,6 @@ const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
]);
|
||||
}
|
||||
}}
|
||||
onTagUpdate={(tags, aid) => {
|
||||
args.onTagUpdate(tags, aid);
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...automations[index],
|
||||
tags,
|
||||
},
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
}}
|
||||
onAutomationAdd={() => {
|
||||
args.onAutomationAdd();
|
||||
const id = String(Date.now());
|
||||
@@ -63,10 +70,10 @@ const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
title: "new",
|
||||
tags: {},
|
||||
entityId: "automation." + id,
|
||||
isSelected: true,
|
||||
state: "on",
|
||||
},
|
||||
]);
|
||||
setSelAutoId(id);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
@@ -81,16 +88,18 @@ const make = (automations: AutomationListAuto[]) => {
|
||||
};
|
||||
return Basic;
|
||||
};
|
||||
const makeAuto = (data: Partial<AutomationListAuto>): AutomationListAuto => ({
|
||||
id: String(Date.now()),
|
||||
entityId: `automation.${Date.now()}`,
|
||||
title: "",
|
||||
description: "",
|
||||
state: "on",
|
||||
isSelected: false,
|
||||
tags: {},
|
||||
...data,
|
||||
});
|
||||
const makeAuto = (data: Partial<AutomationListAuto>): AutomationListAuto => {
|
||||
const id = `${Date.now()}_${Math.random().toString(26).slice(3, 6)}`;
|
||||
return {
|
||||
id,
|
||||
entityId: `automation.${id}`,
|
||||
title: "",
|
||||
description: "",
|
||||
state: "on",
|
||||
tags: {},
|
||||
...data,
|
||||
};
|
||||
};
|
||||
|
||||
export const Empty = make([]);
|
||||
export const Basic = make([
|
||||
@@ -110,4 +119,8 @@ export const Tagged = make([
|
||||
title: "example 4",
|
||||
tags: { room: "bedroom", floor: "2", type: "button" },
|
||||
}),
|
||||
makeAuto({
|
||||
title: "example 5",
|
||||
tags: { room: "bedroom", floor: "2", type: "routine", mode: "party" },
|
||||
}),
|
||||
]);
|
||||
+10
-9
@@ -15,7 +15,7 @@ import { Button } from "components/Inputs/Button";
|
||||
export type AutomationListSidebarProps = {
|
||||
tagsDB: TagDB;
|
||||
automations: AutomationListAuto[];
|
||||
onTagUpdate: (t: Record<string, string>, aid: string) => void;
|
||||
selectedAutomationId: string | null;
|
||||
onAutomationUpdate: (
|
||||
a: AutomationListAutoUpdatable,
|
||||
aid: string,
|
||||
@@ -23,17 +23,18 @@ export type AutomationListSidebarProps = {
|
||||
) => void;
|
||||
onAutomationDelete: (aid: string) => void;
|
||||
onAutomationAdd: () => void;
|
||||
onSelectedAutomationId: (aid: string) => void;
|
||||
};
|
||||
export const AutomationListSidebar: FC<AutomationListSidebarProps> = ({
|
||||
automations,
|
||||
tagsDB,
|
||||
onAutomationAdd,
|
||||
selectedAutomationId,
|
||||
onSelectedAutomationId,
|
||||
...events
|
||||
}) => {
|
||||
const [searchText, setSearchText] = useState("");
|
||||
const [selectedTagIdx, setSelectedTagIdx] = useState<number[]>([]);
|
||||
const [selectedAutomationId, setSelectedAutomationId] =
|
||||
useState<null | string>(null);
|
||||
|
||||
const tags = tagsDB.getTagNames([]);
|
||||
|
||||
@@ -51,12 +52,12 @@ export const AutomationListSidebar: FC<AutomationListSidebarProps> = ({
|
||||
);
|
||||
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, automations, grouping)
|
||||
convertGroupingToItems(i, selectedAutomationId, automations, grouping)
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="automation-list">
|
||||
<div className="automation-list-box--nav">
|
||||
<div className="automation-manager--sidebar">
|
||||
<div className="automation-manager--sidebar-box--nav">
|
||||
<InputText label="Search" value={searchText} onChange={setSearchText} />
|
||||
<InputMultiSelect
|
||||
label="Tags"
|
||||
@@ -66,19 +67,19 @@ export const AutomationListSidebar: FC<AutomationListSidebarProps> = ({
|
||||
max={3}
|
||||
/>
|
||||
</div>
|
||||
<div className="automation-list-box--list">
|
||||
<div className="automation-manager--sidebar-box--list">
|
||||
{items.map((item, i) => (
|
||||
<ListBoxGroup
|
||||
{...item}
|
||||
key={i}
|
||||
onSelect={setSelectedAutomationId}
|
||||
onSelect={onSelectedAutomationId}
|
||||
{...events}
|
||||
tagsDB={tagsDB}
|
||||
initialOpenState={true}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<div className="automation-list-box--bottom">
|
||||
<div className="automation-manager--sidebar-box--bottom-buttons">
|
||||
<Button onClick={onAutomationAdd} color="primary">
|
||||
Add
|
||||
</Button>
|
||||
@@ -0,0 +1,93 @@
|
||||
import { useEffect, useState } from "react";
|
||||
export type TagDB = ReturnType<typeof useTagDB>;
|
||||
export const useTagDB = (
|
||||
automations: Array<{
|
||||
id: string;
|
||||
tags: Record<string, string>;
|
||||
}>,
|
||||
onUpdate: (aid: string, tags: Record<string, string>) => void
|
||||
) => {
|
||||
const [{ automationTags, changed }, setState] = useState({
|
||||
automationTags: convertAutoListToMap(automations),
|
||||
changed: new Set<string>(),
|
||||
});
|
||||
const allTags = computeAllTags(automationTags);
|
||||
const allTagNames = Object.keys(allTags);
|
||||
const isModified = changed.size > 0;
|
||||
|
||||
useEffect(() => {
|
||||
console.log("updating...");
|
||||
setState({
|
||||
automationTags: convertAutoListToMap(automations),
|
||||
changed: new Set(),
|
||||
});
|
||||
}, [automations, setState]);
|
||||
|
||||
return {
|
||||
getTags(automationId: string) {
|
||||
return automationTags[automationId] ?? {};
|
||||
},
|
||||
getTagNames(myTags: string[]): string[] {
|
||||
return allTagNames.filter((n) => !myTags.includes(n)).sort();
|
||||
},
|
||||
getTagValues(tagName: string): string[] {
|
||||
return allTags[tagName]
|
||||
? Array.from(allTags[tagName].values()).sort()
|
||||
: [];
|
||||
},
|
||||
isModified(aid: string) {
|
||||
return changed.has(aid);
|
||||
},
|
||||
update(aid: string, tags: Record<string, string>) {
|
||||
if (JSON.stringify(tags) !== JSON.stringify(automationTags[aid])) {
|
||||
setState({
|
||||
changed: changed.add(aid),
|
||||
automationTags: {
|
||||
...automationTags,
|
||||
[aid]: tags,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
save(aid: string | null) {
|
||||
if (isModified) {
|
||||
if (aid) {
|
||||
onUpdate(aid, automationTags[aid]);
|
||||
} else {
|
||||
for (const aid of changed.values()) {
|
||||
onUpdate(aid, automationTags[aid]);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const convertAutoListToMap = (
|
||||
automations: Array<{
|
||||
id: string;
|
||||
tags: Record<string, string>;
|
||||
}>
|
||||
) => {
|
||||
const automationTags: Record<string, Record<string, string>> = {};
|
||||
automations.forEach(({ id, tags }) => {
|
||||
automationTags[id] = tags;
|
||||
});
|
||||
return automationTags;
|
||||
};
|
||||
|
||||
const computeAllTags = (
|
||||
automations: Record<string, Record<string, string>>
|
||||
) => {
|
||||
const allTags: Record<string, Set<string>> = {};
|
||||
Object.values(automations).forEach((tags) => {
|
||||
Object.keys(tags).forEach((tagName) => {
|
||||
const tagValue = tags[tagName];
|
||||
if (!allTags[tagName]) {
|
||||
allTags[tagName] = new Set();
|
||||
}
|
||||
allTags[tagName].add(tagValue);
|
||||
});
|
||||
});
|
||||
return allTags;
|
||||
};
|
||||
+3
-3
@@ -99,7 +99,7 @@ test("bigAutoList groupings has not duplicate ids", () => {
|
||||
test("convert grouper to items when no tags are selected", () => {
|
||||
const grouping = makeGrouping(automations, [], null);
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, automations, grouping)
|
||||
convertGroupingToItems(i, "", automations, grouping)
|
||||
);
|
||||
expect(items.filter((i) => i.type === "group")).toHaveLength(0);
|
||||
expect(items).toHaveLength(1);
|
||||
@@ -109,7 +109,7 @@ test("convert grouper to items when no tags are selected", () => {
|
||||
test("convert grouper to items when 1 tag is selected", () => {
|
||||
const grouping = makeGrouping(automations, ["Room"], null);
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, automations, grouping)
|
||||
convertGroupingToItems(i, "", automations, grouping)
|
||||
);
|
||||
expect(grouping.top).toHaveLength(3);
|
||||
expect(items.filter((i) => i.type === "group")).toHaveLength(0);
|
||||
@@ -125,7 +125,7 @@ test("convert grouper to items when 1 tag is selected", () => {
|
||||
test("convert grouper to items when 2 tag is selected", () => {
|
||||
const grouping = makeGrouping(automations, ["Room", "Type"], null);
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, automations, grouping)
|
||||
convertGroupingToItems(i, "", automations, grouping)
|
||||
);
|
||||
expect(grouping.top).toHaveLength(3);
|
||||
expect(items.filter((i) => i.type === "group")).toHaveLength(2);
|
||||
+10
-2
@@ -149,6 +149,7 @@ export const makeGrouping = (
|
||||
|
||||
export const convertGroupingToItems = (
|
||||
currentGroupIdx: number,
|
||||
selectedAutomationId: string | null,
|
||||
autos: AutomationListAuto[],
|
||||
grouping: AutomationGrouping
|
||||
): AutomationListItem => {
|
||||
@@ -159,7 +160,12 @@ export const convertGroupingToItems = (
|
||||
return {
|
||||
type: "items",
|
||||
title: groupData.name,
|
||||
data: automationIds.map((i) => autos[i]),
|
||||
data: automationIds.map((i) => {
|
||||
return {
|
||||
...autos[i],
|
||||
isSelected: autos[i].id === selectedAutomationId,
|
||||
};
|
||||
}),
|
||||
isSelected: groupData.selected,
|
||||
};
|
||||
}
|
||||
@@ -167,7 +173,9 @@ export const convertGroupingToItems = (
|
||||
return {
|
||||
type: "group",
|
||||
title: groupData.name,
|
||||
data: subgroups.map((i) => convertGroupingToItems(i, autos, grouping)),
|
||||
data: subgroups.map((i) =>
|
||||
convertGroupingToItems(i, selectedAutomationId, autos, grouping)
|
||||
),
|
||||
isSelected: groupData.selected,
|
||||
};
|
||||
};
|
||||
-3
@@ -6,7 +6,6 @@ import { AutomationListAuto } from "./types";
|
||||
export const consolidateAutomations = (
|
||||
haEntities: HassEntities,
|
||||
configAutomationMetadatas: AutomationMetadata[],
|
||||
selectedAutomationId: string,
|
||||
tagsDB: TagDB
|
||||
): AutomationListAuto[] => {
|
||||
const configData: Record<string, AutomationMetadata> =
|
||||
@@ -45,7 +44,6 @@ export const consolidateAutomations = (
|
||||
state: entityData.state,
|
||||
tags: tagsDB.getTags(autoId),
|
||||
issue,
|
||||
isSelected: selectedAutomationId === autoId,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -57,7 +55,6 @@ export const consolidateAutomations = (
|
||||
description: autoMetadata.description ?? "",
|
||||
state: "unregistered",
|
||||
tags: tagsDB.getTags(autoMetadata.id),
|
||||
isSelected: selectedAutomationId === autoMetadata.id,
|
||||
issue:
|
||||
"homeassistant did not load this automation, try to manually reload automations or restart homeassistant.",
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { ListData } from "apiService/types";
|
||||
import { HAEntitiesState } from "haService/HAEntities";
|
||||
import { FC } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { consolidateAutomations } from "./helpers";
|
||||
import { AutomationListSidebar } from "./Sidebar";
|
||||
import { useAutomationManagerState } from "./state";
|
||||
import { useTagDB } from "./TagDB";
|
||||
|
||||
export type AutomationListProps = {
|
||||
haEntites: HAEntitiesState;
|
||||
configAutomations: ListData<AutomationData>;
|
||||
onUpdate: (i: number, auto: AutomationData) => void;
|
||||
onUpdateTags: (id: string, tags: Record<string, string>) => void;
|
||||
onUpdateState: (id: string, state: string) => void;
|
||||
onTrigger: (id: string, state: string) => void;
|
||||
onAdd: () => void;
|
||||
onRemove: (i: number) => void;
|
||||
};
|
||||
|
||||
export const AutomationList: FC<AutomationListProps> = (props) => {
|
||||
const state = useAutomationManagerState();
|
||||
|
||||
if (!props.haEntites.ready) {
|
||||
return <div className="automation-manager loading">...</div>;
|
||||
}
|
||||
|
||||
const tagsDB = useTagDB(
|
||||
props.configAutomations.data.map(({ metadata: { id }, tags }) => ({
|
||||
id,
|
||||
tags,
|
||||
})),
|
||||
props.onUpdateTags
|
||||
);
|
||||
const automations = consolidateAutomations(
|
||||
props.haEntites.collection.state,
|
||||
props.configAutomations.data.map(({ metadata }) => metadata),
|
||||
tagsDB
|
||||
);
|
||||
return (
|
||||
<div className="automation-manager">
|
||||
{/* <AutomationListSidebar
|
||||
automations={automations}
|
||||
tagsDB={tagsDB}
|
||||
selectedAutomationId={state.current}
|
||||
onTagUpdate
|
||||
/> */}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
import { useState } from "react";
|
||||
import { useCookies } from "react-cookie";
|
||||
|
||||
export const useAutomationManagerState = () => {
|
||||
const [
|
||||
cookies,
|
||||
setCookies,
|
||||
// eslint-disable-next-line
|
||||
_,
|
||||
] = useCookies(["selectedAutomation"]);
|
||||
let initialCurrent = null;
|
||||
try {
|
||||
initialCurrent = cookies.selectedAutomation ?? null;
|
||||
} catch (_) {}
|
||||
|
||||
const [current, setCurrent] = useState<string | null>(initialCurrent);
|
||||
|
||||
return {
|
||||
current,
|
||||
setCurrent(i: string | null) {
|
||||
setCurrent(i);
|
||||
setCookies("selectedAutomation", i === null ? undefined : i);
|
||||
},
|
||||
};
|
||||
};
|
||||
+1
-2
@@ -8,7 +8,6 @@ export type AutomationListAuto = AutomationListAutoUpdatable & {
|
||||
entityId: string;
|
||||
tags: Record<string, string>;
|
||||
issue?: string;
|
||||
isSelected: boolean;
|
||||
};
|
||||
|
||||
export type AutomationListItem = {
|
||||
@@ -21,6 +20,6 @@ export type AutomationListItem = {
|
||||
}
|
||||
| {
|
||||
type: "items";
|
||||
data: Array<AutomationListAuto>;
|
||||
data: Array<AutomationListAuto & { isSelected: boolean }>;
|
||||
}
|
||||
);
|
||||
@@ -27,7 +27,7 @@ export default function InputAutoText({
|
||||
freeSolo
|
||||
className={["input-autotext", className ?? ""].join(" ")}
|
||||
value={value}
|
||||
options={options}
|
||||
options={options.filter((opt) => opt !== value)}
|
||||
disableClearable
|
||||
onChange={(_e, v) => onChange(v === null ? "" : v)}
|
||||
renderInput={(params) => (
|
||||
|
||||
@@ -5,36 +5,37 @@ import { useEffect, useState } from "react";
|
||||
import InputMultiSelect from "./InputMultiSelect";
|
||||
|
||||
export default {
|
||||
title: 'Inputs/InputMultiSelect',
|
||||
title: "Inputs/InputMultiSelect",
|
||||
component: InputMultiSelect,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
} as ComponentMeta<typeof InputMultiSelect>;
|
||||
|
||||
|
||||
const Template: ComponentStory<typeof InputMultiSelect> = args => {
|
||||
|
||||
const [value, setValue] = useState<number[]>(args.selected)
|
||||
const Template: ComponentStory<typeof InputMultiSelect> = (args) => {
|
||||
const [value, setValue] = useState<number[]>(args.selected);
|
||||
|
||||
useEffect(() => {
|
||||
setValue(args.selected);
|
||||
}, [args.selected])
|
||||
}, [args.selected]);
|
||||
|
||||
return <Page>
|
||||
<InputMultiSelect {...args} onChange={a => {
|
||||
setValue(a);
|
||||
args.onChange(a);
|
||||
}} options={args.options} selected={value} />
|
||||
</Page>
|
||||
}
|
||||
return (
|
||||
<Page>
|
||||
<InputMultiSelect
|
||||
{...args}
|
||||
onChange={(a) => {
|
||||
setValue(a);
|
||||
args.onChange(a);
|
||||
}}
|
||||
options={args.options}
|
||||
selected={value}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const Simple = Template.bind({})
|
||||
export const Simple = Template.bind({});
|
||||
Simple.args = {
|
||||
label: "Room Tags",
|
||||
options: [
|
||||
"Bears",
|
||||
"Cats",
|
||||
"Kitchen",
|
||||
"Bathroom",
|
||||
],
|
||||
options: ["Bears", "Cats", "Kitchen", "Bathroom"],
|
||||
selected: [1],
|
||||
}
|
||||
max: 2,
|
||||
};
|
||||
@@ -1,7 +1,13 @@
|
||||
import { ArrowIcon } from "components/Icons";
|
||||
import { useInputMultiSelectStyles } from "./styles";
|
||||
import { useState } from 'react';
|
||||
import { useState } from "react";
|
||||
import CheckBoxOutlineBlankIcon from "@mui/icons-material/CheckBoxOutlineBlank";
|
||||
import CheckBoxIcon from "@mui/icons-material/CheckBox";
|
||||
|
||||
import Autocomplete from "@mui/material/Autocomplete";
|
||||
import TextField from "@mui/material/TextField";
|
||||
import Checkbox from "@mui/material/Checkbox";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
export interface Props<T extends string> {
|
||||
label: string;
|
||||
@@ -13,53 +19,86 @@ export interface Props<T extends string> {
|
||||
className?: string;
|
||||
}
|
||||
export default function InputMultiSelect<T extends string>({
|
||||
selected = [], options, onChange, label, max,
|
||||
selected = [],
|
||||
options,
|
||||
onChange,
|
||||
label,
|
||||
max = 3,
|
||||
}: Props<T>) {
|
||||
// state
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
// alias
|
||||
const maxedOut = max ? selected.length >= max : false;
|
||||
const toggle = () => setOpen(!open);
|
||||
|
||||
const snackbr = useSnackbar();
|
||||
return (
|
||||
<Autocomplete
|
||||
multiple
|
||||
options={options}
|
||||
value={selected.map((i) => options[i])}
|
||||
onChange={(_, newValues: T[] = []) => {
|
||||
if (newValues.length > max) {
|
||||
snackbr.enqueueSnackbar("Can only select up to 3 tags", {
|
||||
variant: "warning",
|
||||
autoHideDuration: 1500,
|
||||
});
|
||||
}
|
||||
onChange(
|
||||
newValues
|
||||
.map((v) => options.indexOf(v))
|
||||
.filter((v) => v > -1)
|
||||
.slice(0, max)
|
||||
);
|
||||
}}
|
||||
renderOption={(props, option, { selected }) => (
|
||||
<li {...props}>
|
||||
<Checkbox
|
||||
icon={<CheckBoxOutlineBlankIcon />}
|
||||
checkedIcon={<CheckBoxIcon />}
|
||||
style={{ marginRight: 8 }}
|
||||
checked={selected}
|
||||
/>
|
||||
{option}
|
||||
</li>
|
||||
)}
|
||||
renderInput={(params) => (
|
||||
<TextField {...params} label={label} variant="filled" />
|
||||
)}
|
||||
/>
|
||||
);
|
||||
// style
|
||||
const { classes } = useInputMultiSelectStyles({
|
||||
open,
|
||||
selected,
|
||||
maxedOut,
|
||||
});
|
||||
// const { classes } = useInputMultiSelectStyles({
|
||||
// open,
|
||||
// selected,
|
||||
// maxedOut,
|
||||
// });
|
||||
|
||||
|
||||
return <div className={classes.root}>
|
||||
<div className={classes.input} onClick={toggle}>
|
||||
<label>{label}</label>
|
||||
<span className={classes.numberSelected}>{selected.length}</span>
|
||||
<ArrowIcon color="white" className={classes.arrow} />
|
||||
<div className={classes.optionsWrapper}>
|
||||
{options.map((v, i) => {
|
||||
const selectedCount = selected.indexOf(i);
|
||||
const isSelected = selectedCount > -1;
|
||||
return <div
|
||||
key={i}
|
||||
title={(maxedOut && !isSelected) ? `Maximum allowed to select is ${max}.` : isSelected ? `Unselect ${v}` : `Select ${v}`}
|
||||
className={[classes.option, isSelected ? 'selected' : 'unselected'].join(' ')}
|
||||
onClick={() => {
|
||||
if (!isSelected) {
|
||||
if (!maxedOut) {
|
||||
onChange([...selected, i])
|
||||
}
|
||||
} else {
|
||||
onChange(
|
||||
selected.slice(0, selectedCount).concat(selected.slice(selectedCount + 1))
|
||||
)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<span>{v}</span>
|
||||
<span>{selectedCount}</span>
|
||||
</div>
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
// return <div className={classes.root}>
|
||||
// <div className={classes.input} onClick={toggle}>
|
||||
// <label>{label}</label>
|
||||
// <span className={classes.numberSelected}>{selected.length}</span>
|
||||
// <ArrowIcon color="white" className={classes.arrow} />
|
||||
// <div className={classes.optionsWrapper}>
|
||||
// {options.map((v, i) => {
|
||||
// const selectedCount = selected.indexOf(i);
|
||||
// const isSelected = selectedCount > -1;
|
||||
// return <div
|
||||
// key={i}
|
||||
// title={(maxedOut && !isSelected) ? `Maximum allowed to select is ${max}.` : isSelected ? `Unselect ${v}` : `Select ${v}`}
|
||||
// className={[classes.option, isSelected ? 'selected' : 'unselected'].join(' ')}
|
||||
// onClick={() => {
|
||||
// if (!isSelected) {
|
||||
// if (!maxedOut) {
|
||||
// onChange([...selected, i])
|
||||
// }
|
||||
// } else {
|
||||
// onChange(
|
||||
// selected.slice(0, selectedCount).concat(selected.slice(selectedCount + 1))
|
||||
// )
|
||||
// }
|
||||
// }}
|
||||
// >
|
||||
// <span>{v}</span>
|
||||
// <span>{selectedCount}</span>
|
||||
// </div>
|
||||
// })}
|
||||
// </div>
|
||||
// </div>
|
||||
// </div>
|
||||
}
|
||||
Reference in new issue
Block a user