Module: React

Defined in:
lib/react.rb,
lib/react/ref.rb,
lib/react/version.rb,
lib/react/children.rb,
lib/react/component/api.rb,
lib/react/component/base.rb,
lib/react/component/match.rb,
lib/react/component/mixin.rb,
lib/react/component/props.rb,
lib/react/component/state.rb,
lib/react/context_wrapper.rb,
lib/react/synthetic_event.rb,
lib/react/component/styles.rb,
lib/react/component/history.rb,
lib/react/component/elements.rb,
lib/react/component/features.rb,
lib/react/component/location.rb,
lib/react/component/callbacks.rb,
lib/react/memo_component/base.rb,
lib/react/pure_component/base.rb,
lib/react/component/resolution.rb,
lib/react/component/unsafe_api.rb,
lib/react/memo_component/mixin.rb,
lib/react/pure_component/mixin.rb,
lib/react/component/initializer.rb,
lib/react/function_component/api.rb,
lib/react/memo_component/creator.rb,
lib/react/component/event_handler.rb,
lib/react/function_component/base.rb,
lib/react/native_constant_wrapper.rb,
lib/react/function_component/mixin.rb,
lib/react/function_component/creator.rb,
lib/react/function_component/resolution.rb,
lib/react/function_component/event_handler.rb,
lib/react/component/should_component_update.rb,
lib/react/component/native_component_constructor.rb,
lib/react/component/native_component_validate_prop.rb

Defined Under Namespace

Modules: Children, Component, FunctionComponent, MemoComponent, PureComponent Classes: ContextWrapper, NativeConstantWrapper, Ref, SyntheticEvent

Constant Summary collapse

VERSION =
'16.9.2'

Class Method Summary collapse

Class Method Details

.clone_element(ruby_react_element, props = nil, children = nil, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/react.rb', line 111

def self.clone_element(ruby_react_element, props = nil, children = nil, &block)
  block_result = `null`
  if block_given?
    block_result = block.call
    block_result = `null` unless block_result
  end
  native_props = props ? `Opal.React.to_native_react_props(props)` : `null`
  `Opal.global.React.cloneElement(ruby_react_element.$to_n(), native_props, block_result)`
end

.create_context(const_name, default_value) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/react.rb', line 121

def self.create_context(const_name, default_value)
  %x{
    Opal.global[const_name] = Opal.global.React.createContext(default_value);
    var new_const = #{React::ContextWrapper.new(`Opal.global[const_name]`)};
    #{Object.const_set(const_name, `new_const`)};
    return new_const;
  }
end

.create_element(type, props = nil, children = nil, &block) ⇒ Object



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
157
158
# File 'lib/react.rb', line 130

def self.create_element(type, props = nil, children = nil, &block)
  %x{
    var component = null;
    var block_result = null;
    var native_props = null;
    if (typeof type.react_component !== 'undefined') {
      component = type.react_component;
    } else {
      component = type;
    }

    Opal.React.render_buffer.push([]);
    #{
      native_props = `Opal.React.to_native_react_props(props)` if props;
    }
    if (block !== nil) {
      block_result = block.$call()
      if (block_result && (block_result !== nil && (typeof block_result === "string" || typeof block_result.$$typeof === "symbol" ||
        (typeof block_result.constructor !== "undefined" && block_result.constructor === Array && block_result[0] && typeof block_result[0].$$typeof === "symbol")
        ))) {
        Opal.React.render_buffer[Opal.React.render_buffer.length - 1].push(block_result);
      }
      children = Opal.React.render_buffer.pop()
      if (children.length == 1) { children = children[0]; }
      else if (children.length == 0) { children = null; }
    }
    return Opal.global.React.createElement(component, native_props, children);
  }
end

.create_factory(type) ⇒ Object



160
161
162
163
# File 'lib/react.rb', line 160

def self.create_factory(type)
  native_function = `Opal.global.React.createFactory(type)`
  proc { `native_function.call()` }
end

.create_refObject



166
167
168
# File 'lib/react.rb', line 166

def self.create_ref
  React::Ref.new(`Opal.global.React.createRef()`)
end

.forwardRef(&block) ⇒ Object



170
171
172
173
# File 'lib/react.rb', line 170

def self.forwardRef(&block)
  # TODO whats the return here? A React:Element?, doc says a React node, whats that?
  `Opal.global.React.forwardRef( function(props, ref) { return block.$call().$to_n(); })`
end

.is_valid_element(react_element) ⇒ Object



175
176
177
# File 'lib/react.rb', line 175

def self.is_valid_element(react_element)
  `Opal.global.React.isValidElement(react_element)`
end

.lazy(import_statement_function) ⇒ Object



179
180
181
# File 'lib/react.rb', line 179

def self.lazy(import_statement_function)
  `Opal.global.React.lazy(import_statement_function)`
end

.memo(function_component, &block) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/react.rb', line 183

def self.memo(function_component, &block)
  if block_given?
    %x{
      var fun = function(prev_props, next_props) {
        return #{block.call(::React::Component::Props.new(`{props: prev_props}`), ::React::Component::Props.new(`{props: next_props}`))};
      }
      return Opal.global.React.memo(function_component, fun);
    }
  else
    `Opal.global.React.memo(function_component)`
  end
end