|
1 | 1 | module Flex |
2 | | - # Custom form builder. Beyond adding USWDS classes, this also |
3 | | - # supports setting the label, hint, and error messages by just |
4 | | - # using the field helpers (i.e text_field, check_box), and adds |
| 2 | + # FormBuilder is a custom form builder that provides USWDS-styled form components. |
| 3 | + # Beyond adding USWDS classes, this also supports setting the label, hint, and error |
| 4 | + # messages by just using the field helpers (i.e text_field, check_box), and adds |
5 | 5 | # additional helpers like fieldset and hint. |
6 | | - # https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html |
| 6 | + # |
| 7 | + # @see https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html |
| 8 | + # @see https://designsystem.digital.gov/components/form-controls/ |
| 9 | + # |
| 10 | + # @example Basic usage |
| 11 | + # <%= flex_form_with(model: @user) do |f| %> |
| 12 | + # <%= f.text_field :name, label: "Full Name", hint: "Enter your legal name" %> |
| 13 | + # <%= f.email_field :email, label: "Email Address" %> |
| 14 | + # <%= f.submit "Save" %> |
| 15 | + # <% end %> |
| 16 | + # |
7 | 17 | class FormBuilder < ActionView::Helpers::FormBuilder |
8 | 18 | standard_helpers = %i[email_field file_field password_field text_area text_field] |
9 | 19 |
|
| 20 | + # Initializes a new FormBuilder instance and sets up the form with USWDS classes. |
| 21 | + # |
| 22 | + # @param args [Array] Arguments passed to the parent FormBuilder constructor |
10 | 23 | def initialize(*args) |
11 | 24 | super |
12 | 25 | self.options[:html] ||= {} |
@@ -55,6 +68,13 @@ def initialize(*args) |
55 | 68 | end |
56 | 69 | end |
57 | 70 |
|
| 71 | + # Renders a checkbox field with USWDS styling. |
| 72 | + # |
| 73 | + # @param [Symbol] attribute The attribute name |
| 74 | + # @param [Hash] options Options for the checkbox |
| 75 | + # @param args [Array] Additional arguments for the standard checkbox helper |
| 76 | + # @option options [String] :label Custom label text |
| 77 | + # @return [String] The rendered HTML for the checkbox |
58 | 78 | def check_box(attribute, options = {}, *args) |
59 | 79 | append_to_option(options, :class, " #{us_class_for_field_type(:check_box)}") |
60 | 80 |
|
@@ -143,6 +163,14 @@ def date_picker(attribute, options = {}) |
143 | 163 | text_field(attribute, options.merge(value: value, group_options: group_options)) |
144 | 164 | end |
145 | 165 |
|
| 166 | + # Renders a memorable date input with month, day, and year fields. |
| 167 | + # |
| 168 | + # @param [Symbol] attribute The attribute name |
| 169 | + # @param [Hash] options Options for the memorable date |
| 170 | + # @option options [String] :legend Custom legend text |
| 171 | + # @option options [String] :hint Custom hint text |
| 172 | + # @return [String] The rendered HTML for the memorable date input |
| 173 | + # @see https://designsystem.digital.gov/components/memorable-date/ |
146 | 174 | def memorable_date(attribute, options = {}) |
147 | 175 | legend_text = options.delete(:legend) || human_name(attribute) |
148 | 176 | hint_text = options.delete(:hint) || I18n.t("flex.form_builder.memorable_date_hint") |
|
0 commit comments