import "./ServiceEditorField.css"; import { FC, useRef } from "react"; import { ServiceEditorOption } from "./options"; import InputText from "components/Inputs/Base/InputText"; import InputNumber from "components/Inputs/Base/InputNumber"; import { RemoveCircle } from "@mui/icons-material"; import { InputAutoComplete, Option, } from "components/Inputs/AutoComplete/InputAutoComplete"; import { prettyName } from "utils/formatting"; import InputBoolean from "components/Inputs/Base/InputBoolean"; import { SelectorNumber, SelectorText } from "services/ha/fieldTypes"; import InputYaml from "components/Inputs/Base/InputYaml"; import { useSnackbar } from "notistack"; import { InputEntity } from "components/Inputs/AutoComplete/InputEntities"; import { ButtonIcon } from "components/Icons/ButtonIcons"; export type ServiceEditorFieldProps = { option: ServiceEditorOption; value: any; onChange: (v: any) => void; onRemove?: () => void; }; export const ServiceEditorField: FC = ({ option, value, onChange, onRemove, }) => { const snackr = useSnackbar(); const msgRef = useRef>({}); const description = option.data.description ?? ""; const isRequired = !onRemove; const label = option.label; let example = ""; const removeIcon = !onRemove ? ( <> ) : ( } /> ); example = option.data.example ? `Example: ${option.data.example}` : ""; 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.value, label: opt.label, }; })} 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 ; } else if ("object" in option.data.selector) { return ; } else if ("entity" in option.data.selector) { const options = option.data.selector.entity ?? {}; return ( ); } } if (option.data.selector) { if (!msgRef.current[JSON.stringify(option.data.selector)]) { msgRef.current[JSON.stringify(option.data.selector)] = true; snackr.enqueueSnackbar( "{JSON.stringify(Object.keys(option.data.selector))}" is of a unimplemented!
Please report this to the maintainers.{" "} For now, we leave you with a generic textbox!
DEBUG="{JSON.stringify(option.data.selector)}"
, { variant: "warning", persist: true, } ); } } return ; };