Sha256: e94b3bb671107715e5a2d38474df24cfdfe455d366520cef38e81ef6dfe2c6b7

Contents?: true

Size: 1.94 KB

Versions: 8

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require "govuk_design_system_formbuilder"

module GOVUKDesignSystemFormBuilder
  module Elements
    class Document < Base
      include FileElement

      MIME_TYPES            = %w[
        image/png image/gif image/jpeg image/webp
        application/pdf
      ].freeze

      def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &block)
        super(builder, object_name, attribute_name, &block)

        @mime_types      = MIME_TYPES || kwargs[:mime_types]
        @label           = label
        @caption         = caption
        @hint            = hint
        @html_attributes = kwargs.merge(file_input_options)
        @form_group      = form_group
      end

      def preview
        options = {}
        add_option(options, :data, "#{stimulus_controller}_target", "preview")
        add_option(options, :class, "preview-file")
        add_option(options, :class, "hidden") unless preview?

        tag.div **options do
          filename = @builder.object.send(@attribute_name).filename.to_s
          tag.p(filename, class: "preview-filename") + destroy_element_trigger
        end
      end

      private

      def stimulus_controller
        "document-field"
      end

      def form_group_class
        "govuk-document-field"
      end
    end
  end
end

module GOVUKDesignSystemFormBuilder
  module Builder
    delegate :config, to: GOVUKDesignSystemFormBuilder

    # Generates a +div+ element with an +input+ with +type=file+ with a label, optional hint.
    #
    # @example A upload field with label as a proc
    #   = f.govuk_file_field :data, label: -> { tag.h3('Upload your document') }
    #
    def govuk_document_field(attribute_name, label: {}, caption: {}, hint: {}, form_group: {}, **kwargs, &block)
      Elements::Document.new(self, object_name, attribute_name, label:, caption:, hint:, form_group:, **kwargs,
                             &block).html
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
katalyst-koi-4.4.1 lib/govuk_design_system_formbuilder/elements/document.rb
katalyst-koi-4.4.0 lib/govuk_design_system_formbuilder/elements/document.rb
katalyst-koi-4.3.5 lib/govuk_design_system_formbuilder/elements/document.rb
katalyst-koi-4.3.4 lib/govuk_design_system_formbuilder/elements/document.rb
katalyst-koi-4.3.3 lib/govuk_design_system_formbuilder/elements/document.rb
katalyst-koi-4.3.2 lib/govuk_design_system_formbuilder/elements/document.rb
katalyst-koi-4.3.1 lib/govuk_design_system_formbuilder/elements/document.rb
katalyst-koi-4.3.0 lib/govuk_design_system_formbuilder/elements/document.rb