# frozen_string_literal: true module Ariadne # `List` is used to show a list of items in a vertical format. class ListComponent < Ariadne::Component DEFAULT_TAG = :ul DEFAULT_UL_CLASSES = "ariadne-divide-y ariadne-divide-gray-300" renders_many :items, "ListItem" # @example Basic # <% numbers = [1, 2, 3] %> # <%= render(Ariadne::ListComponent.new) do |list| %> # <% numbers.each do |number| %> # <%= list.item do |item| %> # <%= item.entry { number } %> # <% end %> # <% end %> # <% end %> # # # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> def initialize(classes: "", attributes: {}) @tag = DEFAULT_TAG @classes = class_names(DEFAULT_UL_CLASSES, classes) @attributes = attributes end def render? items.any? end # This component is part of `ListComponent` and should not be # used as a standalone component. class ListItem < Ariadne::Component DEFAULT_ITEM_CLASSES = "ariadne-relative ariadne-p-1.5 focus:ariadne-ring-2 focus:ariadne-ring-offset-2 focus:ariadne-ring-purple-500 hover:ariadne-bg-button-hover-color" DEFAULT_ENTRY_CLASSES = "hover:ariadne-text-gray-500" renders_one :entry, lambda { |classes: "", attributes: {}, &block| view_context.capture do actual_classes = class_names(DEFAULT_ENTRY_CLASSES, classes) render(Ariadne::BaseComponent.new(tag: :div, classes: actual_classes, attributes: attributes)) { block&.call } end } attr_reader :link, :classes, :attributes def initialize(link: {}, classes: "", attributes: {}) @link = link @classes = class_names(DEFAULT_ITEM_CLASSES, classes) @attributes = attributes end def selected? @selected end def linked? @link.present? end def call Ariadne::BaseComponent.new(tag: :div, classes: @classes, attributes: @attributes) end end end end