Class: Tramway::Form::CheckboxComponent

Inherits:
TailwindComponent show all
Defined in:
app/components/tramway/form/checkbox_component.rb

Overview

Tailwind-styled checkbox field

Constant Summary

Constants inherited from TailwindComponent

TailwindComponent::SIZE_CLASSES

Constants included from Helpers::ViewsHelper

Helpers::ViewsHelper::FORM_SIZES

Instance Method Summary collapse

Methods included from Helpers::ViewsHelper

#tramway_back_button, #tramway_badge, #tramway_button, #tramway_cell, #tramway_chat, #tramway_container, #tramway_flash, #tramway_form_for, #tramway_header, #tramway_main_container, #tramway_row, #tramway_table, #tramway_title

Methods included from Helpers::ComponentHelper

#component

Methods included from Helpers::DecorateHelper

#tramway_decorate

Instance Method Details

#checkbox_button_classesObject



7
8
9
10
11
12
13
14
# File 'app/components/tramway/form/checkbox_component.rb', line 7

def checkbox_button_classes
  'peer h-4 w-4 shrink-0 rounded-sm border border-zinc-800 bg-zinc-950 text-zinc-50 ' \
    'ring-offset-zinc-950 ' \
    'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-zinc-300 ' \
    'focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 ' \
    'data-[state=checked]:border-zinc-50 data-[state=checked]:bg-zinc-50 ' \
    'data-[state=checked]:text-zinc-950'
end

#checkbox_indicator_classesObject



16
17
18
# File 'app/components/tramway/form/checkbox_component.rb', line 16

def checkbox_indicator_classes
  'flex items-center justify-center text-current hidden'
end

#checked?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/components/tramway/form/checkbox_component.rb', line 20

def checked?
  !!ActiveModel::Type::Boolean.new.cast(options.fetch(:checked, value))
end

#hidden_checkbox_idObject



34
35
36
# File 'app/components/tramway/form/checkbox_component.rb', line 34

def hidden_checkbox_id
  "#{@for}_input"
end

#hidden_checkbox_optionsObject



24
25
26
27
28
29
30
31
32
# File 'app/components/tramway/form/checkbox_component.rb', line 24

def hidden_checkbox_options
  options.merge(
    id: hidden_checkbox_id,
    class: 'hidden',
    data: options.fetch(:data, {}).merge('ui--checkbox-target' => 'input'),
    tabindex: -1,
    aria: { hidden: true }
  )
end

#label_classesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/components/tramway/form/checkbox_component.rb', line 38

def label_classes
  default_classes = 'cursor-pointer mb-0 leading-6'

  case size
  when :small
    default_classes += ' text-sm'
  when :medium
    default_classes += ' text-base'
  when :large
    default_classes += ' text-lg'
  end

  default_classes
end