Skip to content

Commit

Permalink
$mol_form_draft: record of bool support, batching friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Sep 29, 2023
1 parent a010359 commit 0389d69
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions check/list/list.view.tree
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$mol_check_list $mol_view
options_checked *
Option* $mol_check
checked? <=> option_checked*? false
label <= option_label* /
Expand Down
14 changes: 14 additions & 0 deletions check/list/list.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ namespace $.$$ {
return {}
}

override options_checked(next?: Record<string, boolean>) {
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<string, boolean>
if (next === null) delete next_rec[id]

return this.options_checked(next_rec)[id]
}

@ $mol_mem
keys(): readonly string[] {
return Object.keys( this.options() )
Expand Down
10 changes: 10 additions & 0 deletions form/draft/demo/demo.view.tree
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions form/draft/demo/demo.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace $.$$ {
return [
this.Title_field(),
this.Config(),
this.Hobbies_field(),
... this.value_str( 'type' ) ? [ this.Content_field() ] : [],
this.Friends_field(),
]
Expand Down
35 changes: 25 additions & 10 deletions form/draft/draft.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -27,7 +37,7 @@ namespace $.$$ {
}

@ $mol_mem_key
value_rec_str( field: string, next? : Record<string, boolean> | null ) {
value_rec_bool( field: string, next? : Record<string, boolean> | null ) {
return this.value( field, next ) ?? {}
}

Expand Down Expand Up @@ -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 )

Expand Down

0 comments on commit 0389d69

Please sign in to comment.