added boolean to serviceeditor
helpertext
This commit is contained in:
1 parent
1496bc66ad
commit
da699403df
7 files changed
+322
-137
No files matched your search
@@ -197,6 +197,10 @@ export function InputAutoComplete<T extends Option>(
|
|||||||
renderInput={(params) => (
|
renderInput={(params) => (
|
||||||
<TextField
|
<TextField
|
||||||
{...params}
|
{...params}
|
||||||
|
InputProps={{
|
||||||
|
...params.InputProps,
|
||||||
|
endAdornment: props.endAdornment ?? params.InputProps.endAdornment,
|
||||||
|
}}
|
||||||
variant="filled"
|
variant="filled"
|
||||||
required={props.required}
|
required={props.required}
|
||||||
label={props.label ?? "Entity ID"}
|
label={props.label ?? "Entity ID"}
|
||||||
|
|||||||
@@ -1,27 +1,41 @@
|
|||||||
import Checkbox from "@mui/material/Checkbox";
|
import Checkbox from "@mui/material/Checkbox";
|
||||||
import FormGroup from "@mui/material/FormGroup";
|
import FormGroup from "@mui/material/FormGroup";
|
||||||
import FormControlLabel from "@mui/material/FormControlLabel";
|
import FormControlLabel from "@mui/material/FormControlLabel";
|
||||||
|
import { ReactNode } from "react";
|
||||||
|
import { FormHelperText, InputAdornment } from "@mui/material";
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
label: string;
|
label: string;
|
||||||
value: boolean;
|
value: boolean;
|
||||||
|
title?: string;
|
||||||
|
helperText?: string;
|
||||||
onChange: (v: boolean) => void;
|
onChange: (v: boolean) => void;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
className?: string;
|
className?: string;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
|
endAdornment?: ReactNode;
|
||||||
}
|
}
|
||||||
export default function InputBoolean({
|
export default function InputBoolean({
|
||||||
label,
|
label,
|
||||||
value = false,
|
value = false,
|
||||||
|
title,
|
||||||
|
helperText,
|
||||||
onChange,
|
onChange,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
className,
|
className,
|
||||||
required,
|
required,
|
||||||
placeholder,
|
placeholder,
|
||||||
|
endAdornment,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
return (
|
return (
|
||||||
<FormGroup className={className}>
|
<FormGroup
|
||||||
|
className={className}
|
||||||
|
title={title}
|
||||||
|
style={{
|
||||||
|
alignItems: "flex-start",
|
||||||
|
}}
|
||||||
|
>
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
control={
|
control={
|
||||||
@@ -32,8 +46,31 @@ export default function InputBoolean({
|
|||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
label={label}
|
label={
|
||||||
|
endAdornment ? (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "row",
|
||||||
|
justifyContent: "center",
|
||||||
|
alignItems: "center",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
style={{
|
||||||
|
flex: 1,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</span>
|
||||||
|
<InputAdornment position="end">{endAdornment}</InputAdornment>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
label
|
||||||
|
)
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
|
<FormHelperText>{helperText}</FormHelperText>
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ import TextField from "@mui/material/TextField";
|
|||||||
import IconButton from "@mui/material/IconButton";
|
import IconButton from "@mui/material/IconButton";
|
||||||
import InputAdornment from "@mui/material/InputAdornment";
|
import InputAdornment from "@mui/material/InputAdornment";
|
||||||
import Edit from "@mui/icons-material/Edit";
|
import Edit from "@mui/icons-material/Edit";
|
||||||
|
import { ReactNode } from "react";
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
label: string | JSX.Element;
|
label: string | JSX.Element;
|
||||||
@@ -15,6 +16,7 @@ export interface Props {
|
|||||||
unit?: string;
|
unit?: string;
|
||||||
placeholder?: any;
|
placeholder?: any;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
|
endAdornment?: ReactNode;
|
||||||
}
|
}
|
||||||
export default function InputNumber({
|
export default function InputNumber({
|
||||||
label,
|
label,
|
||||||
@@ -27,6 +29,7 @@ export default function InputNumber({
|
|||||||
unit,
|
unit,
|
||||||
placeholder,
|
placeholder,
|
||||||
required,
|
required,
|
||||||
|
endAdornment,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const disabled = value === undefined || value === null;
|
const disabled = value === undefined || value === null;
|
||||||
let labelElm = <></>;
|
let labelElm = <></>;
|
||||||
@@ -54,7 +57,9 @@ export default function InputNumber({
|
|||||||
required={required}
|
required={required}
|
||||||
fullWidth
|
fullWidth
|
||||||
InputProps={{
|
InputProps={{
|
||||||
endAdornment: (
|
endAdornment: endAdornment ? (
|
||||||
|
endAdornment
|
||||||
|
) : (
|
||||||
<InputAdornment position="end">
|
<InputAdornment position="end">
|
||||||
<IconButton onClick={() => onChange(disabled ? 0 : undefined)}>
|
<IconButton onClick={() => onChange(disabled ? 0 : undefined)}>
|
||||||
{disabled ? <Edit /> : <Close />}
|
{disabled ? <Edit /> : <Close />}
|
||||||
|
|||||||
@@ -2,4 +2,18 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
align-items: center;
|
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%;
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,8 @@ import { IconButton } from "@mui/material";
|
|||||||
import { RemoveCircle } from "@mui/icons-material";
|
import { RemoveCircle } from "@mui/icons-material";
|
||||||
import { InputAutoComplete, Option } from "components/Inputs/InputAutoComplete";
|
import { InputAutoComplete, Option } from "components/Inputs/InputAutoComplete";
|
||||||
import { prettyName } from "utils/formatting";
|
import { prettyName } from "utils/formatting";
|
||||||
|
import InputBoolean from "components/Inputs/InputBoolean";
|
||||||
|
import { SelectorNumber, SelectorText } from "haService/fieldTypes";
|
||||||
|
|
||||||
export type ServiceEditorFieldProps = {
|
export type ServiceEditorFieldProps = {
|
||||||
option: ServiceEditorOption;
|
option: ServiceEditorOption;
|
||||||
@@ -32,64 +34,63 @@ export const ServiceEditorField: FC<ServiceEditorFieldProps> = ({
|
|||||||
</IconButton>
|
</IconButton>
|
||||||
);
|
);
|
||||||
example = option.data.example ? `Example: ${option.data.example}` : "";
|
example = option.data.example ? `Example: ${option.data.example}` : "";
|
||||||
if (option.data.selector?.number) {
|
const prop = {
|
||||||
return (
|
className: "service-editor--field",
|
||||||
<InputNumber
|
label,
|
||||||
className="service-editor--field"
|
title: description,
|
||||||
label={label}
|
helperText: (
|
||||||
value={value ?? option.data.default}
|
<div className="service-editor--field--description">{description}</div>
|
||||||
min={option.data.selector.number.min}
|
),
|
||||||
max={option.data.selector.number.max}
|
value: value ?? option.data.default,
|
||||||
step={option.data.selector.number.step}
|
onChange: onChange,
|
||||||
unit={option.data.selector.number.unit_of_measurement}
|
required: isRequired,
|
||||||
onChange={onChange}
|
placeholder: example,
|
||||||
required={isRequired}
|
endAdornment: removeIcon,
|
||||||
placeholder={example}
|
};
|
||||||
/>
|
if (option.data.selector) {
|
||||||
);
|
if ("number" in option.data.selector) {
|
||||||
}
|
const options: Partial<SelectorNumber> =
|
||||||
if (option.data.selector?.select) {
|
option.data.selector.number ?? {};
|
||||||
return (
|
return (
|
||||||
<InputAutoComplete
|
<InputNumber
|
||||||
className="service-editor--field"
|
{...prop}
|
||||||
label={label}
|
min={options.min}
|
||||||
value={value}
|
max={options.max}
|
||||||
onChange={onChange}
|
step={options.step}
|
||||||
endAdornment={removeIcon}
|
/>
|
||||||
required={isRequired}
|
);
|
||||||
options={option.data.selector.select.options.map<Option>((opt) => {
|
}
|
||||||
if (typeof opt === "string") {
|
if (option.data.selector?.select) {
|
||||||
|
return (
|
||||||
|
<InputAutoComplete
|
||||||
|
{...prop}
|
||||||
|
options={option.data.selector.select.options.map<Option>((opt) => {
|
||||||
|
if (typeof opt === "string") {
|
||||||
|
return {
|
||||||
|
id: opt,
|
||||||
|
label: prettyName(opt),
|
||||||
|
};
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
id: opt,
|
id: opt.value,
|
||||||
label: prettyName(opt),
|
label: opt.label,
|
||||||
};
|
};
|
||||||
}
|
})}
|
||||||
return {
|
getLabel={(opt) => {
|
||||||
id: opt.value,
|
if (typeof opt === "string") {
|
||||||
label: opt.label,
|
return prettyName(opt);
|
||||||
};
|
}
|
||||||
})}
|
return opt.label;
|
||||||
getLabel={(opt) => {
|
}}
|
||||||
if (typeof opt === "string") {
|
onlyShowLabel
|
||||||
return prettyName(opt);
|
/>
|
||||||
}
|
);
|
||||||
return opt.label;
|
} else if ("text" in option.data.selector) {
|
||||||
}}
|
const options: Partial<SelectorText> = option.data.selector.text ?? {};
|
||||||
onlyShowLabel
|
return <InputText {...prop} multiline={options.multiline} />;
|
||||||
/>
|
} else if ("boolean" in option.data.selector) {
|
||||||
);
|
return <InputBoolean {...prop} />;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return (
|
return <InputText {...prop} />;
|
||||||
<InputText
|
|
||||||
className="service-editor--field"
|
|
||||||
label={label}
|
|
||||||
title={description}
|
|
||||||
helperText={description}
|
|
||||||
value={value ?? option.data.default}
|
|
||||||
onChange={onChange}
|
|
||||||
placeholder={example}
|
|
||||||
required={isRequired}
|
|
||||||
endAdornment={removeIcon}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
@@ -16,41 +16,52 @@ const Template: ComponentStory<typeof ServiceEditor> = (props) => {
|
|||||||
const [state, setState] = useState(props.data);
|
const [state, setState] = useState(props.data);
|
||||||
return (
|
return (
|
||||||
<Page>
|
<Page>
|
||||||
<ServiceEditor
|
|
||||||
{...props}
|
|
||||||
data={state}
|
|
||||||
onUpdate={(d) => {
|
|
||||||
setState(d);
|
|
||||||
props.onUpdate(d);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
maxHeight: "25vh",
|
maxHeight: "99vh",
|
||||||
overflow: "auto",
|
overflow: "auto",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<InputYaml label="" value={state} onChange={() => {}} />
|
<ServiceEditor
|
||||||
</div>
|
{...props}
|
||||||
<div
|
data={state}
|
||||||
style={{
|
onUpdate={(d) => {
|
||||||
maxHeight: "25vh",
|
setState(d);
|
||||||
overflow: "auto",
|
props.onUpdate(d);
|
||||||
}}
|
}}
|
||||||
>
|
|
||||||
<InputYaml label="" value={props.service.fields} onChange={() => {}} />
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
maxHeight: "25vh",
|
|
||||||
overflow: "auto",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<InputYaml
|
|
||||||
label=""
|
|
||||||
value={props.service.target ?? {}}
|
|
||||||
onChange={() => {}}
|
|
||||||
/>
|
/>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
maxHeight: "25vh",
|
||||||
|
overflow: "auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<InputYaml label="" value={state} onChange={() => {}} />
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
maxHeight: "25vh",
|
||||||
|
overflow: "auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<InputYaml
|
||||||
|
label=""
|
||||||
|
value={props.service.fields}
|
||||||
|
onChange={() => {}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
maxHeight: "25vh",
|
||||||
|
overflow: "auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<InputYaml
|
||||||
|
label=""
|
||||||
|
value={props.service.target ?? {}}
|
||||||
|
onChange={() => {}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Page>
|
</Page>
|
||||||
);
|
);
|
||||||
@@ -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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -1,75 +1,69 @@
|
|||||||
export interface TypedHassService {
|
export interface TypedHassService {
|
||||||
name?: string;
|
name?: string;
|
||||||
description: string;
|
description: string;
|
||||||
fields: Fields;
|
fields: Fields;
|
||||||
target?: Targets | null | undefined;
|
target?: Targets | null | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Fields = Record<string, FieldData>
|
export type Fields = Record<string, FieldData>;
|
||||||
export interface Targets {
|
export interface Targets {
|
||||||
device?: TargetData;
|
device?: TargetData;
|
||||||
entity?: TargetData;
|
entity?: TargetData;
|
||||||
}
|
}
|
||||||
export interface TargetData {
|
export interface TargetData {
|
||||||
domain?: string;
|
domain?: string;
|
||||||
integration?: string;
|
integration?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FieldData {
|
export interface FieldData {
|
||||||
name?: string | undefined;
|
name?: string | undefined;
|
||||||
description: string;
|
description: string;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
example?: any;
|
example?: any;
|
||||||
selector?: FieldSelector;
|
selector?: FieldSelector;
|
||||||
advanced?: boolean;
|
advanced?: boolean;
|
||||||
default?: any;
|
default?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FieldSelector {
|
export interface FieldSelector {
|
||||||
text?: SelectorText | null;
|
text?: SelectorText | null;
|
||||||
select?: SelectorSelect;
|
select?: SelectorSelect;
|
||||||
number?: SelectorNumber;
|
number?: SelectorNumber;
|
||||||
boolean?: null;
|
boolean?: null;
|
||||||
object?: null;
|
object?: null;
|
||||||
theme?: null;
|
theme?: null;
|
||||||
addon?: null;
|
addon?: null;
|
||||||
entity?: TargetData;
|
entity?: TargetData;
|
||||||
icon?: null;
|
icon?: null;
|
||||||
color_rgb?: null;
|
color_rgb?: null;
|
||||||
color_temp?: SelectorColorTemp | null;
|
color_temp?: SelectorColorTemp | null;
|
||||||
time?: null;
|
time?: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SelectorColorTemp {
|
export interface SelectorColorTemp {
|
||||||
min_mireds: number;
|
min_mireds: number;
|
||||||
max_mireds: number;
|
max_mireds: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SelectorNumber {
|
export interface SelectorNumber {
|
||||||
min: number;
|
min: number;
|
||||||
max: number;
|
max: number;
|
||||||
unit_of_measurement?: string;
|
unit_of_measurement?: string;
|
||||||
mode?: Mode;
|
mode?: "box";
|
||||||
step?: number;
|
step?: number;
|
||||||
}
|
|
||||||
|
|
||||||
enum Mode {
|
|
||||||
Box = "box",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SelectorSelect {
|
interface SelectorSelect {
|
||||||
options: OptionElement[];
|
options: OptionElement[];
|
||||||
}
|
}
|
||||||
|
|
||||||
type OptionElement = OptionClass | string;
|
type OptionElement = OptionClass | string;
|
||||||
|
|
||||||
interface OptionClass {
|
interface OptionClass {
|
||||||
label: string;
|
label: string;
|
||||||
value: string;
|
value: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SelectorText {
|
export interface SelectorText {
|
||||||
multiline: boolean;
|
multiline: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user