@@ -102,14 +102,29 @@ function onHidePanelClick(el: HTMLElement, e: MouseEvent) {
102102 throw new Error ( 'no panel to hide' ) ; // should never happen, otherwise there is a bug in code
103103}
104104
105+ function assignElementProperty ( el : any , name : string , val : string ) {
106+ name = camelize ( name ) ;
107+ const old = el [ name ] ;
108+ if ( typeof old === 'boolean' ) {
109+ el [ name ] = val === 'true' ;
110+ } else if ( typeof old === 'number' ) {
111+ el [ name ] = parseFloat ( val ) ;
112+ } else if ( typeof old === 'string' ) {
113+ el [ name ] = val ;
114+ } else {
115+ // in the future, we could introduce a better typing system like `data-modal-form.action:string="..."`
116+ throw new Error ( `cannot assign element property ${ name } by value ${ val } ` ) ;
117+ }
118+ }
119+
105120function onShowModalClick ( el : HTMLElement , e : MouseEvent ) {
106121 // A ".show-modal" button will show a modal dialog defined by its "data-modal" attribute.
107122 // Each "data-modal-{target}" attribute will be filled to target element's value or text-content.
108123 // * First, try to query '#target'
109124 // * Then, try to query '[name=target]'
110125 // * Then, try to query '.target'
111126 // * Then, try to query 'target' as HTML tag
112- // If there is a ".{attr}" part like "data-modal-form.action", then the form's "action" attribute will be set.
127+ // If there is a ".{attr}" part like "data-modal-form.action", then the form's "action" property will be set.
113128 e . preventDefault ( ) ;
114129 const modalSelector = el . getAttribute ( 'data-modal' ) ;
115130 const elModal = document . querySelector ( modalSelector ) ;
@@ -122,7 +137,7 @@ function onShowModalClick(el: HTMLElement, e: MouseEvent) {
122137 }
123138
124139 const attrTargetCombo = attrib . name . substring ( modalAttrPrefix . length ) ;
125- const [ attrTargetName , attrTargetAttr ] = attrTargetCombo . split ( '.' ) ;
140+ const [ attrTargetName , attrTargetProp ] = attrTargetCombo . split ( '.' ) ;
126141 // try to find target by: "#target" -> "[name=target]" -> ".target" -> "<target> tag"
127142 const attrTarget = elModal . querySelector ( `#${ attrTargetName } ` ) ||
128143 elModal . querySelector ( `[name=${ attrTargetName } ]` ) ||
@@ -133,8 +148,8 @@ function onShowModalClick(el: HTMLElement, e: MouseEvent) {
133148 continue ;
134149 }
135150
136- if ( attrTargetAttr ) {
137- ( attrTarget as any ) [ camelize ( attrTargetAttr ) ] = attrib . value ;
151+ if ( attrTargetProp ) {
152+ assignElementProperty ( attrTarget , attrTargetProp , attrib . value ) ;
138153 } else if ( attrTarget . matches ( 'input, textarea' ) ) {
139154 ( attrTarget as HTMLInputElement | HTMLTextAreaElement ) . value = attrib . value ; // FIXME: add more supports like checkbox
140155 } else {
0 commit comments