Fix Duration Selector Default (#12098)

* Fix Duration Default

* USe initial form data function
This commit is contained in:
Zack Barett 2022-03-22 18:33:16 -05:00 committed by GitHub
parent 840858b18c
commit 2d9b50defc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import { HaFormSchema } from "./types"; import type { Selector } from "../../data/selector";
import type { HaFormSchema } from "./types";
export const computeInitialHaFormData = ( export const computeInitialHaFormData = (
schema: HaFormSchema[] schema: HaFormSchema[]
@ -31,6 +32,25 @@ export const computeInitialHaFormData = (
minutes: 0, minutes: 0,
seconds: 0, seconds: 0,
}; };
} else if ("selector" in field) {
const selector: Selector = field.selector;
if ("boolean" in selector) {
data[field.name] = false;
} else if ("text" in selector) {
data[field.name] = "";
} else if ("number" in selector) {
data[field.name] = "min" in selector.number ? selector.number.min : 0;
} else if ("select" in selector) {
if (selector.select.options.length) {
data[field.name] = selector.select.options[0][0];
}
} else if ("duration" in selector) {
data[field.name] = {
hours: 0,
minutes: 0,
seconds: 0,
};
}
} }
}); });
return data; return data;

View File

@ -1,8 +1,8 @@
import "../ha-duration-input";
import { html, LitElement } from "lit"; import { html, LitElement } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { DurationSelector } from "../../data/selector"; import type { DurationSelector } from "../../data/selector";
import { HomeAssistant } from "../../types"; import type { HomeAssistant } from "../../types";
import "../ha-duration-input";
@customElement("ha-selector-duration") @customElement("ha-selector-duration")
export class HaTimeDuration extends LitElement { export class HaTimeDuration extends LitElement {