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