From 965b34318f0a31606abb5fa657c301bf6b130be6 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 29 Sep 2025 14:27:45 +1000 Subject: [PATCH] Add new component to provide tag autocomplete for variant tag It uses composition and inject the TagListInputComponent as a depency, which should be more flexible that creating a sub class. This new component could potentially be made more generic if needed --- .../variant_tag_list_input_component.rb | 24 +++++++++++++++++++ ...variant_tag_list_input_component.html.haml | 4 ++++ .../admin/products_v3/_variant_row.html.haml | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 app/components/variant_tag_list_input_component.rb create mode 100644 app/components/variant_tag_list_input_component/variant_tag_list_input_component.html.haml diff --git a/app/components/variant_tag_list_input_component.rb b/app/components/variant_tag_list_input_component.rb new file mode 100644 index 0000000000..7297f0b2e4 --- /dev/null +++ b/app/components/variant_tag_list_input_component.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class VariantTagListInputComponent < ViewComponent::Base + def initialize(name:, variant:, tag_list_input_component: TagListInputComponent, + placeholder: I18n.t("components.tag_list_input.default_placeholder"), + aria_label: nil) + @tag_list_input_component = tag_list_input_component + @variant = variant + @name = name + @tags = variant.tag_list + @placeholder = placeholder + @only_one = false + @aria_label = aria_label + end + + attr_reader :tag_list_input_component, :variant, :name, :tags, :placeholder, :only_one, + :aria_label + + private + + def autocomplete_url + "/admin/tag_rules/variant_tag_rules?enterprise_id=#{variant.supplier_id}" + end +end diff --git a/app/components/variant_tag_list_input_component/variant_tag_list_input_component.html.haml b/app/components/variant_tag_list_input_component/variant_tag_list_input_component.html.haml new file mode 100644 index 0000000000..02797dcdad --- /dev/null +++ b/app/components/variant_tag_list_input_component/variant_tag_list_input_component.html.haml @@ -0,0 +1,4 @@ +%div{ "data-controller": "autocomplete", "data-autocomplete-url-value": autocomplete_url } + = render tag_list_input_component.new(name: , tags: , placeholder: , aria_label: , only_one: , input_field_data_options: {"data-autocomplete-target": "input"}) do + .autocomplete + %ul.suggestion-list{ "data-autocomplete-target": "results" } diff --git a/app/views/admin/products_v3/_variant_row.html.haml b/app/views/admin/products_v3/_variant_row.html.haml index 4235c69a11..089061b72e 100644 --- a/app/views/admin/products_v3/_variant_row.html.haml +++ b/app/views/admin/products_v3/_variant_row.html.haml @@ -80,7 +80,7 @@ = error_message_on variant, :tax_category - if feature?(:variant_tag, spree_current_user) %td.col-tags.field.naked_inputs - = render TagListInputComponent.new(name: f.field_name(:tag_list), tags: variant.tag_list, placeholder: t('.add_a_tag'), aria_label: t('admin.products_page.columns.tags')) + = render VariantTagListInputComponent.new(name: f.field_name(:tag_list), variant:, placeholder: t('.add_a_tag'), aria_label: t('admin.products_page.columns.tags')) %td.col-inherits_properties.align-left -# empty %td.align-right