@@ -109,18 +109,26 @@ function onHidePanelClick(el: HTMLElement, e: MouseEvent) {
109
109
throw new Error ( 'no panel to hide' ) ; // should never happen, otherwise there is a bug in code
110
110
}
111
111
112
- export function assignElementProperty ( el : any , name : string , val : string ) {
113
- name = camelize ( name ) ;
114
- const old = el [ name ] ;
112
+ export type ElementWithAssignableProperties = {
113
+ getAttribute : ( name : string ) => string | null ;
114
+ setAttribute : ( name : string , value : string ) => void ;
115
+ } & Record < string , any >
116
+
117
+ export function assignElementProperty ( el : ElementWithAssignableProperties , kebabName : string , val : string ) {
118
+ const camelizedName = camelize ( kebabName ) ;
119
+ const old = el [ camelizedName ] ;
115
120
if ( typeof old === 'boolean' ) {
116
- el [ name ] = val === 'true' ;
121
+ el [ camelizedName ] = val === 'true' ;
117
122
} else if ( typeof old === 'number' ) {
118
- el [ name ] = parseFloat ( val ) ;
123
+ el [ camelizedName ] = parseFloat ( val ) ;
119
124
} else if ( typeof old === 'string' ) {
120
- el [ name ] = val ;
125
+ el [ camelizedName ] = val ;
126
+ } else if ( old ?. nodeName ) {
127
+ // "form" has an edge case: its "<input name=action>" element overwrites the "action" property, we can only set attribute
128
+ el . setAttribute ( kebabName , val ) ;
121
129
} else {
122
130
// in the future, we could introduce a better typing system like `data-modal-form.action:string="..."`
123
- throw new Error ( `cannot assign element property ${ name } by value ${ val } ` ) ;
131
+ throw new Error ( `cannot assign element property " ${ camelizedName } " by value " ${ val } " ` ) ;
124
132
}
125
133
}
126
134
0 commit comments