module Coco module Concerns module ActsAsButtonGroup extend ActiveSupport::Concern BUTTON_TYPES = { menu: "Coco::MenuButton", color_picker: "Coco::ColorPickerButton", layout_picker: "Coco::LayoutPickerButton", image_picker: "Coco::ImagePickerButton" } included do renders_many :items, types: { noop: -> {}, divider: ->(**kwargs) { tag.div(class: "divider") }, button: ->(*args, **kwargs, &block) { coco_button(*args, **button_kwargs(kwargs, :button), &block) }, menu_button: ->(*args, **kwargs, &block) { instantiate_button(:menu, *args, **kwargs, &block) }, color_picker_button: ->(*args, **kwargs, &block) { instantiate_button(:color_picker, *args, **kwargs, &block) }, image_picker_button: ->(*args, **kwargs, &block) { instantiate_button(:image_picker, *args, **kwargs, &block) }, layout_picker_button: ->(*args, **kwargs, &block) { instantiate_button(:layout_picker, *args, **kwargs, &block) } } end def instantiate_button(type, *args, **kwargs, &block) href, content = if block [args.first, nil] else (args.size == 1) ? [nil, args.first] : args[0..2].reverse! end component = BUTTON_TYPES[type].constantize.new(href: href, **button_kwargs(kwargs, type)) component.with_content(content) if !block && content.present? component end def init_button_group with_item_noop end def with_divider(...) with_item_divider(...) end def with_button(...) with_item_button(...) end def with_menu_button(...) with_item_menu_button(...) end def with_color_picker_button(...) with_item_color_picker_button(...) end def with_image_picker_button(...) with_item_image_picker_button(...) end def with_layout_picker_button(...) with_item_layout_picker_button(...) end private def button_kwargs(kwargs, type = nil) { size: args[:size], resize: args[:resize], **kwargs } end end end end