Method: React::API.create_native_react_class

Defined in:
lib/react/api.rb

.create_native_react_class(type) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/react/api.rb', line 57

def self.create_native_react_class(type)
  raise "Provided class should define `render` method"  if !(type.method_defined? :render)
  render_fn = (type.method_defined? :_render_wrapper) ? :_render_wrapper : :render
  # this was hashing type.to_s, not sure why but .to_s does not work as it Foo::Bar::View.to_s just returns "View"

  @@component_classes[type] ||= begin
    comp = %x{
      class extends React.Component {
        constructor(props) {
          super(props);
          this.mixins = #{type.respond_to?(:native_mixins) ? type.native_mixins : `[]`};
          this.statics = #{type.respond_to?(:static_call_backs) ? type.static_call_backs.to_n : `{}`};
          this.state = {};
          this.__opalInstanceInitializedState = false;
          this.__opalInstanceSyncSetState = true;
          this.__opalInstance = #{type.new(`this`)};
          this.__opalInstanceInitializedState = true;
          this.__opalInstanceSyncSetState = false;
          this.__name = #{type.name};
        }
        static get displayName() {
          if (typeof this.__name != "undefined") {
            return this.__name;
          } else {
            return #{type.name};
          }
        }
        static set displayName(name) {
          this.__name = name;
        }
        static get defaultProps() {
          return #{type.respond_to?(:default_props) ? type.default_props.to_n : `{}`};
        }
        static get propTypes() {
          return  #{type.respond_to?(:prop_types) ? type.prop_types.to_n : `{}`};
        }
        componentWillMount() {
          if (#{type.method_defined? :component_will_mount}) {
            this.__opalInstanceSyncSetState = true;
            this.__opalInstance.$component_will_mount();
            this.__opalInstanceSyncSetState = false;
          }
        }
        componentDidMount() {
          this.__opalInstance.is_mounted = true
          if (#{type.method_defined? :component_did_mount}) {
            this.__opalInstanceSyncSetState = false;
            this.__opalInstance.$component_did_mount();
          }
        }
        componentWillReceiveProps(next_props) {
          if (#{type.method_defined? :component_will_receive_props}) {
            this.__opalInstanceSyncSetState = true;
            this.__opalInstance.$component_will_receive_props(Opal.Hash.$new(next_props));
            this.__opalInstanceSyncSetState = false;
          }
        }
        shouldComponentUpdate(next_props, next_state) {
          if (#{type.method_defined? :should_component_update?}) {
            this.__opalInstanceSyncSetState = false;
            return this.__opalInstance["$should_component_update?"](Opal.Hash.$new(next_props), Opal.Hash.$new(next_state));
          } else { return true; }
        }
        componentWillUpdate(next_props, next_state) {
          if (#{type.method_defined? :component_will_update}) {
            this.__opalInstanceSyncSetState = false;
            this.__opalInstance.$component_will_update(Opal.Hash.$new(next_props), Opal.Hash.$new(next_state));
          }
        }
        componentDidUpdate(prev_props, prev_state) {
          if (#{type.method_defined? :component_did_update}) {
            this.__opalInstanceSyncSetState = false;
            this.__opalInstance.$component_did_update(Opal.Hash.$new(prev_props), Opal.Hash.$new(prev_state));
          }
        }
        componentWillUnmount() {
          if (#{type.method_defined? :component_will_unmount}) {
            this.__opalInstanceSyncSetState = false;
            this.__opalInstance.$component_will_unmount();
          }
          this.__opalInstance.is_mounted = false;
        }

        render() {
          this.__opalInstanceSyncSetState = false;
          return this.__opalInstance.$send(render_fn).$to_n();
        }
      }
    }
    # check to see if there is an after_error callback.  If there is add a
    # componentDidCatch handler. Because legacy behavior is to allow any object
    # that responds to render to act as a component we have to make sure that
    # we have a callbacks_for method.  This all becomes much easier once issue
    # #270 is resolved.
    if type.respond_to?(:callbacks_for) && type.callbacks_for(:after_error) != []
      add_after_error_hook_to_native comp
    end
    comp
  end
end