isomorfeus-react 16.13.9 → 16.13.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -10
  3. data/lib/isomorfeus/props/validator.rb +2 -2
  4. data/lib/isomorfeus/{react_config.rb → react/config.rb} +189 -189
  5. data/lib/isomorfeus/react/memcached_component_cache.rb +19 -0
  6. data/lib/isomorfeus/react/redis_component_cache.rb +19 -0
  7. data/lib/isomorfeus/{thread_local_component_cache.rb → react/thread_local_component_cache.rb} +15 -15
  8. data/lib/isomorfeus/react_view_helper.rb +231 -231
  9. data/lib/isomorfeus/top_level.rb +103 -103
  10. data/lib/isomorfeus/top_level_ssr.rb +42 -42
  11. data/lib/isomorfeus-react-material-ui.rb +4 -4
  12. data/lib/isomorfeus-react-native.rb +5 -0
  13. data/lib/isomorfeus-react-paper.rb +4 -0
  14. data/lib/isomorfeus-react.rb +120 -118
  15. data/lib/isomorfeus_react/lucid_app/api.rb +26 -26
  16. data/lib/isomorfeus_react/lucid_app/base.rb +7 -7
  17. data/lib/isomorfeus_react/lucid_app/mixin.rb +23 -22
  18. data/lib/isomorfeus_react/lucid_app/native_component_constructor.rb +48 -49
  19. data/lib/isomorfeus_react/lucid_app/native_lucid_component_constructor.rb +94 -95
  20. data/lib/isomorfeus_react/lucid_component/api.rb +75 -102
  21. data/lib/isomorfeus_react/lucid_component/app_store_proxy.rb +37 -37
  22. data/lib/isomorfeus_react/lucid_component/base.rb +7 -7
  23. data/lib/isomorfeus_react/lucid_component/class_store_proxy.rb +44 -44
  24. data/lib/isomorfeus_react/lucid_component/initializer.rb +14 -14
  25. data/lib/isomorfeus_react/lucid_component/instance_store_proxy.rb +44 -44
  26. data/lib/isomorfeus_react/lucid_component/mixin.rb +22 -21
  27. data/lib/isomorfeus_react/lucid_component/native_component_constructor.rb +35 -35
  28. data/lib/isomorfeus_react/lucid_component/native_lucid_component_constructor.rb +82 -108
  29. data/lib/isomorfeus_react/lucid_component/styles_api.rb +34 -0
  30. data/lib/isomorfeus_react/lucid_func/base.rb +7 -7
  31. data/lib/isomorfeus_react/lucid_func/initializer.rb +11 -11
  32. data/lib/isomorfeus_react/lucid_func/mixin.rb +18 -17
  33. data/lib/isomorfeus_react/lucid_func/native_component_constructor.rb +81 -81
  34. data/lib/isomorfeus_react/react/function_component/api.rb +105 -104
  35. data/lib/isomorfeus_react/react/function_component/base.rb +8 -8
  36. data/lib/isomorfeus_react/react/function_component/initializer.rb +10 -10
  37. data/lib/isomorfeus_react/react/function_component/mixin.rb +17 -17
  38. data/lib/isomorfeus_react/react/function_component/native_component_constructor.rb +48 -48
  39. data/lib/isomorfeus_react/react/memo_component/base.rb +8 -8
  40. data/lib/isomorfeus_react/react/memo_component/mixin.rb +17 -17
  41. data/lib/isomorfeus_react/react/memo_component/native_component_constructor.rb +49 -49
  42. data/lib/isomorfeus_react_material/lucid_material/app/base.rb +8 -8
  43. data/lib/isomorfeus_react_material/lucid_material/app/mixin.rb +20 -19
  44. data/lib/isomorfeus_react_material/lucid_material/app/native_component_constructor.rb +50 -51
  45. data/lib/isomorfeus_react_material/lucid_material/component/base.rb +9 -9
  46. data/lib/isomorfeus_react_material/lucid_material/component/mixin.rb +19 -18
  47. data/lib/isomorfeus_react_material/lucid_material/component/native_component_constructor.rb +36 -36
  48. data/lib/isomorfeus_react_material/lucid_material/func/base.rb +9 -9
  49. data/lib/isomorfeus_react_material/lucid_material/func/mixin.rb +15 -14
  50. data/lib/isomorfeus_react_material/lucid_material/func/native_component_constructor.rb +83 -83
  51. data/lib/isomorfeus_react_paper/lucid_paper/app/base.rb +9 -0
  52. data/lib/isomorfeus_react_paper/lucid_paper/app/mixin.rb +19 -0
  53. data/lib/isomorfeus_react_paper/lucid_paper/app/native_component_constructor.rb +32 -0
  54. data/lib/isomorfeus_react_paper/lucid_paper/component/base.rb +9 -0
  55. data/lib/isomorfeus_react_paper/lucid_paper/component/mixin.rb +18 -0
  56. data/lib/isomorfeus_react_paper/lucid_paper/component/native_component_constructor.rb +25 -0
  57. data/lib/isomorfeus_react_paper/lucid_paper/func/base.rb +9 -0
  58. data/lib/isomorfeus_react_paper/lucid_paper/func/mixin.rb +14 -0
  59. data/lib/isomorfeus_react_paper/lucid_paper/func/native_component_constructor.rb +71 -0
  60. data/lib/lucid_app/context.rb +7 -7
  61. data/lib/lucid_prop_declaration/mixin.rb +126 -126
  62. data/lib/react/children.rb +34 -34
  63. data/lib/react/component/api.rb +134 -133
  64. data/lib/react/component/base.rb +8 -8
  65. data/lib/react/component/callbacks.rb +115 -115
  66. data/lib/react/component/elements.rb +60 -60
  67. data/lib/react/component/features.rb +48 -48
  68. data/lib/react/component/history.rb +69 -65
  69. data/lib/react/component/initializer.rb +11 -11
  70. data/lib/react/component/location.rb +19 -15
  71. data/lib/react/component/match.rb +35 -31
  72. data/lib/react/component/mixin.rb +20 -20
  73. data/lib/react/component/native_component_constructor.rb +69 -95
  74. data/lib/react/component/props.rb +83 -83
  75. data/lib/react/component/resolution.rb +97 -97
  76. data/lib/react/component/state.rb +58 -54
  77. data/lib/react/component/styles.rb +66 -66
  78. data/lib/react/context_wrapper.rb +48 -44
  79. data/lib/react/native_constant_wrapper.rb +29 -29
  80. data/lib/react/ref.rb +16 -12
  81. data/lib/react/synthetic_event.rb +52 -52
  82. data/lib/react/version.rb +3 -3
  83. data/lib/react.rb +296 -262
  84. data/lib/react_dom.rb +41 -41
  85. data/lib/react_dom_server.rb +18 -18
  86. data/lib/react_native/component/elements.rb +203 -0
  87. data/lib/react_native/lucid_app/react_native_component_constructor.rb +51 -0
  88. data/lib/react_native/lucid_component/react_native_component_constructor.rb +37 -0
  89. data/lib/react_native/lucid_func/react_native_component_constructor.rb +82 -0
  90. data/lib/react_native/react.rb +120 -0
  91. metadata +76 -23
@@ -1,83 +1,83 @@
1
- module React
2
- module Component
3
- class Props
4
- def initialize(native)
5
- @native = native
6
- end
7
-
8
- def method_missing(prop, *args, &block)
9
- %x{
10
- const p = #@native.props;
11
- if (typeof p[prop] === 'undefined') {
12
- prop = Opal.React.lower_camelize(prop);
13
- if (typeof p[prop] === 'undefined') { return nil; }
14
- }
15
- return p[prop];
16
- }
17
- end
18
-
19
- def classes
20
- @classes ||= React::Component::Styles.new(@native, 'classes')
21
- end
22
-
23
- def children
24
- @native.JS[:props].JS[:children]
25
- end
26
-
27
- def theme
28
- @theme ||= React::Component::Styles.new(@native, 'theme')
29
- end
30
-
31
- def isomorfeus_store
32
- @native.JS[:props].JS[:isomorfeus_store]
33
- end
34
-
35
- # for router convenience
36
- def history
37
- return @history if @history
38
- return nil if `typeof #@native.props.history === 'undefined'`
39
- if `typeof #@native.props.history.action !== 'undefined'`
40
- @history = React::Component::History.new(@native)
41
- else
42
- @native.JS[:props].JS[:history]
43
- end
44
- end
45
-
46
- def location
47
- return @location if @location
48
- return nil if `typeof #@native.props.location === 'undefined'`
49
- if `typeof #@native.props.location.pathname !== 'undefined'`
50
- @location = React::Component::Location.new(@native)
51
- else
52
- @native.JS[:props].JS[:location]
53
- end
54
- end
55
-
56
- def match
57
- return @match if @match
58
- return nil if `typeof #@native.props.match === 'undefined'`
59
- if `typeof #@native.props.match.path !== 'undefined'`
60
- @match = React::Component::Match.new(@native)
61
- else
62
- @native.JS[:props].JS[:match]
63
- end
64
- end
65
-
66
- def to_h
67
- `Opal.Hash.$new(#@native.props)`.transform_keys!(&:underscore)
68
- end
69
-
70
- def to_json
71
- JSON.dump(to_transport)
72
- end
73
-
74
- def to_n
75
- @native.JS[:props]
76
- end
77
-
78
- def to_transport
79
- {}.merge(to_h)
80
- end
81
- end
82
- end
83
- end
1
+ module React
2
+ module Component
3
+ class Props
4
+ def initialize(native)
5
+ @native = native
6
+ end
7
+
8
+ def method_missing(prop, *args, &block)
9
+ %x{
10
+ const p = #@native.props;
11
+ if (typeof p[prop] === 'undefined') {
12
+ prop = Opal.React.lower_camelize(prop);
13
+ if (typeof p[prop] === 'undefined') { return nil; }
14
+ }
15
+ return p[prop];
16
+ }
17
+ end
18
+
19
+ def classes
20
+ @classes ||= React::Component::Styles.new(@native, 'classes')
21
+ end
22
+
23
+ def children
24
+ @native.JS[:props].JS[:children]
25
+ end
26
+
27
+ def theme
28
+ @theme ||= React::Component::Styles.new(@native, 'theme')
29
+ end
30
+
31
+ def isomorfeus_store
32
+ @native.JS[:props].JS[:isomorfeus_store]
33
+ end
34
+
35
+ # for router convenience
36
+ def history
37
+ return @history if @history
38
+ return nil if `typeof #@native.props.history === 'undefined'`
39
+ if `typeof #@native.props.history.action !== 'undefined'`
40
+ @history = React::Component::History.new(@native)
41
+ else
42
+ @native.JS[:props].JS[:history]
43
+ end
44
+ end
45
+
46
+ def location
47
+ return @location if @location
48
+ return nil if `typeof #@native.props.location === 'undefined'`
49
+ if `typeof #@native.props.location.pathname !== 'undefined'`
50
+ @location = React::Component::Location.new(@native)
51
+ else
52
+ @native.JS[:props].JS[:location]
53
+ end
54
+ end
55
+
56
+ def match
57
+ return @match if @match
58
+ return nil if `typeof #@native.props.match === 'undefined'`
59
+ if `typeof #@native.props.match.path !== 'undefined'`
60
+ @match = React::Component::Match.new(@native)
61
+ else
62
+ @native.JS[:props].JS[:match]
63
+ end
64
+ end
65
+
66
+ def to_h
67
+ `Opal.Hash.$new(#@native.props)`.transform_keys!(&:underscore)
68
+ end
69
+
70
+ def to_json
71
+ JSON.dump(to_transport)
72
+ end
73
+
74
+ def to_n
75
+ @native.JS[:props]
76
+ end
77
+
78
+ def to_transport
79
+ {}.merge(to_h)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,97 +1,97 @@
1
- module React
2
- module Component
3
- module Resolution
4
- def self.included(base)
5
- base.instance_exec do
6
- unless method_defined?(:_react_component_class_resolution_original_const_missing)
7
- alias _react_component_class_resolution_original_const_missing const_missing
8
- end
9
-
10
- def const_missing(const_name)
11
- %x{
12
- if (typeof Opal.global[const_name] !== "undefined" && (const_name[0] === const_name[0].toUpperCase())) {
13
- var new_const = #{React::NativeConstantWrapper.new(`Opal.global[const_name]`, const_name)};
14
- new_const.react_component = Opal.global[const_name];
15
- #{Object.const_set(const_name, `new_const`)};
16
- return new_const;
17
- } else {
18
- return #{_react_component_class_resolution_original_const_missing(const_name)};
19
- }
20
- }
21
- end
22
-
23
- # this is required for autoloading support, as the component may not be loaded and so its method is not registered.
24
- # must load it first, done by const_get, and next time the method will be there.
25
- unless method_defined?(:_react_component_class_resolution_original_method_missing)
26
- alias _react_component_class_resolution_original_method_missing method_missing
27
- end
28
-
29
- def method_missing(component_name, *args, &block)
30
- # check for ruby component and render it
31
- # otherwise pass on method missing
32
- %x{
33
- var constant;
34
- if (typeof self.iso_react_const_cache === 'undefined') { self.iso_react_const_cache = {}; }
35
- try {
36
- if (typeof self.iso_react_const_cache[component_name] !== 'undefined') {
37
- constant = self.iso_react_const_cache[component_name]
38
- } else {
39
- constant = self.$const_get(component_name);
40
- self.iso_react_const_cache[component_name] = constant;
41
- }
42
- if (typeof constant.react_component !== 'undefined') {
43
- return Opal.React.internal_prepare_args_and_render(constant.react_component, args, block);
44
- }
45
- } catch(err) { }
46
- return #{_react_component_class_resolution_original_method_missing(component_name, *args, block)};
47
- }
48
- end
49
- end
50
- end
51
-
52
- unless method_defined?(:_react_component_resolution_original_method_missing)
53
- alias _react_component_resolution_original_method_missing method_missing
54
- end
55
-
56
- def method_missing(component_name, *args, &block)
57
- # Further on it must check for modules, because $const_get does not take
58
- # the full nesting into account, as usually its called via $$ with the
59
- # nesting provided by the compiler.
60
- %x{
61
- var constant;
62
- if (typeof self.iso_react_const_cache === 'undefined') { self.iso_react_const_cache = {}; }
63
-
64
- if (typeof self.iso_react_const_cache[component_name] !== 'undefined') {
65
- constant = self.iso_react_const_cache[component_name]
66
- } else if (typeof self.$$is_a_module !== 'undefined') {
67
- try {
68
- constant = self.$const_get(component_name);
69
- self.iso_react_const_cache[component_name] = constant;
70
- } catch(err) { }
71
- } else {
72
- let sc = self.$class();
73
- try {
74
- constant = sc.$const_get(component_name);
75
- self.iso_react_const_cache[component_name] = constant;
76
- } catch(err) {
77
- var module_names = sc.$to_s().split("::");
78
- var module_name;
79
- for (var i = module_names.length - 1; i > 0; i--) {
80
- module_name = module_names.slice(0, i).join('::');
81
- try {
82
- constant = sc.$const_get(module_name).$const_get(component_name, false);
83
- self.iso_react_const_cache[component_name] = constant;
84
- break;
85
- } catch(err) { }
86
- }
87
- }
88
- }
89
- if (constant && typeof constant.react_component !== 'undefined') {
90
- return Opal.React.internal_prepare_args_and_render(constant.react_component, args, block);
91
- }
92
- return #{_react_component_resolution_original_method_missing(component_name, *args, block)};
93
- }
94
- end
95
- end
96
- end
97
- end
1
+ module React
2
+ module Component
3
+ module Resolution
4
+ def self.included(base)
5
+ base.instance_exec do
6
+ unless method_defined?(:_react_component_class_resolution_original_const_missing)
7
+ alias _react_component_class_resolution_original_const_missing const_missing
8
+ end
9
+
10
+ def const_missing(const_name)
11
+ %x{
12
+ if (typeof Opal.global[const_name] !== "undefined" && (const_name[0] === const_name[0].toUpperCase())) {
13
+ var new_const = #{React::NativeConstantWrapper.new(`Opal.global[const_name]`, const_name)};
14
+ new_const.react_component = Opal.global[const_name];
15
+ #{Object.const_set(const_name, `new_const`)};
16
+ return new_const;
17
+ } else {
18
+ return #{_react_component_class_resolution_original_const_missing(const_name)};
19
+ }
20
+ }
21
+ end
22
+
23
+ # this is required for autoloading support, as the component may not be loaded and so its method is not registered.
24
+ # must load it first, done by const_get, and next time the method will be there.
25
+ unless method_defined?(:_react_component_class_resolution_original_method_missing)
26
+ alias _react_component_class_resolution_original_method_missing method_missing
27
+ end
28
+
29
+ def method_missing(component_name, *args, &block)
30
+ # check for ruby component and render it
31
+ # otherwise pass on method missing
32
+ %x{
33
+ var constant;
34
+ if (typeof self.iso_react_const_cache === 'undefined') { self.iso_react_const_cache = {}; }
35
+ try {
36
+ if (typeof self.iso_react_const_cache[component_name] !== 'undefined') {
37
+ constant = self.iso_react_const_cache[component_name]
38
+ } else {
39
+ constant = self.$const_get(component_name);
40
+ self.iso_react_const_cache[component_name] = constant;
41
+ }
42
+ if (typeof constant.react_component !== 'undefined') {
43
+ return Opal.React.internal_prepare_args_and_render(constant.react_component, args, block);
44
+ }
45
+ } catch(err) { }
46
+ return #{_react_component_class_resolution_original_method_missing(component_name, *args, block)};
47
+ }
48
+ end
49
+ end
50
+ end
51
+
52
+ unless method_defined?(:_react_component_resolution_original_method_missing)
53
+ alias _react_component_resolution_original_method_missing method_missing
54
+ end
55
+
56
+ def method_missing(component_name, *args, &block)
57
+ # Further on it must check for modules, because $const_get does not take
58
+ # the full nesting into account, as usually its called via $$ with the
59
+ # nesting provided by the compiler.
60
+ %x{
61
+ var constant;
62
+ if (typeof self.iso_react_const_cache === 'undefined') { self.iso_react_const_cache = {}; }
63
+
64
+ if (typeof self.iso_react_const_cache[component_name] !== 'undefined') {
65
+ constant = self.iso_react_const_cache[component_name]
66
+ } else if (typeof self.$$is_a_module !== 'undefined') {
67
+ try {
68
+ constant = self.$const_get(component_name);
69
+ self.iso_react_const_cache[component_name] = constant;
70
+ } catch(err) { }
71
+ } else {
72
+ let sc = self.$class();
73
+ try {
74
+ constant = sc.$const_get(component_name);
75
+ self.iso_react_const_cache[component_name] = constant;
76
+ } catch(err) {
77
+ var module_names = sc.$to_s().split("::");
78
+ var module_name;
79
+ for (var i = module_names.length - 1; i > 0; i--) {
80
+ module_name = module_names.slice(0, i).join('::');
81
+ try {
82
+ constant = sc.$const_get(module_name).$const_get(component_name, false);
83
+ self.iso_react_const_cache[component_name] = constant;
84
+ break;
85
+ } catch(err) { }
86
+ }
87
+ }
88
+ }
89
+ if (constant && typeof constant.react_component !== 'undefined') {
90
+ return Opal.React.internal_prepare_args_and_render(constant.react_component, args, block);
91
+ }
92
+ return #{_react_component_resolution_original_method_missing(component_name, *args, block)};
93
+ }
94
+ end
95
+ end
96
+ end
97
+ end
@@ -1,54 +1,58 @@
1
- module React
2
- module Component
3
- class State
4
- include ::Native::Wrapper
5
-
6
- def method_missing(key, *args, &block)
7
- if `args.length > 0`
8
- new_state = `{}`
9
- new_state.JS[(`key.endsWith('=')` ? key.chop : key)] = args[0]
10
- if block_given?
11
- @native.JS.setState(new_state, `function() { block.$call(); }`)
12
- else
13
- @native.JS.setState(new_state, `null`)
14
- end
15
- else
16
- %x{
17
- if (typeof #@native.state[key] === 'undefined') { return nil; }
18
- return #@native.state[key];
19
- }
20
- end
21
- end
22
-
23
- def set_state(updater, &block)
24
- new_state = `{}`
25
- updater.each do |key, value|
26
- new_state.JS[key] = value
27
- end
28
- if block_given?
29
- @native.JS.setState(new_state, `function() { block.$call(); }`)
30
- else
31
- @native.JS.setState(new_state, `null`)
32
- end
33
- end
34
-
35
- def size
36
- `Object.keys(#@native.state).length`;
37
- end
38
-
39
- def to_n
40
- %x{
41
- var new_native = {};
42
- for (var key in #@native.state) {
43
- if (typeof #@native.state[key].$to_n !== "undefined") {
44
- new_native[key] = #@native.state[key].$to_n();
45
- } else {
46
- new_native[key] = #@native.state[key];
47
- }
48
- }
49
- return new_native;
50
- }
51
- end
52
- end
53
- end
54
- end
1
+ module React
2
+ module Component
3
+ class State
4
+ include ::Native::Wrapper
5
+
6
+ def initialize(native)
7
+ @native = native
8
+ end
9
+
10
+ def method_missing(key, *args, &block)
11
+ if `args.length > 0`
12
+ new_state = `{}`
13
+ new_state.JS[(`key.endsWith('=')` ? key.chop : key)] = args[0]
14
+ if block_given?
15
+ @native.JS.setState(new_state, `function() { block.$call(); }`)
16
+ else
17
+ @native.JS.setState(new_state, `null`)
18
+ end
19
+ else
20
+ %x{
21
+ if (typeof #@native.state[key] === 'undefined') { return nil; }
22
+ return #@native.state[key];
23
+ }
24
+ end
25
+ end
26
+
27
+ def set_state(updater, &block)
28
+ new_state = `{}`
29
+ updater.each do |key, value|
30
+ new_state.JS[key] = value
31
+ end
32
+ if block_given?
33
+ @native.JS.setState(new_state, `function() { block.$call(); }`)
34
+ else
35
+ @native.JS.setState(new_state, `null`)
36
+ end
37
+ end
38
+
39
+ def size
40
+ `Object.keys(#@native.state).length`;
41
+ end
42
+
43
+ def to_n
44
+ %x{
45
+ var new_native = {};
46
+ for (var key in #@native.state) {
47
+ if (typeof #@native.state[key].$to_n !== "undefined") {
48
+ new_native[key] = #@native.state[key].$to_n();
49
+ } else {
50
+ new_native[key] = #@native.state[key];
51
+ }
52
+ }
53
+ return new_native;
54
+ }
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,66 +1,66 @@
1
- module React
2
- module Component
3
- class Styles
4
-
5
- def initialize(native, props_prop = false)
6
- @native = native
7
- @props_prop = props_prop
8
- end
9
-
10
- def [](prop)
11
- method_missing(prop)
12
- end
13
-
14
- def []=(prop, val)
15
- method_missing(prop, val)
16
- end
17
-
18
- def deep_merge(a_hash)
19
- native_hash = a_hash.to_n
20
- React::Component::Styles.new(`Opal.React.merge_deep(#@native, native_hash)`)
21
- end
22
-
23
- def deep_merge!(a_hash)
24
- native_hash = a_hash.to_n
25
- `#@native = Opal.React.merge_deep(#@native, native_hash)`
26
- self
27
- end
28
-
29
- def method_missing(prop, *args, &block)
30
- %x{
31
- let value;
32
- if (#@props_prop) {
33
- if (!#@native.props[#@props_prop] || typeof #@native.props[#@props_prop][prop] === 'undefined') {
34
- console.warn("Style/Theme key '" + prop + "' returning nil!");
35
- return #{nil};
36
- }
37
- value = #@native.props[#@props_prop][prop];
38
- } else {
39
- if (!#@native || typeof #@native[prop] === 'undefined') {
40
- console.warn("Style/Theme key '" + prop + "' returning nil!");
41
- return #{nil};
42
- }
43
- value = #@native[prop];
44
- }
45
- if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value)) { return value; }
46
- if (typeof value === 'function') { return value.apply(#@native, args); }
47
- return Opal.React.Component.Styles.$new(value);
48
- }
49
- end
50
-
51
- def to_h
52
- %x{
53
- if (#@props_prop) { return Opal.Hash.$new(#@native.props[#@props_prop]); }
54
- else { return Opal.Hash.$new(#@native); }
55
- }
56
- end
57
-
58
- def to_n
59
- %x{
60
- if (#@props_prop) { return #@native.props[#@props_prop]; }
61
- else { return #@native; }
62
- }
63
- end
64
- end
65
- end
66
- end
1
+ module React
2
+ module Component
3
+ class Styles
4
+
5
+ def initialize(native, props_prop = false)
6
+ @native = native
7
+ @props_prop = props_prop
8
+ end
9
+
10
+ def [](prop)
11
+ method_missing(prop)
12
+ end
13
+
14
+ def []=(prop, val)
15
+ method_missing(prop, val)
16
+ end
17
+
18
+ def deep_merge(a_hash)
19
+ native_hash = a_hash.to_n
20
+ React::Component::Styles.new(`Opal.React.merge_deep(#@native, native_hash)`)
21
+ end
22
+
23
+ def deep_merge!(a_hash)
24
+ native_hash = a_hash.to_n
25
+ `#@native = Opal.React.merge_deep(#@native, native_hash)`
26
+ self
27
+ end
28
+
29
+ def method_missing(prop, *args, &block)
30
+ %x{
31
+ let value;
32
+ if (#@props_prop) {
33
+ if (!#@native.props[#@props_prop] || typeof #@native.props[#@props_prop][prop] === 'undefined') {
34
+ console.warn("Style/Theme key '" + prop + "' returning nil!");
35
+ return #{nil};
36
+ }
37
+ value = #@native.props[#@props_prop][prop];
38
+ } else {
39
+ if (!#@native || typeof #@native[prop] === 'undefined') {
40
+ console.warn("Style/Theme key '" + prop + "' returning nil!");
41
+ return #{nil};
42
+ }
43
+ value = #@native[prop];
44
+ }
45
+ if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value)) { return value; }
46
+ if (typeof value === 'function') { return value.apply(#@native, args); }
47
+ return Opal.React.Component.Styles.$new(value);
48
+ }
49
+ end
50
+
51
+ def to_h
52
+ %x{
53
+ if (#@props_prop) { return Opal.Hash.$new(#@native.props[#@props_prop]); }
54
+ else { return Opal.Hash.$new(#@native); }
55
+ }
56
+ end
57
+
58
+ def to_n
59
+ %x{
60
+ if (#@props_prop) { return #@native.props[#@props_prop]; }
61
+ else { return #@native; }
62
+ }
63
+ end
64
+ end
65
+ end
66
+ end