Module: React::Component::ClassMethods

Defined in:
lib/react/component.rb

Instance Method Summary collapse

Instance Method Details

#default_propsObject



130
131
132
# File 'lib/react/component.rb', line 130

def default_props
  self.validator ? self.validator.default_props : {}
end

#define_state(*states) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/react/component.rb', line 138

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
      return unless @native
      self.state[name]
    end
    # setter
    define_method("#{name}=") do |new_state|
      return unless @native
      hash = {}
      hash[name] = new_state
      self.set_state(hash)

      new_state
    end
  end
end

#initial_stateObject



126
127
128
# File 'lib/react/component.rb', line 126

def initial_state
  self.init_state || {}
end

#params(&block) ⇒ Object



134
135
136
# File 'lib/react/component.rb', line 134

def params(&block)
  self.validator = React::Validator.build(&block)
end

#prop_typesObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/react/component.rb', line 110

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