# frozen_string_literal: true module Ariadne # Defines a submittable form for adding comments to a conversation. # # # @accessibility This component requires you to pass in a `sr_label` # attribute, which will be used to label the tabs for screen readers. class CommentComponent < Ariadne::Component DEFAULT_TAG = :div TAG_OPTIONS = [DEFAULT_TAG].freeze DEFAULT_CLASSES = "ariadne-border-gray-300 ariadne-border ariadne-shadow ariadne-py-5 ariadne-px-5 ariadne-rounded-md " renders_one :public_tab, lambda { |selected: false, text:, classes: "", attributes: {}| attributes[:"data-public"] = true @tab_idx += 1 id = attributes.fetch(:id, "public-tab-#{@tab_idx}") @public_tab_text = text Ariadne::TabComponent.new(selected: selected, classes: classes, id: id, with_panel: true, attributes: attributes) } renders_one :public_submit, lambda { |classes: "", attributes: {}| Ariadne::ButtonComponent.new(type: Ariadne::BaseButton::TYPE_SUBMIT, scheme: Ariadne::ButtonComponent::DEFAULT_SCHEME, classes: classes, attributes: attributes) } renders_one :internal_tab, lambda { |selected: false, text:, classes: "", attributes: {}| attributes[:"data-public"] = false @tab_idx += 1 id = attributes.fetch(:id, "internal-tab-#{@tab_idx}") @internal_tab_text = text Ariadne::TabComponent.new(selected: selected, classes: classes, id: id, with_panel: true, attributes: attributes) } renders_one :internal_submit, lambda { |classes: "", attributes: {}| Ariadne::ButtonComponent.new(type: Ariadne::BaseButton::TYPE_SUBMIT, scheme: Ariadne::ButtonComponent::DEFAULT_SCHEME, classes: classes, attributes: attributes) } # @example Default # # <%= render(Ariadne::CommentComponent.new(url: "/messages", method: :post, sr_label: "Select delivery time")) do |comment| %> # <% comment.public_tab(selected: true, text: "Reply to sender") %> # <% comment.public_submit { "Send" } %> # <% comment.internal_tab(text: "Internal comment") %> # <% comment.internal_submit { "Send" } %> # <% end %> # # @param url [String] The URL to take action against. # @param method [String] The method to use when submitting the form. # @param sr_label [String] A label to introduce these tabs for screen readers. # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> def initialize(url:, method: :post, sr_label:, classes: "", attributes: {}) @tag = DEFAULT_TAG @classes = class_names( DEFAULT_CLASSES, classes, ) @url = url @method = method @sr_label = sr_label @tab_idx = -1 @attributes = attributes end end end