motion-prime/sections/form.rb in motion-prime-0.1.7 vs motion-prime/sections/form.rb in motion-prime-0.2.0

- old
+ new

@@ -18,35 +18,53 @@ # KEYBOARD_HEIGHT_PORTRAIT = 216 KEYBOARD_HEIGHT_LANDSCAPE = 162 + class_attribute :text_field_limits, :text_view_limits class_attribute :fields_options attr_accessor :fields, :field_indexes, :keyboard_visible after_render :bind_keyboard_events def table_data fields.values end def render_table - @data_stamp = Time.now.to_i init_form_fields + set_data_stamp(self.field_indexes.values) self.table_view = screen.table_view( styles: [:base_form, name.to_sym], delegate: self, dataSource: self ).view end def render_cell(index, table) item = data[index.row] - - screen.table_view_cell styles: [:base_form_field, :"#{name}_field"], reuse_identifier: cell_name(table, index) do + styles = [:base_form_field, :"#{name}_field"] + if item.respond_to?(:container_styles) && item.container_styles.present? + styles += Array.wrap(item.container_styles) + end + screen.table_view_cell styles: styles, reuse_identifier: cell_name(table, index) do |cell_element| + item.cell_element = cell_element if item.respond_to?(:cell_element) item.render(to: screen) end end + def reload_cell(section) + field = section.name.to_sym + path = table_view.indexPathForRowAtPoint(section.cell.center) # do not use indexPathForCell here as field may be invisibe + table_view.beginUpdates + section.cell.removeFromSuperview + + fields[field] = load_field(self.class.fields_options[field]) + @data = nil + set_data_stamp([field_indexes[field]]) + table_view.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimationNone) + table_view.endUpdates + end + # Returns element based on field name and element name # # Examples: # form.element("email:input") # @@ -86,20 +104,10 @@ end # focus on field field(field_name).focus end - class << self - def field(name, options = {}) - options[:name] = name - options[:type] ||= :string - self.fields_options ||= {} - self.fields_options[name] = options - self.fields_options[name] - end - end - def set_height_with_keyboard return if keyboard_visible self.table_view.height -= KEYBOARD_HEIGHT_PORTRAIT self.keyboard_visible = true end @@ -146,18 +154,69 @@ end def textFieldDidBeginEditing(text_field) on_input_edit(text_field) end - private + def textView(text_view, shouldChangeTextInRange:range, replacementText:string) + limit = (self.class.text_view_limits || {}).find do |field_name, limit| + view("#{field_name}:input") + end.try(:last) + return true unless limit + allow_string_replacement?(text_view, limit, range, string) + end + def textField(text_field, shouldChangeCharactersInRange:range, replacementString:string) + limit = (self.class.text_field_limits || {}).find do |field_name, limit| + view("#{field_name}:input") + end.try(:last) + return true unless limit + allow_string_replacement?(text_field, limit, range, string) + end + + def allow_string_replacement?(target, limit, range, string) + if string.length.zero? || (range.length + limit - target.text.length) >= string.length + true + else + target.text.length < limit + end + end + + def load_field(field) + klass = "MotionPrime::#{field[:type].classify}FieldSection".constantize + klass.new(field.merge(form: self)) + end + + def render_field?(name) + true + end + + class << self + def field(name, options = {}) + options[:name] = name + options[:type] ||= :string + self.fields_options ||= {} + self.fields_options[name] = options + self.fields_options[name] + end + + def limit_text_field_length(name, limit) + self.text_field_limits ||= {} + self.text_field_limits[name] = limit + end + def limit_text_view_length(name, limit) + self.text_view_limits ||= {} + self.text_view_limits[name] = limit + end + end + + private def init_form_fields self.fields = {} self.field_indexes = {} index = 0 (self.class.fields_options || []).each do |key, field| - klass = "MotionPrime::#{field[:type].classify}FieldSection".constantize - self.fields[key] = klass.new(field.merge(form: self)) + next unless render_field?(key) + self.fields[key] = load_field(field) self.field_indexes[key] = index index += 1 end end end \ No newline at end of file