Module: Baldur::UiHelperForms
- Included in:
- UiHelper
- Defined in:
- app/helpers/baldur/ui_helper_forms.rb
Instance Method Summary collapse
- #ui_date_field_tag(name, value = nil, label: nil, supporting_text: nil, placeholder: "YYYY-MM-DD", required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}, native_input_options: {}, toggle_label: "Open date picker", icon_name: "calendar", min_date: nil, max_date: nil) ⇒ Object
- #ui_menu_select_tag(name, options:, selected: nil, placeholder: "Select an option", label: nil, supporting_text: nil, wrapper_class: nil, data: nil, input_data: nil, disabled: false) ⇒ Object
- #ui_mobile_field_tag(name, value = nil, label: "Mobile", placeholder: "10-digit mobile number", country_code: "+91", required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}) ⇒ Object
- #ui_text_field_tag(name, value = nil, label: nil, supporting_text: nil, placeholder: nil, type: :text, required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}, multiline: false, prefix: nil, suffix: nil, &block) ⇒ Object
Instance Method Details
#ui_date_field_tag(name, value = nil, label: nil, supporting_text: nil, placeholder: "YYYY-MM-DD", required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}, native_input_options: {}, toggle_label: "Open date picker", icon_name: "calendar", min_date: nil, max_date: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/helpers/baldur/ui_helper_forms.rb', line 35 def ui_date_field_tag(name, value = nil, label: nil, supporting_text: nil, placeholder: "YYYY-MM-DD", required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}, native_input_options: {}, toggle_label: "Open date picker", icon_name: "calendar", min_date: nil, max_date: nil) = ( || {}).deep_dup = ( || {}).deep_dup input_id = [:id].presence || "ui-date-field-#{SecureRandom.hex(3)}" support_id = "#{input_id}-support" wrapper_classes = [ "field", "date-field", wrapper_class ].compact.join(" ") parsed_date = case value when Date value when Time, DateTime, ActiveSupport::TimeWithZone value.to_date when String begin Date.iso8601(value) rescue ArgumentError begin Date.parse(value) rescue ArgumentError nil end end else value.respond_to?(:to_date) ? value.to_date : nil end iso_value = parsed_date&.strftime("%Y-%m-%d") native_id = [:id].presence || "#{input_id}-native" display_value = if .key?(:value) [:value] else iso_value.presence || value.to_s.presence end [:id] = input_id [:class] = [ "text-field__control", "date-field__display", input_class, [:class] ].compact.join(" ") [:type] ||= "text" [:name] ||= name [:placeholder] ||= placeholder if placeholder.present? [:required] = true if required [:disabled] = true if disabled [:autocomplete] ||= "off" [:value] = display_value if display_value.present? && !.key?(:value) display_data = ([:data] || {}).dup display_data[:date_field_display] = true display_data[:date_field_target] ||= "display" display_data[:date_field_native_target] ||= native_id display_data[:field_label] ||= label if label.present? [:data] = display_data aria = [:aria].presence || {} describedby = [ aria[:describedby], support_id ].compact.join(" ").presence aria[:describedby] = describedby if describedby.present? [:aria] = aria if aria.present? [:id] = native_id [:class] = [ "date-field__native", [:class] ].compact.join(" ") [:type] ||= "date" .delete(:name) if [:name].blank? [:value] = iso_value if iso_value.present? && !.key?(:value) [:required] = true if required [:disabled] = true if disabled [:tabindex] ||= "-1" [:autocomplete] ||= "off" [:style] ||= "position:absolute; inset:auto; width:1px; height:1px; opacity:0; pointer-events:none;" if min_date.present? min_date_value = case min_date when Date, Time, DateTime, ActiveSupport::TimeWithZone min_date.strftime("%Y-%m-%d") when String min_date end [:min] = min_date_value if min_date_value.present? end if max_date.present? max_date_value = case max_date when Date, Time, DateTime, ActiveSupport::TimeWithZone max_date.strftime("%Y-%m-%d") when String max_date end [:max] = max_date_value if max_date_value.present? end native_data = ([:data] || {}).dup native_data[:date_field_native] = true native_data[:date_field_target] ||= "native" native_data[:date_field_name] ||= name native_data[:date_field_display_target] ||= input_id [:data] = native_data native_aria = [:aria].presence || {} native_aria[:hidden] = "true" native_aria[:label] ||= label if label.present? [:aria] = native_aria if native_aria.present? baldur_render "baldur/components/date_field", wrapper_classes: wrapper_classes, label: label, supporting_text: supporting_text, display_input_options: , native_input_options: , toggle_label: toggle_label, icon_name: icon_name, support_id: support_id end |
#ui_menu_select_tag(name, options:, selected: nil, placeholder: "Select an option", label: nil, supporting_text: nil, wrapper_class: nil, data: nil, input_data: nil, disabled: false) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'app/helpers/baldur/ui_helper_forms.rb', line 172 def (name, options:, selected: nil, placeholder: "Select an option", label: nil, supporting_text: nil, wrapper_class: nil, data: nil, input_data: nil, disabled: false) normalized = .map do |option| case option when Hash option.symbolize_keys when Array value, label_text, = option details = .is_a?(Hash) ? .symbolize_keys : {} { value: value, label: label_text }.merge(details) else { value: option, label: option } end end normalized.each do |option| option[:value] = option[:value].to_s option[:label] = option[:label].to_s option[:disabled] = !!option[:disabled] if option.key?(:disabled) option[:disabled] ||= option[:available] == false if option.key?(:available) option[:badge] = option[:badge].to_s if option[:badge] option[:badge_variant] = option[:badge_variant].to_sym if option[:badge_variant] option[:badge_size] = option[:badge_size].to_sym if option[:badge_size] option[:support] = option[:support].to_s if option[:support] option[:description] = option[:description].to_s if option[:description] end selected_value = selected.present? ? selected.to_s : nil selected_option = normalized.find { |opt| opt[:value] == selected_value } if selected_value unless selected_option || placeholder.present? selected_option = normalized.find { |opt| !opt[:disabled] } selected_value = selected_option&.dig(:value) end = selected_option&.dig(:label) || placeholder field_id = "ui-menu-select-#{SecureRandom.hex(4)}" auto_width_class = (wrapper_class) ? nil : "field--select-auto" wrapper_classes = [ "field", "field--select", auto_width_class, wrapper_class ].compact.join(" ") baldur_render "baldur/components/menu_select", name: name, field_id: field_id, wrapper_classes: wrapper_classes, wrapper_data: data, label: label, supporting_text: supporting_text, selected_value: selected_value, button_label: , options: normalized, input_data: input_data, disabled: disabled end |
#ui_mobile_field_tag(name, value = nil, label: "Mobile", placeholder: "10-digit mobile number", country_code: "+91", required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/helpers/baldur/ui_helper_forms.rb', line 145 def ui_mobile_field_tag(name, value = nil, label: "Mobile", placeholder: "10-digit mobile number", country_code: "+91", required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}) normalized_value = value.to_s.gsub(/\D+/, "").slice(0, 10) = ( || {}).deep_dup [:data] = ([:data] || {}).merge( mobile_input: true, mobile_country_code: country_code ) [:inputmode] ||= "numeric" [:pattern] ||= "\\d*" [:autocomplete] ||= "tel" merged_wrapper_class = [ wrapper_class, "field--mobile" ].compact.join(" ") ui_text_field_tag( name, normalized_value, label: label, placeholder: placeholder, type: :tel, required: required, disabled: disabled, wrapper_class: merged_wrapper_class, input_class: input_class, input_options: , prefix: country_code ) end |
#ui_text_field_tag(name, value = nil, label: nil, supporting_text: nil, placeholder: nil, type: :text, required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}, multiline: false, prefix: nil, suffix: nil, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/helpers/baldur/ui_helper_forms.rb', line 3 def ui_text_field_tag(name, value = nil, label: nil, supporting_text: nil, placeholder: nil, type: :text, required: false, disabled: false, wrapper_class: nil, input_class: nil, input_options: {}, multiline: false, prefix: nil, suffix: nil, &block) = ( || {}).deep_dup input_id = [:id].presence || "ui-text-field-#{SecureRandom.hex(3)}" field_classes = [ "field", "text-field", ("is-disabled" if disabled), wrapper_class ].compact.join(" ") control_classes = [ "text-field__control", input_class, [:class] ].compact.join(" ") support_id = "#{input_id}-support" [:id] = input_id [:class] = control_classes [:placeholder] = placeholder if placeholder.present? [:required] = true if required [:disabled] = true if disabled [:type] = type unless multiline [:name] = name [:value] = value unless value.nil? || multiline aria = [:aria].presence || {} aria[:describedby] = [ aria[:describedby], support_id ].compact.join(" ").presence [:aria] = aria if aria.present? baldur_render "baldur/components/text_field", wrapper_classes: field_classes, label: label, supporting_text: supporting_text, input_options: , input_value: multiline ? value.to_s : nil, multiline: multiline, prefix: prefix, suffix: suffix, support_id: support_id, supporting_slot: block_given? ? capture(&block) : nil end |