From f4b1b022eec885fe7ca9c17cebe24fa33c5b7a8d Mon Sep 17 00:00:00 2001 From: Tom Chen Date: Sun, 16 Nov 2014 11:34:24 +0800 Subject: [PATCH] fix(create_child_component): event binding for child components --- src/helpers/create_child_component.js | 33 +++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/helpers/create_child_component.js b/src/helpers/create_child_component.js index e081764e..cbb780bd 100644 --- a/src/helpers/create_child_component.js +++ b/src/helpers/create_child_component.js @@ -2,6 +2,8 @@ var React = require("react/addons"), expose_getters_from = require("./expose_getters_from"), + to_event_map = require("../helpers/to_event_map"), + assign_event_map_to_prop_types_and_spec = require("../helpers/assign_event_map_to_prop_types_and_spec"), EventBindingMixin = require("../mixins/EventBindingMixin"); function ensure_instance_created (component, createdCallback, createFactory) { @@ -25,7 +27,12 @@ function setMapToInstance (component, instance) { } module.exports = (childName, eventNames, _created_callback) => { - var createdCallback = _created_callback || setMapToInstance; + var createdCallback = _created_callback || setMapToInstance, + + EVENT_MAP = to_event_map(eventNames), + + ChildPropTypes, + ChildSpec; function create_instance (component, context) { var ChildClass = context.getApi()[childName], @@ -35,13 +42,17 @@ module.exports = (childName, eventNames, _created_callback) => { return instance; } + ChildPropTypes = { + + }; + /* * shouldComponentUpdate: true. Always rerender for child */ - return React.createClass({ + ChildSpec = { displayName: childName, - mixins: [EventBindingMixin({__keys__: []})], + mixins: [EventBindingMixin(EVENT_MAP)], contextTypes: { getMap: React.PropTypes.func, @@ -50,6 +61,8 @@ module.exports = (childName, eventNames, _created_callback) => { getInstanceByRef: React.PropTypes.func }, + propTypes: ChildPropTypes, + getInitialState () { return { _instance: null @@ -63,10 +76,6 @@ module.exports = (childName, eventNames, _created_callback) => { }, create_instance); }, - componentWillUpdate () { - ensure_instance_created(this, this.clear_listeners); - }, - componentDidUpdate () { ensure_instance_created(this, (instance) => { instance.setOptions(this.props); @@ -86,13 +95,13 @@ module.exports = (childName, eventNames, _created_callback) => { return this._render(this.props, this.state); }, - get_event_names () { - return "animation_changed click clickable_changed cursor_changed dblclick drag dragend draggable_changed dragstart flat_changed icon_changed mousedown mouseout mouseover mouseup position_changed rightclick shape_changed title_changed visible_changed zindex_changed"; - }, - _render (props, state) { return null; } - }); + }; + + assign_event_map_to_prop_types_and_spec(EVENT_MAP, ChildPropTypes, ChildSpec); + + return React.createClass(ChildSpec); };