@@ -19,7 +19,7 @@ def process_exception(e, component, reraise = nil)
1919 end
2020
2121 def validator
22- @validator ||= React ::Validator . new
22+ @validator ||= React ::Validator . new ( self )
2323 end
2424
2525 def prop_types
@@ -47,50 +47,71 @@ def params(&block)
4747 end
4848
4949 def define_param_method ( name , param_type )
50+ wrapper = const_get ( "PropsWrapper" )
5051 if param_type == React ::Observable
51- ( @two_way_params ||= [ ] ) << name
52- define_method ( "#{ name } " ) do
53- params [ name ] . instance_variable_get ( "@value" ) if params [ name ]
52+ # (@two_way_params ||= []) << name
53+ wrapper . define_method ( "#{ name } " ) do
54+ @props_hash [ name ] . instance_variable_get ( "@value" ) if @props_hash [ name ]
5455 end
55- define_method ( "#{ name } !" ) do |*args |
56- return unless params [ name ]
56+ wrapper . define_method ( "#{ name } !" ) do |*args |
57+ return unless @props_hash [ name ]
5758 if args . count > 0
58- current_value = params [ name ] . instance_variable_get ( "@value" )
59- params [ name ] . call args [ 0 ]
59+ current_value = @props_hash [ name ] . instance_variable_get ( "@value" )
60+ @props_hash [ name ] . call args [ 0 ]
6061 current_value
6162 else
62- current_value = params [ name ] . instance_variable_get ( "@value" )
63- params [ name ] . call current_value unless @dont_update_state rescue nil # rescue in case we in middle of render
64- params [ name ]
63+ current_value = @props_hash [ name ] . instance_variable_get ( "@value" )
64+ @props_hash [ name ] . call current_value unless @dont_update_state rescue nil # rescue in case we in middle of render
65+ @props_hash [ name ]
6566 end
6667 end
68+ define_method ( "#{ name } " ) { deprecated_params_method ( "#{ name } " ) }
69+ define_method ( "#{ name } !" ) { |*args | deprecated_params_method ( "#{ name } !" , *args ) }
6770 elsif param_type == Proc
68- define_method ( "#{ name } " ) do |*args , &block |
69- params [ name ] . call ( *args , &block ) if params [ name ]
71+ wrapper . define_method ( "#{ name } " ) do |*args , &block |
72+ @props_hash [ name ] . call ( *args , &block ) if @props_hash [ name ]
7073 end
74+ define_method ( "#{ name } " ) { deprecated_params_method ( "#{ name } " , *args , &block ) }
7175 else
72- define_method ( "#{ name } " ) do
76+ wrapper . define_method ( "#{ name } " ) do
7377 @processed_params [ name ] ||= if param_type . respond_to? :_react_param_conversion
74- param_type . _react_param_conversion params [ name ]
78+ param_type . _react_param_conversion @props_hash [ name ]
7579 elsif param_type . is_a? ( Array ) && param_type [ 0 ] . respond_to? ( :_react_param_conversion )
76- params [ name ] . collect { |param | param_type [ 0 ] . _react_param_conversion param }
80+ @props_hash [ name ] . collect { |param | param_type [ 0 ] . _react_param_conversion param }
7781 else
78- params [ name ]
82+ @props_hash [ name ]
7983 end
8084 end
85+ define_method ( "#{ name } " ) { deprecated_params_method ( "#{ name } " ) }
86+ end
87+ end
88+
89+ def param ( *args )
90+ if args [ 0 ] . is_a? Hash
91+ options = args [ 0 ]
92+ name = options . first [ 0 ]
93+ default = options . first [ 1 ]
94+ options . delete ( name )
95+ options . merge! ( { default : default } )
96+ else
97+ name = args [ 0 ]
98+ options = args [ 1 ] || { }
99+ end
100+ if options [ :default ]
101+ validator . optional ( name , options )
102+ else
103+ validator . requires ( name , options )
81104 end
82105 end
83106
84107 def required_param ( name , options = { } )
85108 validator . requires ( name , options )
86- define_param_method ( name , options [ :type ] )
87109 end
88110
89111 alias_method :require_param , :required_param
90112
91113 def optional_param ( name , options = { } )
92114 validator . optional ( name , options )
93- define_param_method ( name , options [ :type ] ) unless name == :params
94115 end
95116
96117 def collect_other_params_as ( name )
@@ -130,7 +151,6 @@ def define_state_methods(this, name, from = nil, &block)
130151 React ::State . set_state ( from || self , name , new_state )
131152 end
132153 this . define_method ( "#{ name } !" ) do |*args |
133- #return unless @native
134154 if args . count > 0
135155 yield name , React ::State . get_state ( from || self , name ) , args [ 0 ] if block && block . arity > 0
136156 current_value = React ::State . get_state ( from || self , name )
0 commit comments