The personal finance app for everyone (by everyone)
Ruby
Style file inputs through the form builder (#3804)
Guillem Arias Faustefd51d788b0cf
diff --git a/app/helpers/styled_form_builder.rb b/app/helpers/styled_form_builder.rbindex 7899dd5ea3..e03ef83ec6 100644--- a/app/helpers/styled_form_builder.rb+++ b/app/helpers/styled_form_builder.rb@@ -27,6 +27,29 @@ class StyledFormBuilder < ActionView::Helpers::FormBuildersuper(method, tag_value, merged_options)end+ # The browser's own file button otherwise, unstyled beside every other+ # control. Given a label, it sits in the same bordered field as the rest;+ # the selector button is a compact chip, so the field keeps their height.+ # Without a label it comes back bare, as the dropzones use it: they pass+ # `hidden` and draw their own target, and the classes merge so that stays.+ #+ # The chip inherits its colour rather than taking `file:text-primary`: that+ # utility's dark variant is a `:where()` selector, which cannot follow the+ # pseudo-element, so the chip kept its light-mode text on a dark ground.+ FILE_FIELD_CLASSES = "w-full text-sm text-primary cursor-pointer " \+ "file:mr-2 file:px-2 file:rounded-md file:border-0 file:bg-container-inset " \+ "file:font-medium file:cursor-pointer hover:file:bg-container-inset-hover".freeze++ def file_field(method, options = {})+ form_options = options.slice(:label, :label_tooltip, :container_class, :required)+ html_options = options.except(:label, :label_tooltip, :container_class)+ html_options[:class] = @template.class_names(FILE_FIELD_CLASSES, html_options[:class])++ return super(method, html_options) unless form_options[:label]++ build_field(method, form_options, html_options) { |merged_options| super(method, merged_options) }+ end+def select(method, choices, options = {}, html_options = {})field_options = normalize_options(options, html_options)diff --git a/app/views/account_statements/index.html.erb b/app/views/account_statements/index.html.erbindex dba3f16774..1d45dede56 100644--- a/app/views/account_statements/index.html.erb+++ b/app/views/account_statements/index.html.erb@@ -15,14 +15,14 @@</div><%= styled_form_with url: account_statements_path, scope: :account_statement, multipart: true, class: "grid gap-3 lg:grid-cols-[1fr_16rem_auto] lg:items-end" do |form| %>- <div>- <%= form.label :files, t("account_statements.form.files_label"), class: "form-field__label" %>- <%= form.file_field :files,- multiple: true,- accept: AccountStatement::ACCEPTED_FILE_EXTENSIONS.join(","),- class: "form-field__input" %>- <p class="text-xs text-secondary mt-2"><%= t("account_statements.form.files_hint", max_size: AccountStatement::MAX_FILE_SIZE / 1.megabyte) %></p>- </div>+ <%= form.file_field :files,+ label: t("account_statements.form.files_label"),+ multiple: true,+ accept: AccountStatement::ACCEPTED_FILE_EXTENSIONS.join(",") %>+ <%# Its own row on wide screens, so the three fields line up with each+ other rather than with the bottom of this hint. Stacked, it still+ follows the file field it describes. %>+ <p class="text-xs text-secondary -mt-1 lg:col-start-1 lg:row-start-2"><%= t("account_statements.form.files_hint", max_size: AccountStatement::MAX_FILE_SIZE / 1.megabyte) %></p><%= form.collection_select :account_id,@accounts,:id,diff --git a/app/views/accounts/show/_statements.html.erb b/app/views/accounts/show/_statements.html.erbindex 6be92fd4e3..877f203986 100644--- a/app/views/accounts/show/_statements.html.erb+++ b/app/views/accounts/show/_statements.html.erb@@ -42,11 +42,10 @@<%= styled_form_with url: account_statements_path, scope: :account_statement, multipart: true, class: "space-y-3" do |form| %><%= form.hidden_field :account_id, value: account.id %><div>- <%= form.label :files, t("account_statements.form.files_label"), class: "form-field__label" %><%= form.file_field :files,+ label: t("account_statements.form.files_label"),multiple: true,- accept: AccountStatement::ACCEPTED_FILE_EXTENSIONS.join(","),- class: "form-field__input" %>+ accept: AccountStatement::ACCEPTED_FILE_EXTENSIONS.join(",") %><p class="text-xs text-secondary mt-2"><%= t("account_statements.form.files_hint", max_size: AccountStatement::MAX_FILE_SIZE / 1.megabyte) %></p></div><%= form.submit t("account_statements.form.account_upload") %>diff --git a/test/helpers/styled_form_builder_test.rb b/test/helpers/styled_form_builder_test.rbnew file mode 100644index 0000000000..7912c250b8--- /dev/null+++ b/test/helpers/styled_form_builder_test.rb@@ -0,0 +1,24 @@+require "test_helper"++class StyledFormBuilderTest < ActionView::TestCase+ setup do+ @builder = StyledFormBuilder.new(:account_statement, nil, self, {})+ end++ test "a labelled file field sits in a form field with its label" do+ field = Nokogiri::HTML.fragment(@builder.file_field(:files, label: "Statement files")).at(".form-field")++ assert field, "expected the file input wrapped in a .form-field"+ assert_equal "account_statement_files", field.at("label.form-field__label")["for"]+ assert_includes field.at("input[type=file]")["class"].split, "file:bg-container-inset"+ end++ # The dropzones pass `hidden` and draw their own target: no wrapper, and+ # their classes merged in rather than replaced.+ test "an unlabelled file field comes back bare and keeps the caller's classes" do+ fragment = Nokogiri::HTML.fragment(@builder.file_field(:files, class: "hidden"))++ assert_nil fragment.at(".form-field")+ assert_includes fragment.at("input[type=file]")["class"].split, "hidden"+ end+end