# frozen_string_literal: true module Ariadne # Input with a dropdown selection class ComboboxComponent < Ariadne::Component DEFAULT_TAG = :div DEFAULT_OPTIONS_WRAPPER_TAG = :div TAG_OPTIONS = [DEFAULT_TAG].freeze DEFAULT_CLASSES = { wrapper: "ariadne-group ariadne-w-fit ariadne-relative", options_wrapper: "group-data-[toggleable-state-value=false]:ariadne-hidden ariadne-absolute ariadne-w-full ariadne-z-10", input: "", } DEFAULT_ATTRIBUTES = { wrapper: { "data-controller": "options toggleable string-match", }, options_wrapper: {}, input: { type: "text", autocomplete: "off", "data-action": "input->string-match#change focusin->toggleable#on", }, } renders_many :options renders_one :icon, Ariadne::HeroiconComponent # @example Default # # <%= render(Ariadne::ComboboxComponent.new) { "Example" } %> # # @param tag [Symbol, String] The rendered tag name. # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> def initialize( tag: DEFAULT_TAG, classes: "", attributes: {}, options_tag: DEFAULT_OPTIONS_WRAPPER_TAG, options_wrapper_classes: "", options_wrapper_attributes: {}, close_on_outside_click: true, close_on_select: false, initially_open: false, single_selection: false, toggleable_options: true, # Do options unselect if clicked a second time input_classes: "", input_attributes: {} ) @tag = check_incoming_tag(DEFAULT_TAG, tag) @classes = merge_class_names(DEFAULT_CLASSES[:wrapper], classes) @attributes = DEFAULT_ATTRIBUTES[:wrapper] .merge({ "data-options-is-multi-value": !single_selection, "data-options-toggleable-value": toggleable_options, "data-toggleable-close-on-outside-click-value": close_on_outside_click, "data-toggleable-state-value": initially_open, "aria-multiselectable": !single_selection, }) .merge(attributes) @input_classes = merge_class_names(DEFAULT_CLASSES[:input], input_classes) @input_attributes = DEFAULT_ATTRIBUTES[:input].merge(input_attributes) @options_wrapper_tag = check_incoming_tag(DEFAULT_OPTIONS_WRAPPER_TAG, options_tag) @options_wrapper_classes = merge_class_names(DEFAULT_CLASSES[:options_wrapper], options_wrapper_classes) @options_wrapper_attributes = DEFAULT_ATTRIBUTES[:options_wrapper] .merge({ "data-action": close_on_select ? "click->toggleable#off" : "" }) .merge(options_wrapper_attributes) end end end