Skip to content

Commit

Permalink
Support for disabled selectors in config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts committed Oct 29, 2024
1 parent 48dfa11 commit b833040
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/ha-form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface HaFormBaseSchema {
suffix?: string;
// This value will be set initially when form is loaded
suggested_value?: HaFormData;
// Disable flag is passed here from backend for config flow
disabled?: boolean;
};
context?: Record<string, string>;
}
Expand Down
15 changes: 13 additions & 2 deletions src/dialogs/config-flow/step-flow-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TemplateResult,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { dynamicElement } from "../../common/dom/dynamic-element-directive";
import { fireEvent } from "../../common/dom/fire_event";
import { isNavigationClick } from "../../common/dom/is-navigation-click";
Expand Down Expand Up @@ -46,6 +47,13 @@ class StepFlowForm extends LitElement {
this.removeEventListener("keydown", this._handleKeyDown);
}

private handleDisabledFields = memoizeOne((schema) =>
schema?.map((field) => ({
...field,
...(field?.description?.disabled ? { disabled: true } : {}),
}))
);

protected render(): TemplateResult {
const step = this.step;
const stepData = this._stepDataProcessed;
Expand All @@ -62,7 +70,9 @@ class StepFlowForm extends LitElement {
.data=${stepData}
.disabled=${this._loading}
@value-changed=${this._stepDataChanged}
.schema=${autocompleteLoginFields(step.data_schema)}
.schema=${autocompleteLoginFields(
this.handleDisabledFields(step.data_schema)
)}
.error=${step.errors}
.computeLabel=${this._labelCallback}
.computeHelper=${this._helperCallback}
Expand Down Expand Up @@ -180,8 +190,9 @@ class StepFlowForm extends LitElement {
Object.keys(stepData).forEach((key) => {
const value = stepData[key];
const isEmpty = [undefined, ""].includes(value);
const field = this.step.data_schema?.find((f) => f.name === key);

if (!isEmpty) {
if (!isEmpty && !field?.description?.disabled) {
toSendData[key] = value;
}
});
Expand Down

0 comments on commit b833040

Please sign in to comment.