diff --git a/djx/src/main/ts/form/_FormMixin.ts b/djx/src/main/ts/form/_FormMixin.ts --- a/djx/src/main/ts/form/_FormMixin.ts +++ b/djx/src/main/ts/form/_FormMixin.ts @@ -70,13 +70,15 @@ const collectDescendantFormWidgets = (ch */ const _obj = (v: unknown) => (typeof v === "object" && v !== null ? v : {}) as Record; +type MergeHint = "array" | "append" | "unset"; + /** Combines the values * * @param prev The previous value, `undefined` if none * @param value The new value to store * @param hint The hint how to combine values */ -const _combine = (prev: unknown, value: unknown, hint?: string) => +const _combine = (prev: unknown, value: unknown, hint?: MergeHint) => // write the value as an array and append new values to it hint === "array" ? prev === undefined ? [value] : ([] as unknown[]).concat(prev, value) : // write the value as is and convert it the array when new values are appended @@ -94,7 +96,7 @@ const _merge = ( [prop, ...rest]: string[], obj: Record, value: unknown, - hint?: string + hint?: MergeHint ): unknown => ({ ...obj, [prop]: rest.length > 0 ? @@ -111,7 +113,7 @@ const _merge = ( * @param hint The hint how to store the value. The valid values are: * `array`, `append`, `unset`. */ -const _assign = (name: string, obj: object, value: unknown, hint?: string) => +const _assign = (name: string, obj: object, value: unknown, hint?: MergeHint) => _merge(name.split("."), _obj(obj), value, hint) as object; @djclass @@ -221,6 +223,9 @@ abstract class _FormMixin extends djbase } } }); + + // Note: no need to call this._set("value", ...) as the child updates will trigger onChange events + // which I am monitoring. } _getValueAttr() { @@ -238,15 +243,17 @@ abstract class _FormMixin extends djbase return { name, value }; } else { // give radio widgets a default of null - return { name, value: null, hint: "unset" }; + return { name, value: null, hint: "unset" as const}; } } else { // checkbox/toggle button - if (value !== false) - return { name, value, hint: "array" }; + return value !== false ? + { name, value, hint: "array" as const} : + // empty array when no checkboxes are selected + { name, value: [], hint: "unset" as const }; } } else { - return { name, value, hint: "append" }; + return { name, value, hint: "append" as const}; } } return {}; @@ -294,7 +301,7 @@ abstract class _FormMixin extends djbase } this._onChangeDelayTimer = this.defer(() => { this._onChangeDelayTimer = { remove: () => { } }; - this._set("value", this.get("value")); + this._set("value", this._getValueAttr()); }, 10); } }