# typed: false # frozen_string_literal: true module Ariadne module Form module Select class Component < Ariadne::Form::BaseInputComponent option :name option :label, default: proc { false } option :changed_action, optional: true Choice = Data.define(:name, :value, :selected) attr_reader :choices accepts_html_attributes do |html_attrs| html_attrs[:data] ||= {} if changed_action html_attrs[:data][:action] = changed_action end end def initialize(**) super @choices = [] end def with_choice(name:, value:, selected: false) @choices << Choice.new(name:, value:, selected:) end def selected_choice @selected_choice ||= @choices.find(&:selected) end def selected?(choice) choice == selected_choice end def select_styles Ariadne::ViewComponents.tailwind_merger.merge([style(:select), html_attrs[:class]].join(" ")) end # def before_render # raise # @selected_choice = @choices.select { |choice| choice.selected } # raise ArgumentError, "Can only have one selected choice" if @selected_choice.length > 1 # @selected_choice = @selected_choice.first if @selected_choice.blank? # end style do base do end end style :select do base do [ "ariadne-rounded-md", "ariadne-block", "ariadne-w-full", "ariadne-border", "ariadne-border-input", "ariadne-bg-panel", "dark:ariadne-bg-panel-dark", "ariadne-text-sm", "ariadne-ring-offset-background", "placeholder:ariadne-text-muted-foreground", "focus:ariadne-outline-none", "focus:ariadne-ring-2", "focus:ariadne-ring-ring", "focus:ariadne-ring-offset-2", "disabled:ariadne-cursor-not-allowed", "disabled:ariadne-opacity-50", ] end end style :label do base do [ "ariadne-text-sm", "ariadne-font-medium", "ariadne-leading-none", "peer-disabled:ariadne-cursor-not-allowed", "peer-disabled:ariadne-opacity-70", "ariadne-block", "ariadne-mb-2", "ariadne-text-sm", "ariadne-font-medium", "ariadne-text-gray-900", "ariadne-dark:text-white", ] end end style :wrapper do base do [ "ariadne-group", "ariadne-relative", "ariadne-block", "before:ariadneabsolute", "before:ariadne-inset-px", "before:ariadne-rounded-md", "before:ariadne-bg-white", "before:ariadne-shadow", "dark:before:ariadne-hidden", "after:ariadne-pointer-events-none", "after:ariadne-absolute", "after:ariadne-inset-0", "after:ariadne-rounded-md", "after:ariadne-ring-inset", "after:ariadne-ring-transparent", "after:focus:ariadne-ring-2", "after:focus:ariadne-ring-blue-500", "disabled:ariadne-opacity-50", "before:disabled:ariadne-bg-zinc-950/5", "before:disabled:ariadne-shadow-none", ] end end end end end end