lib/react/function_component/creator.rb in isomorfeus-react-16.6.8 vs lib/react/function_component/creator.rb in isomorfeus-react-16.8.0
- old
+ new
@@ -1,78 +1,32 @@
module React
module FunctionComponent
- class Creator
- def self.event_handler(name, &block)
+ module Creator
+ def self.extended(base)
%x{
- Opal.React.FunctionComponent.Runner.event_handlers[name] = function(event, info) {
- #{ruby_event = ::React::SyntheticEvent.new(`event`)};
- #{React::FunctionComponent::Runner.new(`{}`).instance_exec(ruby_event, `info`, &block)};
- }
- }
- end
-
- def self.function_component(component_name, &block)
- %x{
- var fun = function(props) {
+ base.react_component = function(props) {
Opal.React.render_buffer.push([]);
- Opal.React.active_components.push(Opal.React.FunctionComponent.Runner.event_handlers);
- #{React::FunctionComponent::Runner.new(`props`).instance_exec(&block)};
+ Opal.React.active_components.push(base);
+ #{base.new(`props`).instance_exec(&`base.function_block`)};
Opal.React.active_components.pop();
return Opal.React.render_buffer.pop();
}
- var const_names;
- if (component_name.includes('.')) {
- const_names = component_name.split('.');
- } else {
- const_names = [component_name];
- }
- var const_last = const_names.length - 1;
- const_names.reduce(function(prev, curr) {
- if (prev && prev[curr]) {
- return prev[curr];
- } else {
- if (const_names.indexOf(curr) === const_last) {
- prev[curr] = fun;
- return prev[curr];
- } else {
- prev[curr] = {};
- return prev[curr];
- }
- }
- }, Opal.global);
}
- end
- def self.memo_component(component_name, &block)
- %x{
- var fun = Opal.global.React.memo(function(props) {
- Opal.React.render_buffer.push([]);
- Opal.React.active_components.push(Opal.React.FunctionComponent.Runner.event_handlers);
- #{React::FunctionComponent::Runner.new(`props`).instance_exec(&block)};
- Opal.React.active_components.pop();
- return Opal.React.render_buffer.pop();
- });
- var const_names;
- if (component_name.includes('.')) {
- const_names = component_name.split('.');
- } else {
- const_names = [component_name];
- }
- var const_last = const_names.length - 1;
- const_names.reduce(function(prev, curr) {
- if (prev && prev[curr]) {
- return prev[curr];
- } else {
- if (const_names.indexOf(curr) === const_last) {
- prev[curr] = fun;
- return prev[curr];
- } else {
- prev[curr] = {};
- return prev[curr];
- }
- }
- }, Opal.global);
- }
+ base_module = base.to_s.deconstantize
+ if base_module != ''
+ base_module.constantize.define_singleton_method(base.to_s.demodulize) do |*args, &block|
+ `Opal.React.internal_prepare_args_and_render(#{base}.react_component, args, block)`
+ end
+ else
+ Object.define_method(base.to_s) do |*args, &block|
+ `Opal.React.internal_prepare_args_and_render(#{base}.react_component, args, block)`
+ end
+ end
+
+ def create_function(&block)
+ `base.function_block = #{block}`
+ end
end
end
end
end