simple_form.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 :warning_hint, wrap_with: { tag: :span, class: [:hint, 'warning-hint'] }
  80. b.use :hint, wrap_with: { tag: :span, class: :hint }
  81. b.use :error, wrap_with: { tag: :span, class: :error }
  82. end
  83. config.wrappers :with_floating_label, class: [:input, :with_floating_label], hint_class: :field_with_hint, error_class: :field_with_errors do |b|
  84. b.use :html5
  85. b.use :label_input, wrap_with: { tag: :div, class: :label_input }
  86. b.use :hint, wrap_with: { tag: :span, class: :hint }
  87. b.use :error, wrap_with: { tag: :span, class: :error }
  88. end
  89. config.wrappers :with_block_label, class: [:input, :with_block_label], hint_class: :field_with_hint, error_class: :field_with_errors do |b|
  90. b.use :html5
  91. b.use :label
  92. b.use :warning_hint, wrap_with: { tag: :span, class: [:hint, 'warning-hint'] }
  93. b.use :hint, wrap_with: { tag: :span, class: :hint }
  94. b.use :input, wrap_with: { tag: :div, class: :label_input }
  95. b.use :error, wrap_with: { tag: :span, class: :error }
  96. end
  97. # The default wrapper to be used by the FormBuilder.
  98. config.default_wrapper = :default
  99. # Define the way to render check boxes / radio buttons with labels.
  100. # Defaults to :nested for bootstrap config.
  101. # inline: input + label
  102. # nested: label > input
  103. config.boolean_style = :nested
  104. # Default class for buttons
  105. config.button_class = 'btn'
  106. # Method used to tidy up errors. Specify any Rails Array method.
  107. # :first lists the first message for each field.
  108. # Use :to_sentence to list all errors for each field.
  109. # config.error_method = :first
  110. # Default tag used for error notification helper.
  111. config.error_notification_tag = :div
  112. # CSS class to add for error notification helper.
  113. config.error_notification_class = 'error_notification'
  114. # ID to add for error notification helper.
  115. # config.error_notification_id = nil
  116. # Series of attempts to detect a default label method for collection.
  117. # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
  118. # Series of attempts to detect a default value method for collection.
  119. # config.collection_value_methods = [ :id, :to_s ]
  120. # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
  121. # config.collection_wrapper_tag = nil
  122. # You can define the class to use on all collection wrappers. Defaulting to none.
  123. # config.collection_wrapper_class = nil
  124. # You can wrap each item in a collection of radio/check boxes with a tag,
  125. # defaulting to :span.
  126. # config.item_wrapper_tag = :span
  127. # You can define a class to use in all item wrappers. Defaulting to none.
  128. # config.item_wrapper_class = nil
  129. # How the label text should be generated altogether with the required text.
  130. config.label_text = ->(label, required, _explicit_label) { "#{label} #{required}" }
  131. # You can define the class to use on all labels. Default is nil.
  132. # config.label_class = nil
  133. # You can define the default class to be used on forms. Can be overridden
  134. # with `html: { :class }`. Defaulting to none.
  135. # config.default_form_class = nil
  136. # You can define which elements should obtain additional classes
  137. # config.generate_additional_classes_for = [:wrapper, :label, :input]
  138. # Whether attributes are required by default (or not). Default is true.
  139. # config.required_by_default = true
  140. # Tell browsers whether to use the native HTML5 validations (novalidate form option).
  141. # These validations are enabled in SimpleForm's internal config but disabled by default
  142. # in this configuration, which is recommended due to some quirks from different browsers.
  143. # To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
  144. # change this configuration to true.
  145. config.browser_validations = false
  146. # Collection of methods to detect if a file type was given.
  147. # config.file_methods = [ :mounted_as, :file?, :public_filename ]
  148. # Custom mappings for input types. This should be a hash containing a regexp
  149. # to match as key, and the input type that will be used when the field name
  150. # matches the regexp as value.
  151. # config.input_mappings = { /count/ => :integer }
  152. # Custom wrappers for input types. This should be a hash containing an input
  153. # type as key and the wrapper that will be used for all inputs with specified type.
  154. # config.wrapper_mappings = { string: :prepend }
  155. # Namespaces where SimpleForm should look for custom input classes that
  156. # override default inputs.
  157. # config.custom_inputs_namespaces << "CustomInputs"
  158. # Default priority for time_zone inputs.
  159. # config.time_zone_priority = nil
  160. # Default priority for country inputs.
  161. # config.country_priority = nil
  162. # When false, do not use translations for labels.
  163. # config.translate_labels = true
  164. # Automatically discover new inputs in Rails' autoload path.
  165. # config.inputs_discovery = true
  166. # Cache SimpleForm inputs discovery
  167. # config.cache_discovery = !Rails.env.development?
  168. # Default class for inputs
  169. # config.input_class = nil
  170. # Define the default class of the input wrapper of the boolean input.
  171. config.boolean_label_class = 'checkbox'
  172. # Defines if the default input wrapper class should be included in radio
  173. # collection wrappers.
  174. # config.include_default_input_wrapper_class = true
  175. # Defines which i18n scope will be used in Simple Form.
  176. # config.i18n_scope = 'simple_form'
  177. end