Module: React::Component::ClassMethods
- Defined in:
- lib/react/opal/component.rb
Instance Method Summary collapse
- #consume_context(item, klass) ⇒ Object
- #default_props ⇒ Object
- #define_state(*states) ⇒ Object
- #define_state_prop(prop, &block) ⇒ Object
- #get_prop_type(klass) ⇒ Object
- #initial_state ⇒ Object
- #params(&block) ⇒ Object
- #prop_types ⇒ Object
- #provide_context(item, klass, &block) ⇒ Object
Instance Method Details
#consume_context(item, klass) ⇒ Object
177 178 179 180 |
# File 'lib/react/opal/component.rb', line 177 def consume_context(item, klass) self.context_types ||= {} self.context_types[item] = get_prop_type(klass) end |
#default_props ⇒ Object
131 132 133 |
# File 'lib/react/opal/component.rb', line 131 def default_props self.validator ? self.validator.default_props : {} end |
#define_state(*states) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/react/opal/component.rb', line 196 def define_state(*states) raise "Block could be only given when define exactly one state" if block_given? && states.count > 1 self.init_state = {} unless self.init_state if block_given? self.init_state[states[0]] = yield end states.each do |name| # getter define_method("#{name}") do self.state[name] end # setter define_method("#{name}=") do |new_state| hash = {} hash[name] = new_state self.set_state(hash) new_state end end end |
#define_state_prop(prop, &block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/react/opal/component.rb', line 143 def define_state_prop(prop, &block) define_state prop update_value = lambda do |new_value| new_value = instance_exec(new_value, &block) if block self.send("#{prop}=", new_value) end before_mount do # need to execute in context of each object instance_exec params[prop], &update_value end before_receive_props do |new_props| # need to execute in context of each object instance_exec new_props[prop], &update_value end end |
#get_prop_type(klass) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/react/opal/component.rb', line 159 def get_prop_type(klass) if klass == Proc `React.PropTypes.func` elsif klass.is_a?(Proc) `React.PropTypes.object` elsif klass == Boolean `React.PropTypes.bool` elsif klass.ancestors.include?(Numeric) `React.PropTypes.number` elsif klass == String `React.PropTypes.string` elsif klass == Array `React.PropTypes.array` else `React.PropTypes.object` end end |
#initial_state ⇒ Object
127 128 129 |
# File 'lib/react/opal/component.rb', line 127 def initial_state self.init_state || {} end |
#params(&block) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/react/opal/component.rb', line 135 def params(&block) if self.validator self.validator.evaluate_more_rules(&block) else self.validator = React::Validator.build(&block) end end |
#prop_types ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/react/opal/component.rb', line 111 def prop_types if self.validator { _componentValidator: %x{ function(props, propName, componentName) { var errors = #{validator.validate(Hash.new(`props`))}; var error = new Error(#{"In component `" + self.name + "`\n" + `errors`.join("\n")}); return #{`errors`.count > 0 ? `error` : `undefined`}; } } } else {} end end |
#provide_context(item, klass, &block) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/react/opal/component.rb', line 182 def provide_context(item, klass, &block) self.child_context_types ||= {} self.child_context_types[item] = get_prop_type(klass) self.child_context_get ||= {} self.child_context_get[item] = block unless method_defined?(:get_child_context) define_method(:get_child_context) do Hash[self.child_context_get.map do |item, blk| [item, instance_eval(&blk)] end].to_n end end end |