@@ -13,6 +13,7 @@ const {
13
13
removeDecorator,
14
14
ensureImport,
15
15
isProperty,
16
+ renameEventHandler,
16
17
} = require ( '../utils/native' ) ;
17
18
18
19
const EVENT_HANDLER_METHODS = [
@@ -98,14 +99,6 @@ module.exports = function transformNativeComponent(root, options) {
98
99
throw new SilentError ( `Using \`this.elementId\` is not supported in tagless components` ) ;
99
100
}
100
101
101
- // skip components that use `click()` etc.
102
- for ( let methodName of EVENT_HANDLER_METHODS ) {
103
- let handlerMethod = classBody . filter ( path => isMethod ( path , methodName ) ) [ 0 ] ;
104
- if ( handlerMethod ) {
105
- throw new SilentError ( `Using \`${ methodName } ()\` is not supported in tagless components` ) ;
106
- }
107
- }
108
-
109
102
// analyze `elementId`, `attributeBindings`, `classNames` and `classNameBindings`
110
103
let elementId = findElementId ( classBody ) ;
111
104
debug ( 'elementId: %o' , elementId ) ;
@@ -119,6 +112,19 @@ module.exports = function transformNativeComponent(root, options) {
119
112
let classNameBindings = findClassNameBindings ( classDeclaration ) ;
120
113
debug ( 'classNameBindings: %o' , classNameBindings ) ;
121
114
115
+ let eventHandlers = new Map ( ) ;
116
+ // rename event handlers and add @action
117
+ for ( let eventName of EVENT_HANDLER_METHODS ) {
118
+ let handlerMethod = classBody . filter ( path => isMethod ( path , eventName ) ) [ 0 ] ;
119
+
120
+ if ( handlerMethod ) {
121
+ let methodName = renameEventHandler ( handlerMethod ) ;
122
+ addClassDecorator ( handlerMethod , 'action' ) ;
123
+ ensureImport ( root , 'action' , '@ember/object' ) ;
124
+ eventHandlers . set ( eventName , methodName ) ;
125
+ }
126
+ }
127
+
122
128
// set `@tagName('')`
123
129
addClassDecorator ( exportDefaultDeclaration , 'tagName' , [ j . stringLiteral ( '' ) ] ) ;
124
130
ensureImport ( root , 'tagName' , '@ember-decorators/component' ) ;
@@ -142,5 +148,13 @@ module.exports = function transformNativeComponent(root, options) {
142
148
143
149
let newSource = root . toSource ( ) ;
144
150
145
- return { newSource, tagName, elementId, classNames, classNameBindings, attributeBindings } ;
151
+ return {
152
+ newSource,
153
+ tagName,
154
+ elementId,
155
+ classNames,
156
+ classNameBindings,
157
+ attributeBindings,
158
+ eventHandlers,
159
+ } ;
146
160
} ;
0 commit comments