simple_form.rb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # frozen_string_literal: true
  2. # Use this setup block to configure all options available in SimpleForm.
  3. module AppendComponent
  4. def append(_wrapper_options = nil)
  5. @append ||= begin
  6. options[:append].to_s.html_safe if options[:append].present?
  7. end
  8. end
  9. end
  10. module RecommendedComponent
  11. def recommended(_wrapper_options = nil)
  12. return unless options[:recommended]
  13. key = options[:recommended].is_a?(Symbol) ? options[:recommended] : :recommended
  14. options[:label_text] = ->(raw_label_text, _required_label_text, _label_present) { safe_join([raw_label_text, ' ', content_tag(:span, I18n.t(key, scope: 'simple_form'), class: key)]) }
  15. nil
  16. end
  17. end
  18. module WarningHintComponent
  19. def warning_hint(_wrapper_options = nil)
  20. @warning_hint ||= begin
  21. options[:warning_hint].to_s.html_safe if options[:warning_hint].present?
  22. end
  23. end
  24. end
  25. SimpleForm.include_component(AppendComponent)
  26. SimpleForm.include_component(RecommendedComponent)
  27. SimpleForm.include_component(WarningHintComponent)
  28. SimpleForm.setup do |config|
  29. # Wrappers are used by the form builder to generate a
  30. # complete input. You can remove any component from the
  31. # wrapper, change the order or even add your own to the
  32. # stack. The options given below are used to wrap the
  33. # whole input.
  34. config.wrappers :default, class: :input, hint_class: :field_with_hint, error_class: :field_with_errors do |b|
  35. ## Extensions enabled by default
  36. # Any of these extensions can be disabled for a
  37. # given input by passing: `f.input EXTENSION_NAME => false`.
  38. # You can make any of these extensions optional by
  39. # renaming `b.use` to `b.optional`.
  40. # Determines whether to use HTML5 (:email, :url, ...)
  41. # and required attributes
  42. b.use :html5
  43. # Calculates placeholders automatically from I18n
  44. # You can also pass a string as f.input placeholder: "Placeholder"
  45. b.use :placeholder
  46. ## Optional extensions
  47. # They are disabled unless you pass `f.input EXTENSION_NAME => true`
  48. # to the input. If so, they will retrieve the values from the model
  49. # if any exists. If you want to enable any of those
  50. # extensions by default, you can change `b.optional` to `b.use`.
  51. # Calculates maxlength from length validations for string inputs
  52. b.optional :maxlength
  53. # Calculates pattern from format validations for string inputs
  54. b.optional :pattern
  55. # Calculates min and max from length validations for numeric inputs
  56. b.optional :min_max
  57. # Calculates readonly automatically from readonly attributes
  58. b.optional :readonly
  59. ## Inputs
  60. b.use :input
  61. b.use :hint, wrap_with: { tag: :span, class: :hint }
  62. b.use :error, wrap_with: { tag: :span, class: :error }
  63. ## full_messages_for
  64. # If you want to display the full error message for the attribute, you can
  65. # use the component :full_error, like:
  66. #
  67. # b.use :full_error, wrap_with: { tag: :span, class: :error }
  68. end
  69. config.wrappers :with_label, class: [:input, :with_label], hint_class: :field_with_hint, error_class: :field_with_errors do |b|
  70. b.use :html5
  71. b.wrapper tag: :div, class: :label_input do |ba|
  72. ba.optional :recommended
  73. ba.use :label
  74. ba.wrapper tag: :div, class: :label_input__wrapper do |bb|
  75. bb.use :input
  76. bb.optional :append, wrap_with: { tag: :div, class: 'label_input__append' }
  77. end
  78. end
  79. b.use :hint, wrap_with: { tag: :span, class: :hint }
  80. b.use :error, wrap_with: { tag: :span, class: :error }
  81. end
  82. config.wrappers :with_floating_label, class: [:input, :with_floating_label], hint_class: :field_with_hint, error_class: :field_with_errors do |b|
  83. b.use :html5
  84. b.use :label_input, wrap_with: { tag: :div, class: :label_input }
  85. b.use :hint, wrap_with: { tag: :span, class: :hint }
  86. b.use :error, wrap_with: { tag: :span, class: :error }
  87. end
  88. config.wrappers :with_block_label, class: [:input, :with_block_label], hint_class: :field_with_hint, error_class: :field_with_errors do |b|
  89. b.use :html5
  90. b.use :label
  91. b.use :hint, wrap_with: { tag: :span, class: :hint }
  92. b.use :warning_hint, wrap_with: { tag: :span, class: [:hint, 'warning-hint'] }
  93. b.use :input, wrap_with: { tag: :div, class: :label_input }
  94. b.use :error, wrap_with: { tag: :span, class: :error }
  95. end
  96. # The default wrapper to be used by the FormBuilder.
  97. config.default_wrapper = :default
  98. # Define the way to render check boxes / radio buttons with labels.
  99. # Defaults to :nested for bootstrap config.
  100. # inline: input + label
  101. # nested: label > input
  102. config.boolean_style = :nested
  103. # Default class for buttons
  104. config.button_class = 'btn'
  105. # Method used to tidy up errors. Specify any Rails Array method.
  106. # :first lists the first message for each field.
  107. # Use :to_sentence to list all errors for each field.
  108. # config.error_method = :first
  109. # Default tag used for error notification helper.
  110. config.error_notification_tag = :div
  111. # CSS class to add for error notification helper.
  112. config.error_notification_class = 'error_notification'
  113. # ID to add for error notification helper.
  114. # config.error_notification_id = nil
  115. # Series of attempts to detect a default label method for collection.
  116. # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
  117. # Series of attempts to detect a default value method for collection.
  118. # config.collection_value_methods = [ :id, :to_s ]
  119. # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
  120. # config.collection_wrapper_tag = nil
  121. # You can define the class to use on all collection wrappers. Defaulting to none.
  122. # config.collection_wrapper_class = nil
  123. # You can wrap each item in a collection of radio/check boxes with a tag,
  124. # defaulting to :span.
  125. # config.item_wrapper_tag = :span
  126. # You can define a class to use in all item wrappers. Defaulting to none.
  127. # config.item_wrapper_class = nil
  128. # How the label text should be generated altogether with the required text.
  129. config.label_text = lambda { |label, required, explicit_label| "#{label} #{required}" }
  130. # You can define the class to use on all labels. Default is nil.
  131. # config.label_class = nil
  132. # You can define the default class to be used on forms. Can be overridden
  133. # with `html: { :class }`. Defaulting to none.
  134. # config.default_form_class = nil
  135. # You can define which elements should obtain additional classes
  136. # config.generate_additional_classes_for = [:wrapper, :label, :input]
  137. # Whether attributes are required by default (or not). Default is true.
  138. # config.required_by_default = true
  139. # Tell browsers whether to use the native HTML5 validations (novalidate form option).
  140. # These validations are enabled in SimpleForm's internal config but disabled by default
  141. # in this configuration, which is recommended due to some quirks from different browsers.
  142. # To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
  143. # change this configuration to true.
  144. config.browser_validations = false
  145. # Collection of methods to detect if a file type was given.
  146. # config.file_methods = [ :mounted_as, :file?, :public_filename ]
  147. # Custom mappings for input types. This should be a hash containing a regexp
  148. # to match as key, and the input type that will be used when the field name
  149. # matches the regexp as value.
  150. # config.input_mappings = { /count/ => :integer }
  151. # Custom wrappers for input types. This should be a hash containing an input
  152. # type as key and the wrapper that will be used for all inputs with specified type.
  153. # config.wrapper_mappings = { string: :prepend }
  154. # Namespaces where SimpleForm should look for custom input classes that
  155. # override default inputs.
  156. # config.custom_inputs_namespaces << "CustomInputs"
  157. # Default priority for time_zone inputs.
  158. # config.time_zone_priority = nil
  159. # Default priority for country inputs.
  160. # config.country_priority = nil
  161. # When false, do not use translations for labels.
  162. # config.translate_labels = true
  163. # Automatically discover new inputs in Rails' autoload path.
  164. # config.inputs_discovery = true
  165. # Cache SimpleForm inputs discovery
  166. # config.cache_discovery = !Rails.env.development?
  167. # Default class for inputs
  168. # config.input_class = nil
  169. # Define the default class of the input wrapper of the boolean input.
  170. config.boolean_label_class = 'checkbox'
  171. # Defines if the default input wrapper class should be included in radio
  172. # collection wrappers.
  173. # config.include_default_input_wrapper_class = true
  174. # Defines which i18n scope will be used in Simple Form.
  175. # config.i18n_scope = 'simple_form'
  176. end