Sha256: 88bb356f4eadf9e8768ca515b522a2f68c576a110a854a2dd6b5383bf6d4cbfb

Contents?: true

Size: 1.88 KB

Versions: 141

Compression:

Stored size: 1.88 KB

Contents

# 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|  %>
    #         <%= 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

    # 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"

      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

      private def linked?
        @link.present?
      end

      def call
        render(Ariadne::BaseComponent.new(tag: :li, classes: @classes, attributes: @attributes)) do
          if linked?
            render(Ariadne::LinkComponent.new(href: @link[:href], classes: @link[:classes], attributes: @link[:attributes])) do
              content
            end
          else
            content
          end
        end
      end
    end
  end
end

Version data entries

141 entries across 141 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.34 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.34-x86_64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.34-x86_64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.34-x64-mingw32 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.34-x64-mingw-ucrt app/components/ariadne/list_component.rb
ariadne_view_components-0.0.34-arm64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.34-aarch64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.33 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.33-x86_64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.33-x86_64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.33-x64-mingw32 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.33-x64-mingw-ucrt app/components/ariadne/list_component.rb
ariadne_view_components-0.0.33-arm64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.33-aarch64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.32 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.32-x86_64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.32-x86_64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.32-x64-mingw32 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.32-x64-mingw-ucrt app/components/ariadne/list_component.rb
ariadne_view_components-0.0.32-arm64-darwin app/components/ariadne/list_component.rb