Class: Polaris::CheckboxComponent

Inherits:
Component
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/components/polaris/checkbox_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form: nil, attribute: nil, name: nil, label: nil, label_hidden: false, checked: false, disabled: false, multiple: false, help_text: nil, error: nil, value: "1", unchecked_value: "0", wrapper_arguments: {}, input_options: {}, **system_arguments) ⇒ CheckboxComponent

Returns a new instance of CheckboxComponent.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/components/polaris/checkbox_component.rb', line 11

def initialize(
  form: nil,
  attribute: nil,
  name: nil,
  label: nil,
  label_hidden: false,
  checked: false,
  disabled: false,
  multiple: false,
  help_text: nil,
  error: nil,
  value: "1",
  unchecked_value: "0",
  wrapper_arguments: {},
  input_options: {},
  **system_arguments
)
  @form = form
  @attribute = attribute
  @name = name
  @checked = checked
  @disabled = disabled
  @multiple = multiple
  @value = value
  @unchecked_value = unchecked_value

  @system_arguments = system_arguments
  @system_arguments[:tag] = "span"
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    "Polaris-Checkbox",
    "Polaris-Checkbox--labelHidden": label_hidden,
    "Polaris-Checkbox--error": error.present?
  )

  @wrapper_arguments = {
    label: label,
    label_hidden: label_hidden,
    disabled: disabled,
    help_text: help_text,
    error: error
  }.merge(wrapper_arguments)

  @input_options = input_options
  @input_options[:classes] = class_names(
    @input_options[:classes],
    "Polaris-Checkbox__Input",
    "Polaris-Checkbox__Input--indeterminate": indeterminate?
  )
end

Instance Attribute Details

#checked ⇒ Object (readonly)

Returns the value of attribute checked.



7
8
9
# File 'app/components/polaris/checkbox_component.rb', line 7

def checked
  @checked
end

Instance Method Details

#before_render ⇒ Object



70
71
72
# File 'app/components/polaris/checkbox_component.rb', line 70

def before_render
  validate!
end

#checkbox ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/components/polaris/checkbox_component.rb', line 74

def checkbox
  render Polaris::BaseCheckbox.new(
    form: @form,
    attribute: @attribute,
    name: @name,
    checked: @checked,
    disabled: @disabled,
    multiple: @multiple,
    value: @value,
    unchecked_value: @unchecked_value,
    **@input_options
  )
end

#icon_name ⇒ Object



66
67
68
# File 'app/components/polaris/checkbox_component.rb', line 66

def icon_name
  indeterminate? ? "MinusIcon" : "CheckSmallIcon"
end

#indeterminate? ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/components/polaris/checkbox_component.rb', line 62

def indeterminate?
  @checked == :indeterminate
end