input area

This commit is contained in:
ari committed 2022-06-16 17:24:31 -04:00
1 parent 0fc3ea6146
commit 15c97b81b4
16 files changed
+699 -416

No files matched your search

@@ -5,10 +5,7 @@ import InputText from "components/Inputs/InputText";
import InputNumber from "components/Inputs/InputNumber";
import { IconButton } from "@mui/material";
import { RemoveCircle } from "@mui/icons-material";
import { InputEntity } from "components/Inputs/InputEntities";
import InputAutoText from "components/Inputs/InputAutoText";
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
import { Option } from "haService";
import { InputAutoComplete, Option } from "components/Inputs/InputAutoComplete";
import { prettyName } from "utils/formatting";
export type ServiceEditorFieldProps = {
@@ -45,85 +42,67 @@ export const ServiceEditorField: FC<ServiceEditorFieldProps> = ({
required={isRequired}
/>
);
if (option.type === "field") {
example = option.data.example ? `Example: ${option.data.example}` : "";
if (option.data.selector?.number) {
return (
<InputNumber
className="service-editor--field"
label={label}
value={value ?? option.data.default}
min={option.data.selector.number.min}
max={option.data.selector.number.max}
step={option.data.selector.number.step}
unit={option.data.selector.number.unit_of_measurement}
onChange={onChange}
required={isRequired}
placeholder={example}
/>
);
}
if (option.data.selector?.select) {
return (
<InputAutoComplete
className="service-editor--field"
label={label}
value={value}
onChange={onChange}
endAdornment={removeIcon}
required={isRequired}
options={option.data.selector.select.options.map<Option>((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
/>
);
}
example = option.data.example ? `Example: ${option.data.example}` : "";
if (option.data.selector?.number) {
return (
<InputText
<InputNumber
className="service-editor--field"
label={label}
title={description}
helperText={description}
value={value ?? option.data.default}
min={option.data.selector.number.min}
max={option.data.selector.number.max}
step={option.data.selector.number.step}
unit={option.data.selector.number.unit_of_measurement}
onChange={onChange}
placeholder={example}
required={isRequired}
endAdornment={removeIcon}
placeholder={example}
/>
);
} else {
if (option.id === "entity") {
if ("domain" in option.data) {
return (
<InputEntity
value={value}
onChange={onChange}
className="service-editor--field"
multiple
restrictToDomain={
option.data.domain ? [option.data.domain] : undefined
}
/>
);
}
}
}
if (option.data.selector?.select) {
return (
<InputAutoComplete
className="service-editor--field"
label={label}
value={value}
onChange={onChange}
endAdornment={removeIcon}
required={isRequired}
options={option.data.selector.select.options.map<Option>((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
/>
);
}
return (
<InputText
className="service-editor--field"
label={label}
title={description}
helperText={description}
value={value ?? option.data.default}
onChange={onChange}
placeholder={example}
required={isRequired}
endAdornment={removeIcon}
/>
);
return input;
};
@@ -0,0 +1,10 @@
.service-editor--targets {
display: flex;
flex-direction: row;
gap: 1em;
padding-bottom: 0.5em;
}
.service-editor--targets>div {
flex: 1;
}
@@ -0,0 +1,89 @@
import "./ServiceEditorTarget.css";
import { InputDevice } from "components/Inputs/InputDevice";
import { InputEntity } from "components/Inputs/InputEntities";
import { Targets } from "haService/fieldTypes";
import { FC } from "react";
import { InputArea } from "components/Inputs/InputArea";
type RawTargetData = {
entity_id?: string[] | string;
area_id?: string[] | string;
device_id?: string[] | string;
};
type CleanTargetData = {
entity_id: string[];
area_id: string[];
device_id: string[];
};
export type ServiceEditorTargetProps = {
defn: Targets;
value: RawTargetData | null | undefined;
onChange: (v: any) => void;
};
export const ServiceEditorTarget: FC<ServiceEditorTargetProps> = (props) => {
const value = cleanUpValue(props.value ?? {});
const domainRestrictions: string[] = [];
if (props.defn.entity) {
if (typeof props.defn.entity.domain === "string") {
domainRestrictions.push(props.defn.entity.domain);
}
}
return (
<div className="service-editor--targets">
<InputEntity
label="Target Entities"
value={value.entity_id}
onChange={(entity_id) =>
props.onChange({
...(props.value || {}),
entity_id,
})
}
restrictToDomain={domainRestrictions}
multiple
/>
<InputDevice
label="Target Devices"
value={value.device_id}
onChange={(device_id) =>
props.onChange({
...(props.value || {}),
device_id,
})
}
multiple
/>
<InputArea
label="Target Areas"
value={value.area_id}
onChange={(area_id) =>
props.onChange({
...(props.value || {}),
area_id,
})
}
multiple
/>
</div>
);
};
const cleanUpValue = (value: RawTargetData): CleanTargetData => {
const cleanOne = (v?: string[] | string): string[] => {
if (typeof v === "string") {
return [v];
}
if (!v || !Array.isArray(v)) {
return [];
}
return v;
};
return {
entity_id: cleanOne(value.entity_id),
device_id: cleanOne(value.device_id),
area_id: cleanOne(value.area_id),
};
};
+17 -30
View File
@@ -7,11 +7,11 @@ import {
ServiceEditorOption,
splitUpOptions,
convertFieldToOption,
convertTargetToOption,
ServiceEditorData,
} from "./options";
import { ServiceEditorField } from "./ServiceEditorField";
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
import { ServiceEditorTarget } from "./ServiceEditorTarget";
export type ServiceEditorProps = {
serviceId: string;
@@ -26,7 +26,6 @@ export const ServiceEditor: FC<ServiceEditorProps> = ({
onUpdate,
}) => {
const options = splitUpOptions(service, data);
console.log(service);
const [additionalFields, setAdditionalFields] = useState<{
field: string[];
optionalList: ServiceEditorOption[];
@@ -60,14 +59,14 @@ export const ServiceEditor: FC<ServiceEditorProps> = ({
<>
{options.required.map((opt) => (
<ServiceEditorField
key={opt.type + opt.id}
key={"field-" + opt.id}
option={opt}
value={data[opt.type][opt.id]}
value={data.field[opt.id]}
onChange={(v) =>
onUpdate({
...data,
[opt.type]: {
...data[opt.type],
field: {
...data.field,
[opt.id]: v,
},
})
@@ -75,28 +74,17 @@ export const ServiceEditor: FC<ServiceEditorProps> = ({
/>
))}
{hasTargets && (
<div className="service-editor--target">
{Object.keys(service.target ?? {}).map((key) => {
const targetData = (service.target as any)[key];
const option = convertTargetToOption(key, targetData);
return (
<ServiceEditorField
key={"fields-" + key}
option={option}
value={data.target[key] ?? ""}
onChange={(v) =>
onUpdate({
...data,
target: {
...data.target,
[key]: v,
},
})
}
/>
);
})}
</div>
<ServiceEditorTarget
key={"target-" + serviceId}
defn={service.target ?? {}}
value={data.target}
onChange={(target) =>
onUpdate({
...data,
target,
})
}
/>
)}
<div className="service-editor--used">
{additionalFields.field.map((key) => {
@@ -154,8 +142,7 @@ export const ServiceEditor: FC<ServiceEditorProps> = ({
...additionalFields,
field: additionalFields.field.concat([newField.id]),
optionalList: additionalFields.optionalList.filter(
(opt) =>
!(opt.type === newField.type && opt.id === newField.id)
(opt) => !(opt.id === newField.id)
),
});
}
+5 -25
View File
@@ -1,41 +1,21 @@
import { BaseOption } from "haService";
import { FieldData, TargetData, TypedHassService } from "haService/fieldTypes";
import { BaseOption } from "components/Inputs/InputAutoComplete";
import { FieldData, TypedHassService } from "haService/fieldTypes";
import { prettyName } from "utils/formatting";
export type ServiceEditorData = {
field: Record<string, any>;
target: Record<string, any>;
};
export type ServiceEditorOption = BaseOption<
| {
type: "field";
data: {
description: string;
} & FieldData;
}
| {
type: "target";
data: {
description: string;
} & TargetData;
}
>;
export type ServiceEditorOption = BaseOption<{
data: FieldData
}>;
export const convertFieldToOption = (id: string, data: FieldData): ServiceEditorOption => ({
id,
label: data.name ?? prettyName(id),
type: "field",
data,
})
export const convertTargetToOption = (id: string, data: TargetData): ServiceEditorOption => ({
id,
type: "target",
label: ((data ?? {}) as any).name ?? prettyName(id),
data: (data as any) ?? {
description: "",
},
})
export const splitUpOptions = (service: TypedHassService, data: ServiceEditorData) => {
const required: ServiceEditorOption[] = [];