mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-26 06:17:20 +00:00
Use dynamic struct validation for entities card rows (#9962)
This commit is contained in:
parent
9ccb5360b3
commit
d6dbbcb0de
@ -1,6 +1,6 @@
|
|||||||
import { refine, string } from "superstruct";
|
import { refine, string } from "superstruct";
|
||||||
|
|
||||||
const isCustomType = (value: string) => value.startsWith("custom:");
|
export const isCustomType = (value: string) => value.startsWith("custom:");
|
||||||
|
|
||||||
export const customType = () =>
|
export const customType = () =>
|
||||||
refine(string(), "custom element type", isCustomType);
|
refine(string(), "custom element type", isCustomType);
|
||||||
|
@ -8,8 +8,9 @@ import {
|
|||||||
any,
|
any,
|
||||||
array,
|
array,
|
||||||
assert,
|
assert,
|
||||||
boolean,
|
|
||||||
assign,
|
assign,
|
||||||
|
boolean,
|
||||||
|
dynamic,
|
||||||
literal,
|
literal,
|
||||||
number,
|
number,
|
||||||
object,
|
object,
|
||||||
@ -19,7 +20,10 @@ import {
|
|||||||
union,
|
union,
|
||||||
} from "superstruct";
|
} from "superstruct";
|
||||||
import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event";
|
||||||
import { customType } from "../../../../common/structs/is-custom-type";
|
import {
|
||||||
|
customType,
|
||||||
|
isCustomType,
|
||||||
|
} from "../../../../common/structs/is-custom-type";
|
||||||
import { entityId } from "../../../../common/structs/is-entity-id";
|
import { entityId } from "../../../../common/structs/is-entity-id";
|
||||||
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
|
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
|
||||||
import "../../../../components/entity/state-badge";
|
import "../../../../components/entity/state-badge";
|
||||||
@ -38,6 +42,7 @@ import "../hui-entities-card-row-editor";
|
|||||||
import "../hui-sub-element-editor";
|
import "../hui-sub-element-editor";
|
||||||
import { processEditorEntities } from "../process-editor-entities";
|
import { processEditorEntities } from "../process-editor-entities";
|
||||||
import { actionConfigStruct } from "../structs/action-struct";
|
import { actionConfigStruct } from "../structs/action-struct";
|
||||||
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { entitiesConfigStruct } from "../structs/entities-struct";
|
import { entitiesConfigStruct } from "../structs/entities-struct";
|
||||||
import {
|
import {
|
||||||
EditorTarget,
|
EditorTarget,
|
||||||
@ -45,7 +50,6 @@ import {
|
|||||||
SubElementEditorConfig,
|
SubElementEditorConfig,
|
||||||
} from "../types";
|
} from "../types";
|
||||||
import { configElementStyle } from "./config-elements-style";
|
import { configElementStyle } from "./config-elements-style";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
|
||||||
|
|
||||||
const buttonEntitiesRowConfigStruct = object({
|
const buttonEntitiesRowConfigStruct = object({
|
||||||
type: literal("button"),
|
type: literal("button"),
|
||||||
@ -142,24 +146,53 @@ const textEntitiesRowConfigStruct = object({
|
|||||||
icon: optional(string()),
|
icon: optional(string()),
|
||||||
});
|
});
|
||||||
|
|
||||||
const customRowConfigStruct = type({
|
const customEntitiesRowConfigStruct = type({
|
||||||
type: customType(),
|
type: customType(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const entitiesRowConfigStruct = union([
|
const entitiesRowConfigStruct = dynamic<any>((value) => {
|
||||||
entitiesConfigStruct,
|
if (value && typeof value === "object" && "type" in value) {
|
||||||
buttonEntitiesRowConfigStruct,
|
if (isCustomType((value as LovelaceRowConfig).type!)) {
|
||||||
castEntitiesRowConfigStruct,
|
return customEntitiesRowConfigStruct;
|
||||||
conditionalEntitiesRowConfigStruct,
|
}
|
||||||
dividerEntitiesRowConfigStruct,
|
|
||||||
sectionEntitiesRowConfigStruct,
|
switch ((value as LovelaceRowConfig).type!) {
|
||||||
webLinkEntitiesRowConfigStruct,
|
case "attribute": {
|
||||||
buttonsEntitiesRowConfigStruct,
|
return attributeEntitiesRowConfigStruct;
|
||||||
attributeEntitiesRowConfigStruct,
|
}
|
||||||
callServiceEntitiesRowConfigStruct,
|
case "button": {
|
||||||
textEntitiesRowConfigStruct,
|
return buttonEntitiesRowConfigStruct;
|
||||||
customRowConfigStruct,
|
}
|
||||||
]);
|
case "buttons": {
|
||||||
|
return buttonsEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
case "call-service": {
|
||||||
|
return callServiceEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
case "cast": {
|
||||||
|
return castEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
case "conditional": {
|
||||||
|
return conditionalEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
case "divider": {
|
||||||
|
return dividerEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
case "section": {
|
||||||
|
return sectionEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
case "text": {
|
||||||
|
return textEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
case "weblink": {
|
||||||
|
return webLinkEntitiesRowConfigStruct;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No "type" property => has to be the default entity row config struct
|
||||||
|
return entitiesConfigStruct;
|
||||||
|
});
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user