@@ -70,13 +70,15 const collectDescendantFormWidgets = (ch | |||
|
70 | 70 | */ |
|
71 | 71 | const _obj = (v: unknown) => (typeof v === "object" && v !== null ? v : {}) as Record<string, unknown>; |
|
72 | 72 | |
|
73 | type MergeHint = "array" | "append" | "unset"; | |
|
74 | ||
|
73 | 75 | /** Combines the values |
|
74 | 76 | * |
|
75 | 77 | * @param prev The previous value, `undefined` if none |
|
76 | 78 | * @param value The new value to store |
|
77 | 79 | * @param hint The hint how to combine values |
|
78 | 80 | */ |
|
79 |
const _combine = (prev: unknown, value: unknown, hint?: |
|
|
81 | const _combine = (prev: unknown, value: unknown, hint?: MergeHint) => | |
|
80 | 82 | // write the value as an array and append new values to it |
|
81 | 83 | hint === "array" ? prev === undefined ? [value] : ([] as unknown[]).concat(prev, value) : |
|
82 | 84 | // write the value as is and convert it the array when new values are appended |
@@ -94,7 +96,7 const _merge = ( | |||
|
94 | 96 | [prop, ...rest]: string[], |
|
95 | 97 | obj: Record<string, unknown>, |
|
96 | 98 | value: unknown, |
|
97 |
hint?: |
|
|
99 | hint?: MergeHint | |
|
98 | 100 | ): unknown => ({ |
|
99 | 101 | ...obj, |
|
100 | 102 | [prop]: rest.length > 0 ? |
@@ -111,7 +113,7 const _merge = ( | |||
|
111 | 113 | * @param hint The hint how to store the value. The valid values are: |
|
112 | 114 | * `array`, `append`, `unset`. |
|
113 | 115 | */ |
|
114 |
const _assign = (name: string, obj: object, value: unknown, hint?: |
|
|
116 | const _assign = (name: string, obj: object, value: unknown, hint?: MergeHint) => | |
|
115 | 117 | _merge(name.split("."), _obj(obj), value, hint) as object; |
|
116 | 118 | |
|
117 | 119 | @djclass |
@@ -221,6 +223,9 abstract class _FormMixin extends djbase | |||
|
221 | 223 | } |
|
222 | 224 | } |
|
223 | 225 | }); |
|
226 | ||
|
227 | // Note: no need to call this._set("value", ...) as the child updates will trigger onChange events | |
|
228 | // which I am monitoring. | |
|
224 | 229 | } |
|
225 | 230 | |
|
226 | 231 | _getValueAttr() { |
@@ -238,15 +243,17 abstract class _FormMixin extends djbase | |||
|
238 | 243 | return { name, value }; |
|
239 | 244 | } else { |
|
240 | 245 | // give radio widgets a default of null |
|
241 | return { name, value: null, hint: "unset" }; | |
|
246 | return { name, value: null, hint: "unset" as const}; | |
|
242 | 247 | } |
|
243 | 248 | } else { |
|
244 | 249 | // checkbox/toggle button |
|
245 |
|
|
|
246 |
|
|
|
250 | return value !== false ? | |
|
251 | { name, value, hint: "array" as const} : | |
|
252 | // empty array when no checkboxes are selected | |
|
253 | { name, value: [], hint: "unset" as const }; | |
|
247 | 254 | } |
|
248 | 255 | } else { |
|
249 | return { name, value, hint: "append" }; | |
|
256 | return { name, value, hint: "append" as const}; | |
|
250 | 257 | } |
|
251 | 258 | } |
|
252 | 259 | return {}; |
@@ -294,7 +301,7 abstract class _FormMixin extends djbase | |||
|
294 | 301 | } |
|
295 | 302 | this._onChangeDelayTimer = this.defer(() => { |
|
296 | 303 | this._onChangeDelayTimer = { remove: () => { } }; |
|
297 |
this._set("value", this.get( |
|
|
304 | this._set("value", this._getValueAttr()); | |
|
298 | 305 | }, 10); |
|
299 | 306 | } |
|
300 | 307 | } |
General Comments 0
You need to be logged in to leave comments.
Login now