@@ -13,6 +13,7 @@ const {
1313 removeDecorator,
1414 ensureImport,
1515 isProperty,
16+ renameEventHandler,
1617} = require ( '../utils/native' ) ;
1718
1819const EVENT_HANDLER_METHODS = [
@@ -98,14 +99,6 @@ module.exports = function transformNativeComponent(root, options) {
9899 throw new SilentError ( `Using \`this.elementId\` is not supported in tagless components` ) ;
99100 }
100101
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-
109102 // analyze `elementId`, `attributeBindings`, `classNames` and `classNameBindings`
110103 let elementId = findElementId ( classBody ) ;
111104 debug ( 'elementId: %o' , elementId ) ;
@@ -119,6 +112,19 @@ module.exports = function transformNativeComponent(root, options) {
119112 let classNameBindings = findClassNameBindings ( classDeclaration ) ;
120113 debug ( 'classNameBindings: %o' , classNameBindings ) ;
121114
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+
122128 // set `@tagName('')`
123129 addClassDecorator ( exportDefaultDeclaration , 'tagName' , [ j . stringLiteral ( '' ) ] ) ;
124130 ensureImport ( root , 'tagName' , '@ember-decorators/component' ) ;
@@ -142,5 +148,13 @@ module.exports = function transformNativeComponent(root, options) {
142148
143149 let newSource = root . toSource ( ) ;
144150
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+ } ;
146160} ;
0 commit comments