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
This commit is contained in:
Gaetan Craig-Riou
2025-09-29 14:27:45 +10:00
parent 3bb9eb9765
commit 965b34318f
3 changed files with 29 additions and 1 deletions

View File

@@ -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

View File

@@ -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" }

View File

@@ -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