@@ -70,13 +70,15 const collectDescendantFormWidgets = (ch | |||||
70 | */ |
|
70 | */ | |
71 | const _obj = (v: unknown) => (typeof v === "object" && v !== null ? v : {}) as Record<string, unknown>; |
|
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 | /** Combines the values |
|
75 | /** Combines the values | |
74 | * |
|
76 | * | |
75 | * @param prev The previous value, `undefined` if none |
|
77 | * @param prev The previous value, `undefined` if none | |
76 | * @param value The new value to store |
|
78 | * @param value The new value to store | |
77 | * @param hint The hint how to combine values |
|
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 | // write the value as an array and append new values to it |
|
82 | // write the value as an array and append new values to it | |
81 | hint === "array" ? prev === undefined ? [value] : ([] as unknown[]).concat(prev, value) : |
|
83 | hint === "array" ? prev === undefined ? [value] : ([] as unknown[]).concat(prev, value) : | |
82 | // write the value as is and convert it the array when new values are appended |
|
84 | // write the value as is and convert it the array when new values are appended | |
@@ -94,7 +96,7 const _merge = ( | |||||
94 | [prop, ...rest]: string[], |
|
96 | [prop, ...rest]: string[], | |
95 | obj: Record<string, unknown>, |
|
97 | obj: Record<string, unknown>, | |
96 | value: unknown, |
|
98 | value: unknown, | |
97 |
hint?: |
|
99 | hint?: MergeHint | |
98 | ): unknown => ({ |
|
100 | ): unknown => ({ | |
99 | ...obj, |
|
101 | ...obj, | |
100 | [prop]: rest.length > 0 ? |
|
102 | [prop]: rest.length > 0 ? | |
@@ -111,7 +113,7 const _merge = ( | |||||
111 | * @param hint The hint how to store the value. The valid values are: |
|
113 | * @param hint The hint how to store the value. The valid values are: | |
112 | * `array`, `append`, `unset`. |
|
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 | _merge(name.split("."), _obj(obj), value, hint) as object; |
|
117 | _merge(name.split("."), _obj(obj), value, hint) as object; | |
116 |
|
118 | |||
117 | @djclass |
|
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 | _getValueAttr() { |
|
231 | _getValueAttr() { | |
@@ -238,15 +243,17 abstract class _FormMixin extends djbase | |||||
238 | return { name, value }; |
|
243 | return { name, value }; | |
239 | } else { |
|
244 | } else { | |
240 | // give radio widgets a default of null |
|
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 | } else { |
|
248 | } else { | |
244 | // checkbox/toggle button |
|
249 | // checkbox/toggle button | |
245 |
|
|
250 | return value !== false ? | |
246 |
|
|
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 | } else { |
|
255 | } else { | |
249 | return { name, value, hint: "append" }; |
|
256 | return { name, value, hint: "append" as const}; | |
250 | } |
|
257 | } | |
251 | } |
|
258 | } | |
252 | return {}; |
|
259 | return {}; | |
@@ -294,7 +301,7 abstract class _FormMixin extends djbase | |||||
294 | } |
|
301 | } | |
295 | this._onChangeDelayTimer = this.defer(() => { |
|
302 | this._onChangeDelayTimer = this.defer(() => { | |
296 | this._onChangeDelayTimer = { remove: () => { } }; |
|
303 | this._onChangeDelayTimer = { remove: () => { } }; | |
297 |
this._set("value", this.get( |
|
304 | this._set("value", this._getValueAttr()); | |
298 | }, 10); |
|
305 | }, 10); | |
299 | } |
|
306 | } | |
300 | } |
|
307 | } |
General Comments 0
You need to be logged in to leave comments.
Login now