lib/hyperstack/component/element.rb in hyper-component-1.0.alpha1.4 vs lib/hyperstack/component/element.rb in hyper-component-1.0.alpha1.5

- old
+ new

@@ -37,22 +37,29 @@ @ref = x @_child_element._update_ref(x) if @_child_element end def ref - @ref || raise("#{self} has not been mounted yet") + return @ref if @ref + raise("The instance of #{self.type} has not been mounted yet") if properties[:ref] + raise("Attempt to get a ref on #{self.type} which is a static component.") end def dom_node - @type.is_a?(String) ? ref : ref.dom_node + `typeof #{ref}.$dom_node == 'function'` ? ref.dom_node : ref end - # Attach event handlers. + # Attach event handlers. skip false, nil and blank event names def on(*event_names, &block) - event_names.each { |event_name| merge_event_prop!(event_name, &block) } - @native = `React.cloneElement(#{@native}, #{@properties.shallow_to_n})` + any_found = false + event_names.each do |event_name| + next unless event_name && event_name.strip != '' + merge_event_prop!(event_name, &block) + any_found = true + end + @native = `React.cloneElement(#{@native}, #{@properties.shallow_to_n})` if any_found self end # Render element into DOM in the current rendering context. # Used for elements that are not yet in DOM, i.e. they are provided as children @@ -112,9 +119,12 @@ def merge_built_in_event_prop!(prop_name, &block) @properties.merge!( prop_name => %x{ function(){ var react_event = arguments[0]; + if (arguments.length == 0 || !react_event.nativeEvent) { + return #{yield(*Array(`arguments`))} + } var all_args; var other_args; if (arguments.length > 1) { all_args = Array.prototype.slice.call(arguments); other_args = all_args.slice(1, arguments.length);