Skip to content

Commit

Permalink
$mol_string: show errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Nov 4, 2023
1 parent 39f06da commit 6f80226
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions string/demo/demo.view.tree
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ $mol_string_demo $mol_example_small
<= Hint $mol_string
hint \Batman
value? <=> name? \
<= Broken $mol_string
hint \Broken
value? <=> broken? \
<= Filled $mol_string
value? <=> name2? \Jocker
<= Disabled $mol_string
Expand Down
1 change: 1 addition & 0 deletions string/string.view.tree
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $mol_string $mol_view
0
auto /
<= selection_watcher null
<= error_report null
field *
^
disabled <= disabled false
Expand Down
25 changes: 24 additions & 1 deletion string/string.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,40 @@ namespace $.$$ {
*/
export class $mol_string extends $.$mol_string {

@ $mol_action
event_change( next? : Event ) {
if( !next ) return
const el = next.target as HTMLInputElement
const from = el.selectionStart
const to = el.selectionEnd
el.value = this.value_changed( el.value )
try {
el.value = this.value_changed( el.value )
} catch( error ) {
const el = this.dom_node() as HTMLInputElement
if( error instanceof Error ) {
el.setCustomValidity( error.message )
el.reportValidity()
}
$mol_fail_hidden( error )
}
if( to === null ) return
el.selectionEnd = to
el.selectionStart = from
this.selection_change( next )
}

@ $mol_mem
error_report() {
try {
if( this.focused() ) this.value()
} catch( error ) {
const el = this.dom_node() as HTMLInputElement
if( error instanceof Error ) {
el.setCustomValidity( error.message )
el.reportValidity()
}
}
}

hint_visible() {
return ( this.enabled() ? this.hint() : '' ) || ' '
Expand Down

0 comments on commit 6f80226

Please sign in to comment.