# 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-bg-white 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-comment-component-target"] ||= "tab" attributes[:"data-action"] ||= "click->comment-component#toggleTab" attributes[:"data-public"] = true @tab_idx += 1 render(Ariadne::TabComponent.new(selected: selected, classes: classes, attributes: attributes)) { text } } renders_one :internal_tab, lambda { |selected: false, text:, classes: "", attributes: {}| attributes[:"data-comment-component-target"] ||= "tab" attributes[:"data-action"] ||= "click->comment-component#toggleTab" @tab_idx += 1 render(Ariadne::TabComponent.new(selected: selected, classes: classes, attributes: attributes)) { text } } renders_one :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")) { "Example" } %> # # @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:, 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 @attributes[:"data-controller"] = "comment-component" attributes[:"data-comment-component-target"] = "commentComponent" end end end