|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Primer |
| 4 | + module Forms |
| 5 | + # :nodoc: |
| 6 | + class FieldsetGroup < BaseComponent |
| 7 | + ## |
| 8 | + # @param title [String] The title displayed as the heading for the fieldset |
| 9 | + # @param inputs [Array<Primer::Forms::Dsl::Input>] Array of form inputs to be grouped |
| 10 | + # @param builder [ActionView::Helpers::FormBuilder] The form builder instance |
| 11 | + # @param form [Primer::Forms::BaseForm] The form object |
| 12 | + # @param layout [Symbol] Layout style for the input group (default: :default_layout) |
| 13 | + # @param heading_arguments [Hash] Arguments passed to the heading component |
| 14 | + # @option heading_arguments [String] :id The ID for the heading element |
| 15 | + # @option heading_arguments [Symbol] :tag The HTML tag for the heading (default: :h3) |
| 16 | + # @option heading_arguments [Symbol] :size The size of the heading (default: :medium) |
| 17 | + # @param group_arguments [Hash] Arguments passed to the input group component |
| 18 | + # @param system_arguments [Hash] Additional system arguments passed to the section wrapper |
| 19 | + def initialize( # rubocop:disable Metrics/AbcSize |
| 20 | + title:, |
| 21 | + inputs:, |
| 22 | + builder:, |
| 23 | + form:, |
| 24 | + layout: Primer::Forms::Group::DEFAULT_LAYOUT, |
| 25 | + heading_arguments: {}, |
| 26 | + group_arguments: {}, |
| 27 | + **system_arguments |
| 28 | + ) |
| 29 | + super() |
| 30 | + |
| 31 | + @title = title |
| 32 | + |
| 33 | + @heading_arguments = heading_arguments |
| 34 | + @heading_arguments[:id] ||= "subhead-#{SecureRandom.uuid}" |
| 35 | + @heading_arguments[:tag] ||= :h3 |
| 36 | + @heading_arguments[:size] ||= :medium |
| 37 | + |
| 38 | + @fieldset_arguments = { |
| 39 | + legend_text: @title, |
| 40 | + visually_hide_legend: true, |
| 41 | + aria: { labelledby: @heading_arguments[:id] } |
| 42 | + } |
| 43 | + @group_arguments = group_arguments.merge(inputs:, builder:, form:, layout:) |
| 44 | + |
| 45 | + @system_arguments = system_arguments |
| 46 | + @system_arguments[:tag] = :section |
| 47 | + @system_arguments[:mb] ||= 4 |
| 48 | + @system_arguments[:aria] ||= {} |
| 49 | + @system_arguments[:aria][:labelledby] = @heading_arguments[:id] |
| 50 | + @system_arguments[:hidden] = :none if inputs.all?(&:hidden?) |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | +end |
0 commit comments