From 0389d69e1a4515f15f9d60081a932e85ce4c0b31 Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Fri, 29 Sep 2023 12:08:28 +0300 Subject: [PATCH] $mol_form_draft: record of bool support, batching friendly --- check/list/list.view.tree | 1 + check/list/list.view.ts | 14 ++++++++++++++ form/draft/demo/demo.view.tree | 10 ++++++++++ form/draft/demo/demo.view.ts | 1 + form/draft/draft.view.ts | 35 ++++++++++++++++++++++++---------- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/check/list/list.view.tree b/check/list/list.view.tree index a79c5591e57..14dc9ec2c60 100644 --- a/check/list/list.view.tree +++ b/check/list/list.view.tree @@ -1,4 +1,5 @@ $mol_check_list $mol_view + options_checked * Option* $mol_check checked? <=> option_checked*? false label <= option_label* / diff --git a/check/list/list.view.ts b/check/list/list.view.ts index d338b08fce1..40d9b854a62 100644 --- a/check/list/list.view.ts +++ b/check/list/list.view.ts @@ -10,6 +10,20 @@ namespace $.$$ { return {} } + override options_checked(next?: Record) { + return next ?? {} + } + + override option_checked(id: string, next?: boolean | null) { + const prev = this.options_checked() + if (next === undefined) return prev[id] + + const next_rec = { ... prev, [id]: next } as Record + if (next === null) delete next_rec[id] + + return this.options_checked(next_rec)[id] + } + @ $mol_mem keys(): readonly string[] { return Object.keys( this.options() ) diff --git a/form/draft/demo/demo.view.tree b/form/draft/demo/demo.view.tree index b50701f871a..9015e7a88ac 100644 --- a/form/draft/demo/demo.view.tree +++ b/form/draft/demo/demo.view.tree @@ -4,6 +4,7 @@ $mol_form_draft_demo_article $mol_object2 adult? false content? \ friends? /string + hobbies? * $mol_form_draft_demo $mol_example title \Article draft form demo @@ -21,6 +22,7 @@ $mol_form_draft_demo $mol_example submit_allowed => publish_allowed value_str*? => value_str*? value_arr_str*? => value_arr_str*? + value_rec_bool*? => value_rec_bool*? changed => changed form_fields / <= Title_field $mol_form_field @@ -56,6 +58,14 @@ $mol_form_draft_demo $mol_example Content <= Content $mol_textarea hint \Long long story.. value? <=> value_str*content? + <= Hobbies_field $mol_form_field + name \Hobbies + Content <= Hobbies $mol_check_list + options_checked? <=> value_rec_bool*hobbies? + options * + programming \Programming + bikinkg \Biking + fishing \Fishing <= Friends_field $mol_form_field name \Friends Content <= Friends $mol_select_list diff --git a/form/draft/demo/demo.view.ts b/form/draft/demo/demo.view.ts index bc77cff0e46..230eede1c54 100644 --- a/form/draft/demo/demo.view.ts +++ b/form/draft/demo/demo.view.ts @@ -7,6 +7,7 @@ namespace $.$$ { return [ this.Title_field(), this.Config(), + this.Hobbies_field(), ... this.value_str( 'type' ) ? [ this.Content_field() ] : [], this.Friends_field(), ] diff --git a/form/draft/draft.view.ts b/form/draft/draft.view.ts index 97d9b49f2b6..2aacd89d06a 100644 --- a/form/draft/draft.view.ts +++ b/form/draft/draft.view.ts @@ -17,6 +17,16 @@ namespace $.$$ { return Boolean(val) ?? false } + function normalize_val(prev: Value, next: Value | null) { + switch( typeof prev ) { + case 'boolean': return String( next ) === 'true' + case 'number': return Number( next ) + case 'string': return String( next ) + } + + return next + } + /** * @see https://mol.hyoo.ru/#!section=demos/demo=mol_form_draft_demo */ @@ -27,7 +37,7 @@ namespace $.$$ { } @ $mol_mem_key - value_rec_str( field: string, next? : Record | null ) { + value_rec_bool( field: string, next? : Record | null ) { return this.value( field, next ) ?? {} } @@ -70,16 +80,21 @@ namespace $.$$ { submit( next? : Event ) { const model = this.model() as unknown as Model - - for( let [ field, next ] of Object.entries( this.state() ) ) { - const prev = model[ field ]() - switch( typeof prev ) { - case 'boolean': next = String( next ) === 'true'; break - case 'number': next = Number( next ); break - case 'string': next = String( next ); break + + const tasks = Object.entries( this.state() ).map( + ([ field, next ]) => () => { + const prev = model[ field ]() + + return { + field, + next: normalize_val(prev, next) + } } - model[ field ]( next ) - } + ) + + const normalized = $mol_wire_race(...tasks) + + $mol_wire_race(...normalized.map(({ field, next }) => () => model[ field ]( next ))) this.state( null )