Skip to content

Commit

Permalink
update for ha-selector logic (oops)
Browse files Browse the repository at this point in the history
  • Loading branch information
snootched committed Dec 21, 2024
1 parent 8421b48 commit 2f5f1db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ha-editor-formbuilder-yaml",
"version": "2024.12.3-beta.1",
"version": "2024.12.3-beta.2",
"description": "A reimplementation of ha-editor-formbuilder - supporting YAML configuration of HA Selector controls, conditional expresssions, and more.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,13 @@ export default class EditorForm extends LitElement {
//console.debug("newValue: ", newValue);

// Determine if the control is a checkbox
const isCheckbox = target.tagName === "HA-CHECKBOX";
// const isCheckbox = target.tagName === "HA-CHECKBOX";

// Determine if the control is handling an array
const isArray = target.tagName === "HA-SELECTOR" && Array.isArray(ev.detail.value);

// Update the config using a helper function
this._updateConfig(configPath, newValue, isCheckbox);
this._updateConfig(configPath, newValue, isArray);

// Fire the "config-changed" event
fireEvent(this, "config-changed", { config: this._config }, { bubbles: true, composed: true });
Expand All @@ -178,7 +181,13 @@ export default class EditorForm extends LitElement {

// Helper function to extract the new value based on control type
private _getNewValue(target: HAInputElement, detail?: ValueChangedEvent['detail']): string | boolean | undefined | string[] | number | object {
if (target.tagName === "HA-SWITCH") {
if (target.tagName === "HA-SELECTOR") {
if (Array.isArray(detail?.value)) {
return detail?.value;
} else {
return detail?.value !== undefined ? detail.value : target.value;
}
} else if (target.tagName === "HA-SWITCH") {
return target.checked !== undefined ? target.checked : target.__checked; // Handle switch control
} else if (target.tagName === "HA-CHECKBOX") {
// Return the value of the checkbox, whether checked or unchecked
Expand All @@ -196,7 +205,7 @@ export default class EditorForm extends LitElement {
}
}

private _updateConfig(configPath: string[], newValue: any, isCheckbox: boolean = false) {
private _updateConfig(configPath: string[], newValue: any, isArray: boolean = false) {
if (!configPath.length) {
return;
}
Expand All @@ -213,7 +222,7 @@ export default class EditorForm extends LitElement {

const lastKey = configPath[configPath.length - 1];

if (isCheckbox) {
if (isArray) {
// Handle checkbox case: update array of values
const existingValues = nestedConfig[lastKey] || [];
const updatedValues = existingValues.slice(); // Create a copy to avoid mutation
Expand Down

0 comments on commit 2f5f1db

Please sign in to comment.