Sha256: 7ffcde17df594efd270e0cdfcc76e1bb687a4df37f3684b2eb9a835ff358c155

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Coco
  class ButtonTo < Coco::Component
    include Concerns::WrapsComponent
    include Concerns::AcceptsOptions

    accepts_option :fit, from: [:auto, :full]

    wraps_component :button do |args|
      Coco::Button.new(
        type: :submit,
        fit: get_option_value(:fit) || :auto,
        **args
      )
    end

    %i[confirmation].each do |slot_name|
      renders_one slot_name, ->(**kwargs, &block) do
        button.send(:"with_#{slot_name}", **kwargs, &block)
      end
    end

    attr_reader :method

    def initialize(form: nil, params: nil, method: :post, action: nil, href: nil, turbo: false, turbo_frame: nil, **kwargs)
      @form_args = form.to_h
      @action = action || href
      @method = method
      @params = params.to_h
      @turbo = turbo
      @turbo_frame = turbo_frame
    end

    def params
      to_form_params(@params)
    end

    def form_args
      {
        url: action,
        method: method,
        data: {
          turbo: turbo_data_attr_value,
          turbo_frame: @turbo_frame
        }
      }.deep_merge(@form_args)
    end

    def turbo_data_attr_value
      if @turbo == false
        "false"
      elsif @turbo == true
        "true"
      end
    end

    def action
      @action.is_a?(String) ? @action : helpers.url_for(@action)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
coveragebook_components-0.18.0 app/components/coco/buttons/button_to/button_to.rb
coveragebook_components-0.17.7 app/components/coco/buttons/button_to/button_to.rb
coveragebook_components-0.17.6 app/components/coco/buttons/button_to/button_to.rb