Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$mol_form_draft: array support #637

Merged
merged 21 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions form/draft/demo/demo.view.tree
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ $mol_form_draft_demo_article $mol_object2
type? \
adult? false
content? \
friends? /string

$mol_form_draft_demo $mol_example
title \Article draft form demo
Expand All @@ -19,6 +20,7 @@ $mol_form_draft_demo $mol_example
submit? => publish?
submit_allowed => publish_allowed
value_str*? => value_str*?
value_arr_str*? => value_arr_str*?
changed => changed
form_fields /
<= Title_field $mol_form_field
Expand Down Expand Up @@ -54,12 +56,26 @@ $mol_form_draft_demo $mol_example
Content <= Content $mol_textarea
hint \Long long story..
value? <=> value_str*content?
<= Friends_field $mol_form_field
name \Friends
Content <= Friends $mol_select_list
dictionary <= friends_suggestions *
jocker \Jocker
harley \Harley Quinn
penguin \Penguin
riddler \Riddler
bane \Bane
freeze \Mister Freeze
clay \Clayface
mask \Black Mask
value? <=> value_arr_str*friends?
body <= form_body /
<= Title_field
<= Config $mol_form_group sub /
<= Adult_field
<= Type_field
<= Content_field
<= Friends_field
buttons /
<= Publish $mol_button_major
title \Publish
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 @@ -8,6 +8,7 @@ namespace $.$$ {
this.Title_field(),
this.Config(),
... this.value_str( 'type' ) ? [ this.Content_field() ] : [],
this.Friends_field(),
]
}

Expand Down
50 changes: 38 additions & 12 deletions form/draft/draft.view.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
namespace $.$$ {

type Primitive = string | number | boolean

type Value = readonly Primitive[] | Primitive | Record<string, boolean>
type Model = Record<string, (next?: Value | null) => Value>

function norm_string(val: unknown) {
return String(val) ?? ''
}

function norm_number(val: unknown) {
return Number(val) ?? 0
}

function norm_bool(val: unknown) {
return Boolean(val) ?? false
}

/**
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_form_draft_demo
*/
export class $mol_form_draft extends $.$mol_form_draft {

@ $mol_mem_key
value_arr_str( field: string, next? : readonly string[] | null ) {
zerkalica marked this conversation as resolved.
Show resolved Hide resolved
return this.value( field, next )?.map(norm_string) ?? []
}

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

@ $mol_mem_key
value_str( field: string, next? : string | null ) {
return String( this.value( field, next ) ?? '' )
return norm_string( this.value( field, next ) )
}

@ $mol_mem_key
value_numb( field: string, next? : boolean | null ) {
return Number( this.value( field, next ) ?? 0 )
return norm_number( this.value( field, next ) )
}

@ $mol_mem_key
value_bool( field: string, next? : boolean | null ) {
return Boolean( this.value( field, next ) ?? false )
return norm_bool( this.value( field, next ) )
}

@ $mol_mem_key
value( field: string, next?: string | number | boolean | null ) {
return this.state( next?.valueOf && { ... this.state(), [ field ]: next } )[ field ]
?? ( this.model() as any )[ field ]()
value<T extends Value>( field: string, next?: T | null ) {
return this.state( next?.valueOf && { ... this.state(), [ field ]: next } )[ field ] as T | undefined
?? ( this.model() as unknown as Model )[ field ]() as T
}

@ $mol_mem
state( next?: Record< string, string | number | boolean | null > | null ) {
state( next?: Record< string, Value | null > | null ) {
return $mol_state_local.value( `${ this }.state()`, next ) ?? {}
}

Expand All @@ -39,20 +65,20 @@ namespace $.$$ {
submit_allowed() {
return this.changed() && super.submit_allowed()
}

@ $mol_action
submit( next? : Event ) {

const model = this.model()
const model = this.model() as unknown as Model

for( let [ field, next ] of Object.entries( this.state() ) ) {
const prev = ( model as any )[ field ]()
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
}
;( model as any )[ field ]( next )
model[ field ]( next )
}

this.state( null )
Expand Down
2 changes: 1 addition & 1 deletion select/list/list.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace $.$$ {
*/
export class $mol_select_list extends $.$mol_select_list {

override value( val? : string[] ) {
override value( val? : readonly string[] ) {
return super.value( val ) as readonly string[]
}

Expand Down