Z-Wave JS: Fix validation/parsing for custom config param UI (#22789)

* Z-Wave JS: Fix validation/parsing for custom config param UI

* Apply suggestions from code review

Co-authored-by: Bram Kragten <mail@bramkragten.nl>

---------

Co-authored-by: Petar Petrov <MindFreeze@users.noreply.github.com>
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
AlCalzone 2024-11-12 15:57:32 +01:00 committed by GitHub
parent 4d107f978c
commit 7c851d4542
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -117,21 +117,31 @@ class ZWaveJSCustomParam extends LitElement {
`; `;
} }
private _tryParseNumber(value: string): number | undefined {
if (!value) return undefined;
const parsed = Number(value);
if (Number.isNaN(parsed)) return undefined;
return parsed;
}
private _customParamNumberChanged(ev: Event) { private _customParamNumberChanged(ev: Event) {
this._customParamNumber = this._customParamNumber = this._tryParseNumber(
Number((ev.target as HTMLInputElement).value) || undefined; (ev.target as HTMLInputElement).value
);
} }
private _customValueSizeChanged(ev: Event) { private _customValueSizeChanged(ev: Event) {
this._valueSize = Number((ev.target as HTMLSelectElement).value) || 1; this._valueSize =
this._tryParseNumber((ev.target as HTMLSelectElement).value) ?? 1;
} }
private _customValueChanged(ev: Event) { private _customValueChanged(ev: Event) {
this._value = Number((ev.target as HTMLInputElement).value) || undefined; this._value = this._tryParseNumber((ev.target as HTMLInputElement).value);
} }
private _customValueFormatChanged(ev: Event) { private _customValueFormatChanged(ev: Event) {
this._valueFormat = Number((ev.target as HTMLSelectElement).value) || 0; this._valueFormat =
this._tryParseNumber((ev.target as HTMLSelectElement).value) ?? 0;
} }
private async _getCustomConfigValue() { private async _getCustomConfigValue() {