From da699403df49b34df3fceec711170d74d194fb72 Mon Sep 17 00:00:00 2001 From: Ari Date: Sun, 19 Jun 2022 00:29:39 -0400 Subject: [PATCH] added boolean to serviceeditor helpertext --- .../components/Inputs/InputAutoComplete.tsx | 4 + webapp/src/components/Inputs/InputBoolean.tsx | 41 +++- webapp/src/components/Inputs/InputNumber.tsx | 7 +- .../ServiceEditor/ServiceEditorField.css | 14 ++ .../ServiceEditor/ServiceEditorField.tsx | 115 +++++------ .../ServiceEditor/index.stories.tsx | 188 +++++++++++++++--- webapp/src/haService/fieldTypes.ts | 90 ++++----- 7 files changed, 322 insertions(+), 137 deletions(-) diff --git a/webapp/src/components/Inputs/InputAutoComplete.tsx b/webapp/src/components/Inputs/InputAutoComplete.tsx index 578085b..f420b8d 100644 --- a/webapp/src/components/Inputs/InputAutoComplete.tsx +++ b/webapp/src/components/Inputs/InputAutoComplete.tsx @@ -197,6 +197,10 @@ export function InputAutoComplete( renderInput={(params) => ( void; disabled?: boolean; className?: string; required?: boolean; placeholder?: string; + endAdornment?: ReactNode; } export default function InputBoolean({ label, value = false, + title, + helperText, onChange, disabled = false, className, required, placeholder, + endAdornment, }: Props) { return ( - + } - label={label} + label={ + endAdornment ? ( +
+ + {label} + + {endAdornment} +
+ ) : ( + label + ) + } /> + {helperText}
); } diff --git a/webapp/src/components/Inputs/InputNumber.tsx b/webapp/src/components/Inputs/InputNumber.tsx index ee1514f..d16b318 100644 --- a/webapp/src/components/Inputs/InputNumber.tsx +++ b/webapp/src/components/Inputs/InputNumber.tsx @@ -3,6 +3,7 @@ import TextField from "@mui/material/TextField"; import IconButton from "@mui/material/IconButton"; import InputAdornment from "@mui/material/InputAdornment"; import Edit from "@mui/icons-material/Edit"; +import { ReactNode } from "react"; export interface Props { label: string | JSX.Element; @@ -15,6 +16,7 @@ export interface Props { unit?: string; placeholder?: any; required?: boolean; + endAdornment?: ReactNode; } export default function InputNumber({ label, @@ -27,6 +29,7 @@ export default function InputNumber({ unit, placeholder, required, + endAdornment, }: Props) { const disabled = value === undefined || value === null; let labelElm = <>; @@ -54,7 +57,9 @@ export default function InputNumber({ required={required} fullWidth InputProps={{ - endAdornment: ( + endAdornment: endAdornment ? ( + endAdornment + ) : ( onChange(disabled ? 0 : undefined)}> {disabled ? : } diff --git a/webapp/src/components/ServiceEditor/ServiceEditorField.css b/webapp/src/components/ServiceEditor/ServiceEditorField.css index 521a833..1c1fac7 100644 --- a/webapp/src/components/ServiceEditor/ServiceEditorField.css +++ b/webapp/src/components/ServiceEditor/ServiceEditorField.css @@ -2,4 +2,18 @@ display: flex; flex-direction: row; align-items: center; +} + +.service-editor--field .MuiFormHelperText-contained { + max-width: 100%; + overflow: hidden; +} + +.service-editor--field--description { + text-overflow: ellipsis; + overflow: hidden; + word-wrap: break-word; + white-space: nowrap; + width: 100%; + max-width: 100%; } \ No newline at end of file diff --git a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx index 065a2c7..a9dc84a 100644 --- a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx +++ b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx @@ -7,6 +7,8 @@ import { IconButton } from "@mui/material"; import { RemoveCircle } from "@mui/icons-material"; import { InputAutoComplete, Option } from "components/Inputs/InputAutoComplete"; import { prettyName } from "utils/formatting"; +import InputBoolean from "components/Inputs/InputBoolean"; +import { SelectorNumber, SelectorText } from "haService/fieldTypes"; export type ServiceEditorFieldProps = { option: ServiceEditorOption; @@ -32,64 +34,63 @@ export const ServiceEditorField: FC = ({ ); example = option.data.example ? `Example: ${option.data.example}` : ""; - if (option.data.selector?.number) { - return ( - - ); - } - if (option.data.selector?.select) { - return ( - ((opt) => { - if (typeof opt === "string") { + const prop = { + className: "service-editor--field", + label, + title: description, + helperText: ( +
{description}
+ ), + value: value ?? option.data.default, + onChange: onChange, + required: isRequired, + placeholder: example, + endAdornment: removeIcon, + }; + if (option.data.selector) { + if ("number" in option.data.selector) { + const options: Partial = + option.data.selector.number ?? {}; + return ( + + ); + } + if (option.data.selector?.select) { + return ( + ((opt) => { + if (typeof opt === "string") { + return { + id: opt, + label: prettyName(opt), + }; + } return { - id: opt, - label: prettyName(opt), + id: opt.value, + label: opt.label, }; - } - return { - id: opt.value, - label: opt.label, - }; - })} - getLabel={(opt) => { - if (typeof opt === "string") { - return prettyName(opt); - } - return opt.label; - }} - onlyShowLabel - /> - ); + })} + getLabel={(opt) => { + if (typeof opt === "string") { + return prettyName(opt); + } + return opt.label; + }} + onlyShowLabel + /> + ); + } else if ("text" in option.data.selector) { + const options: Partial = option.data.selector.text ?? {}; + return ; + } else if ("boolean" in option.data.selector) { + return ; + } } - return ( - - ); + return ; }; diff --git a/webapp/src/components/ServiceEditor/index.stories.tsx b/webapp/src/components/ServiceEditor/index.stories.tsx index d404f68..fa60e13 100644 --- a/webapp/src/components/ServiceEditor/index.stories.tsx +++ b/webapp/src/components/ServiceEditor/index.stories.tsx @@ -16,41 +16,52 @@ const Template: ComponentStory = (props) => { const [state, setState] = useState(props.data); return ( - { - setState(d); - props.onUpdate(d); - }} - />
- {}} /> -
-
- {}} /> -
-
- {}} + { + setState(d); + props.onUpdate(d); + }} /> +
+ {}} /> +
+
+ {}} + /> +
+
+ {}} + /> +
); @@ -427,3 +438,122 @@ export const LightTurnOn = makeExample({ }, }, }); + +export const UltimateService = makeExample({ + name: "Ultimate Service", + description: "Show a notification in the frontend.", + fields: { + message: { + name: "Text", + description: "Message body of the notification. [Templates accepted]", + example: "Please check your configuration.yaml.", + selector: { + text: null, + }, + }, + entity_id: { + name: "Entity with domain filter", + description: "Name(s) of media player entities.", + selector: { + entity: { + domain: "media_player", + }, + }, + }, + level: { + name: "Options", + description: "Log level.", + default: "error", + selector: { + select: { + options: [ + { + label: "Debug", + value: "debug", + }, + { + label: "Info", + value: "info", + }, + { + label: "Warning", + value: "warning", + }, + { + label: "Error", + value: "error", + }, + { + label: "Critical", + value: "critical", + }, + ], + }, + }, + }, + format: { + name: "String Options", + description: "Stream format supported by media player.", + default: "hls", + selector: { + select: { + options: ["hls"], + }, + }, + }, + keep_days: { + name: "Number", + description: "Number of history days to keep in database after purge.", + selector: { + number: { + min: 0, + max: 365, + unit_of_measurement: "days", + }, + }, + }, + repack: { + name: "Boolean", + description: + "Attempt to save disk space by rewriting the entire database file.", + default: false, + selector: { + boolean: null, + }, + }, + domains: { + name: "Objects", + description: + "List the domains that need to be removed from the recorder database.", + example: "sun", + default: [], + selector: { + object: null, + }, + }, + addon: { + name: "Add-on", + description: "The add-on slug.", + example: "core_ssh", + selector: { + addon: null, + }, + }, + name: { + name: "Theme", + description: "Name of a predefined theme", + example: "default", + selector: { + theme: null, + }, + }, + icon: { + name: "Icon", + description: "Name of icon for the group.", + example: "mdi:camera", + selector: { + icon: null, + }, + }, + }, +}); diff --git a/webapp/src/haService/fieldTypes.ts b/webapp/src/haService/fieldTypes.ts index 2961178..ec9612b 100644 --- a/webapp/src/haService/fieldTypes.ts +++ b/webapp/src/haService/fieldTypes.ts @@ -1,75 +1,69 @@ export interface TypedHassService { - name?: string; - description: string; - fields: Fields; - target?: Targets | null | undefined; + name?: string; + description: string; + fields: Fields; + target?: Targets | null | undefined; } -export type Fields = Record +export type Fields = Record; export interface Targets { - device?: TargetData; - entity?: TargetData; + device?: TargetData; + entity?: TargetData; } export interface TargetData { - domain?: string; - integration?: string; + domain?: string; + integration?: string; } export interface FieldData { - name?: string | undefined; - description: string; - required?: boolean; - example?: any; - selector?: FieldSelector; - advanced?: boolean; - default?: any; + name?: string | undefined; + description: string; + required?: boolean; + example?: any; + selector?: FieldSelector; + advanced?: boolean; + default?: any; } export interface FieldSelector { - text?: SelectorText | null; - select?: SelectorSelect; - number?: SelectorNumber; - boolean?: null; - object?: null; - theme?: null; - addon?: null; - entity?: TargetData; - icon?: null; - color_rgb?: null; - color_temp?: SelectorColorTemp | null; - time?: null; + text?: SelectorText | null; + select?: SelectorSelect; + number?: SelectorNumber; + boolean?: null; + object?: null; + theme?: null; + addon?: null; + entity?: TargetData; + icon?: null; + color_rgb?: null; + color_temp?: SelectorColorTemp | null; + time?: null; } -interface SelectorColorTemp { - min_mireds: number; - max_mireds: number; +export interface SelectorColorTemp { + min_mireds: number; + max_mireds: number; } -interface SelectorNumber { - min: number; - max: number; - unit_of_measurement?: string; - mode?: Mode; - step?: number; -} - -enum Mode { - Box = "box", +export interface SelectorNumber { + min: number; + max: number; + unit_of_measurement?: string; + mode?: "box"; + step?: number; } interface SelectorSelect { - options: OptionElement[]; + options: OptionElement[]; } type OptionElement = OptionClass | string; interface OptionClass { - label: string; - value: string; + label: string; + value: string; } -interface SelectorText { - multiline: boolean; +export interface SelectorText { + multiline: boolean; } - -