From fed45e1d78e05859a36a113b0166316024f63a0a Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Mon, 15 Nov 2021 20:26:23 -0500 Subject: [PATCH] - refactorted icons - condition box view and editor --- web/package.json | 2 + .../ConditionEditor/childrenRender.tsx | 42 +++++++++++++++++++ .../childrenRender/template.tsx | 0 .../ConditionEditor/index.stories.tsx | 9 ++-- web/src/components/ConditionEditor/index.tsx | 32 ++++++++++---- web/src/components/ConditionEditor/style.ts | 26 ++++++++++-- .../Inputs/InputViewEdit.stories.tsx | 24 +++++++++++ web/src/components/Inputs/InputViewEdit.tsx | 23 +++++----- web/src/components/Inputs/styles.ts | 15 +++++++ web/src/icons/IconBase.tsx | 28 +++++++++++++ web/src/icons/checkmark.tsx | 12 ------ web/src/icons/icons.stories.tsx | 18 ++++++++ web/src/icons/icons.tsx | 21 ++++++++++ web/src/icons/pencil.tsx | 22 ---------- web/yarn.lock | 19 +++++++++ 15 files changed, 235 insertions(+), 58 deletions(-) create mode 100644 web/src/components/ConditionEditor/childrenRender.tsx delete mode 100644 web/src/components/ConditionEditor/childrenRender/template.tsx create mode 100644 web/src/components/Inputs/InputViewEdit.stories.tsx create mode 100644 web/src/icons/IconBase.tsx delete mode 100644 web/src/icons/checkmark.tsx create mode 100644 web/src/icons/icons.stories.tsx create mode 100644 web/src/icons/icons.tsx delete mode 100644 web/src/icons/pencil.tsx diff --git a/web/package.json b/web/package.json index 689ed98..6df97bf 100644 --- a/web/package.json +++ b/web/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "chroma-js": "^2.1.2", "color": "^4.0.1", "js-yaml": "^4.1.0", "jss": "^10.8.2", @@ -19,6 +20,7 @@ "@storybook/addon-links": "^6.3.12", "@storybook/react": "^6.3.12", "@testing-library/jest-dom": "^5.15.0", + "@types/chroma-js": "^2.1.3", "@types/color": "^3.0.2", "@types/jest": "^27.0.2", "@types/js-yaml": "^4.0.4", diff --git a/web/src/components/ConditionEditor/childrenRender.tsx b/web/src/components/ConditionEditor/childrenRender.tsx new file mode 100644 index 0000000..3bbf638 --- /dev/null +++ b/web/src/components/ConditionEditor/childrenRender.tsx @@ -0,0 +1,42 @@ +import { FC } from "react"; +import { AutomationCondition, TemplateCondition } from "~/automations/types/conditions"; +import { getDescriptionFromAutomationNode } from "~/automations/utils"; +import InputText from "../Inputs/InputText"; + + +interface Editor extends FC<{ + condition: C, + onChange: (condition: C) => void; +}> {} + + +export const getEditor = (condition: AutomationCondition): Editor => { + switch (condition.condition) { + case 'template': + return TemplateEditor; + default: + return () =>
Not Ready
+ } +} + +export const TemplateEditor: Editor = ({ + onChange, + condition, +}) => { + return
+ onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + alias + } + })}/> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + value_template + } + })}/> +
+} \ No newline at end of file diff --git a/web/src/components/ConditionEditor/childrenRender/template.tsx b/web/src/components/ConditionEditor/childrenRender/template.tsx deleted file mode 100644 index e69de29..0000000 diff --git a/web/src/components/ConditionEditor/index.stories.tsx b/web/src/components/ConditionEditor/index.stories.tsx index feb4f9f..9915b89 100644 --- a/web/src/components/ConditionEditor/index.stories.tsx +++ b/web/src/components/ConditionEditor/index.stories.tsx @@ -1,4 +1,5 @@ import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useState } from "react"; import { usePageTheme } from "~/styles/page"; import { ConditionEditor } from "."; @@ -9,10 +10,11 @@ export default { } as ComponentMeta; -const Template: ComponentStory = args => { +const Template: ComponentStory = ({condition, ...args}) => { const {classes} = usePageTheme({}); + const [data, setData] = useState(condition); return
- +
} @@ -24,5 +26,6 @@ SimpleGraph.args = { condition_data: { value_template: "states('switch.light_kitchen') == 'on'" } - } + }, + displayMode: true, } \ No newline at end of file diff --git a/web/src/components/ConditionEditor/index.tsx b/web/src/components/ConditionEditor/index.tsx index 96a76ff..e3c066e 100644 --- a/web/src/components/ConditionEditor/index.tsx +++ b/web/src/components/ConditionEditor/index.tsx @@ -1,34 +1,47 @@ -import { FC } from "react"; +import { FC, useState } from "react"; import { AutomationCondition } from "~/automations/types/conditions"; import { getDescriptionFromAutomationNode } from "~/automations/utils"; +import { CheckMarkIcon, PencilIcon } from "~/icons/icons"; +import { getEditor } from "./childrenRender"; import { useStyles } from "./style"; export const ConditionEditor: FC<{ condition: AutomationCondition; + displayMode?: boolean; onDelete: (which: 'root' | number) => void; onAddChild: () => void; + onUpdate: (data: AutomationCondition) => void; }> = ({ condition, onDelete, + onUpdate, // onAddChild, }) => { - // alias - let children = getDescriptionFromAutomationNode(condition); + // state + const [displayMode, setDisplayMode] = useState(true); + + // children + let children: any = 'n/a' + if (displayMode) { + children = getDescriptionFromAutomationNode(condition); + } else { + const Editor = getEditor(condition); + children = { + console.log({data}) + onUpdate(data) + }}/> + } + // styles let hasChildren = false; if (condition.condition === "not") { - children = "not" hasChildren = true } else if (condition.condition ==='and') { - children = "and" hasChildren = true } else if (condition.condition ==='or') { - children = "or" hasChildren = true } - - // styles const {classes} = useStyles({ hasChildren }); // components @@ -43,5 +56,8 @@ export const ConditionEditor: FC<{
{children}
+ } \ No newline at end of file diff --git a/web/src/components/ConditionEditor/style.ts b/web/src/components/ConditionEditor/style.ts index c426890..a9d7a7f 100644 --- a/web/src/components/ConditionEditor/style.ts +++ b/web/src/components/ConditionEditor/style.ts @@ -1,4 +1,5 @@ import { Styles } from "jss"; +import Color from 'chroma-js'; import { createAppUseStyles } from "~/styles/theme"; export const useStyles = createAppUseStyles<{ @@ -45,6 +46,25 @@ export const useStyles = createAppUseStyles<{ left: -35, top: 10, }, + modifyBtn: { + backgroundColor: theme.primary, + fill: theme.primaryAccent, + border: `1px solid ${theme.primaryAccent}`, + borderRadius: 10, + borderTopLeftRadius: 0, + borderBottomLeftRadius: 0, + cursor: 'pointer', + "&:hover": { + backgroundColor: Color(theme.primary).brighten().hex(), + fill: Color(theme.primaryAccent).brighten().hex(), + }, + "&:hover $icon": { + transform: "rotate(90deg)" + } + }, + icon: { + transition: "200ms ease-out" + }, deleteBtn, children: ({}) => ({ flex: 1, @@ -54,9 +74,9 @@ export const useStyles = createAppUseStyles<{ justifyContent: 'center', alignItems: 'center', border: `1px solid ${theme.primaryAccent}`, - borderRadius: 10, - borderTopLeftRadius: 0, - borderBottomLeftRadius: 0, + // borderRadius: 10, + // borderTopLeftRadius: 0, + // borderBottomLeftRadius: 0, }) } }) \ No newline at end of file diff --git a/web/src/components/Inputs/InputViewEdit.stories.tsx b/web/src/components/Inputs/InputViewEdit.stories.tsx new file mode 100644 index 0000000..3e27e88 --- /dev/null +++ b/web/src/components/Inputs/InputViewEdit.stories.tsx @@ -0,0 +1,24 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useState } from "react"; +import { usePageTheme } from "~/styles/page"; +import InputViewEdit from "./InputViewEdit"; + +export default { + title: 'InputViewEdit', + component: InputViewEdit, + parameters: { actions: { argTypesRegex: '^on.*' } }, + } as ComponentMeta; + + +const Template: ComponentStory = args => { + const {classes} = usePageTheme({}); + const [value, setValue] = useState('') + return
+ +
+} + +export const SimpleText = Template.bind({}) +SimpleText.args = { + label: "Entity ID", +} \ No newline at end of file diff --git a/web/src/components/Inputs/InputViewEdit.tsx b/web/src/components/Inputs/InputViewEdit.tsx index 62cb96d..fb6e754 100644 --- a/web/src/components/Inputs/InputViewEdit.tsx +++ b/web/src/components/Inputs/InputViewEdit.tsx @@ -1,8 +1,8 @@ import { useRef, useState, useEffect, HTMLProps } from "react"; -import CheckMarkIcon from "~/icons/checkmark"; -import PencilIcon from "~/icons/pencil"; +import {CheckMarkIcon, PencilIcon} from "~/icons/icons"; import { useToolTip } from "~/tooltip/context"; import InputWrapper from "./InputWrapper"; +import { useInputViewEditStyles } from "./styles"; export interface Props { textBoxFor?: string; @@ -18,29 +18,32 @@ export default function InputViewEdit({ value="", onChange, additionalTooltipFilters={}, - useTextArea=false, }: Props) { + // state const tooltip = useToolTip(); const [isEditing, setIsEditing] = useState(false); + // alias for on calls const onChangeWrapped = (t: string) => { onChange(t); setIsEditing(false); } + // ref to grab focus when editing const inputRef = useRef(null); useEffect(() => { if (isEditing && inputRef.current) { + console.log('focusing...') inputRef.current.focus() } }, [inputRef.current, isEditing]); - const Text = useTextArea ? (p: HTMLProps) =>