lib/under_os/ui/input.rb in under-os-1.1.0 vs lib/under_os/ui/input.rb in under-os-1.2.0

- old
+ new

@@ -1,41 +1,140 @@ class UnderOs::UI::Input < UnderOs::UI::View - include UnderOs::UI::Editable - wraps UITextField, tag: 'input' def initialize(options={}) super - # self.type = options[:type] if options[:type] + self.type = options[:type] if options[:type] + self.name = options[:name] if options[:name] self.value = options[:value] if options[:value] self.placeholder = options[:placeholder] if options[:placeholder] self.keyboard = options[:keyboard] if options[:keyboard] + self.disabled = true if options[:disabled] - @_.delegate = self + @_.delegate = self if @_.respond_to?(:delegate=) + + if @_.class == UITextField + @_.addTarget self, action: :handle_focus, forControlEvents:UIControlEventEditingDidBegin + @_.addTarget self, action: :handle_change, forControlEvents:UIControlEventEditingChanged + @_.addTarget self, action: :handle_blur, forControlEvents:UIControlEventEditingDidEnd + end end - # FIXME apparently there is a problem with this property in rubymotion - # def type - # if @_.secureTextEntry - # :password - # else - # :text - # end - # end + def name + @name + end - # def type=(type) - # case type.to_sym - # when :password then @_.secureTextEntry = true - # end - # end + def name=(text) + @name = text + end - def textFieldShouldReturn(textField) - hide_keyboard - false + def value + @_.text end + def value=(value) + @_.text = value + end + + def placeholder + @_.placeholder + end + + def placeholder=(value) + @_.placeholder = value + end + + def type + if @_.respond_to?(:secureTextEntry) && @_.secureTextEntry + :password + else + keyboard == :default ? :text : keyboard + end + end + + def type=(type) + case type.to_sym + when :password then @_.secureTextEntry = true + else self.keyboard = type + end + end + + def keyboard + KEYBOARDS.index(@_.keyboardType) + end + + def keyboard=(keyboard) + keyboard = keyboard.to_sym if keyboard.is_a?(String) + keyboard = KEYBOARDS[keyboard] || keyboard + + raise "Unknown keyboard type: #{keyboard}" if keyboard.is_a?(Symbol) + + @_.keyboardType = keyboard + end + def hide_keyboard + puts "DEPRECATED: please use the `#blur` method instead of `#hide_keyboard`" + blur + end + + KEYBOARDS = { + default: UIKeyboardTypeDefault, + text: UIKeyboardTypeDefault, + ascii: UIKeyboardTypeASCIICapable, + numeric: UIKeyboardTypeNumbersAndPunctuation, + url: UIKeyboardTypeURL, + numbers: UIKeyboardTypeNumberPad, + phone: UIKeyboardTypePhonePad, + name: UIKeyboardTypeNamePhonePad, + email: UIKeyboardTypeEmailAddress, + decimal: UIKeyboardTypeDecimalPad, + twitter: UIKeyboardTypeTwitter, + search: UIKeyboardTypeWebSearch + } + + def disabled + ! @_.isEnabled + end + + alias :disabled? :disabled + + def disabled=(value) + @_.enabled = ! value + end + + def disable + self.disabled = true + end + + def enable + self.disabled = false + end + + def focus + @_.becomeFirstResponder + end + + def blur @_.resignFirstResponder end +# delegate + + def textFieldShouldReturn(textField) + blur + end + +protected + + def handle_focus + emit('focus') + end + + def handle_change + emit('change') + end + + def handle_blur + emit('blur') + end end