Class: LightningUiKit::TextareaComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Errors, Labelable
Defined in:
app/components/lightning_ui_kit/textarea_component.rb

Instance Method Summary collapse

Methods included from Labelable

#effective_label, #render_label?

Methods included from Errors

#error_messages, #has_errors?, #infer_errors_from_association

Methods inherited from BaseComponent

#merge_classes

Methods included from HeroiconHelper

#heroicon

Constructor Details

#initialize(name:, value: nil, autofocus: false, label: nil, form: nil, type: :text, error: nil, description: nil, disabled: false, multiple: false, rows: 3, cols: nil, **options) ⇒ TextareaComponent

Returns a new instance of TextareaComponent.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 5

def initialize(name:, value: nil, autofocus: false, label: nil, form: nil, type: :text, error: nil, description: nil, disabled: false, multiple: false, rows: 3, cols: nil, **options)
  @name = name
  @value = value
  @disabled = disabled
  @autofocus = autofocus
  @rows = rows
  @multiple = multiple
  @cols = cols
  @error = error
  @label = label
  @form = form
  @type = type
  @description = description
  @options = options
end

Instance Method Details

#control_classesObject



87
88
89
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 87

def control_classes
  "lui:relative lui:block lui:w-full lui:before:absolute lui:before:inset-px lui:before:rounded-[7px] lui:before:bg-surface-input lui:before:shadow-sm lui:after:pointer-events-none lui:after:absolute lui:after:inset-0 lui:after:rounded-lg lui:after:ring-transparent lui:after:ring-inset lui:sm:focus-within:after:ring-2 lui:sm:focus-within:after:ring-focus lui:has-data-disabled:opacity-50 lui:has-data-disabled:before:bg-surface-hover lui:has-data-disabled:before:shadow-none lui:has-data-invalid:before:shadow-destructive-border/10"
end

#dataObject



21
22
23
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 21

def data
  {controller: "lui-field"}.merge(@options[:data] || {})
end

#description_dataObject



37
38
39
40
41
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 37

def description_data
  {slot: "description"}.merge(@options[:description_data] || {}).dup.tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#error_dataObject



43
44
45
46
47
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 43

def error_data
  {slot: "error"}.merge(@options[:error_data] || {}).dup.tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#input_dataObject



25
26
27
28
29
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 25

def input_data
  {lui_field_target: "field"}.merge(@options[:input_data] || {}).dup.tap do |data|
    data[:invalid] = "true" if has_errors?
  end
end

#label_dataObject



31
32
33
34
35
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 31

def label_data
  {slot: "label"}.merge(@options[:label_data] || {}).dup.tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#label_html_optionsObject



49
50
51
52
53
54
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 49

def label_html_options
  {
    class: "lui:text-base/6 lui:text-foreground lui:select-none lui:data-disabled:opacity-50 lui:sm:text-sm/6",
    data: label_data
  }
end

#render_labelObject



56
57
58
59
60
61
62
63
64
65
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 56

def render_label
  return unless render_label?

  label_text = effective_label
  if @form
    @form.label(@name, label_text, **label_html_options)
  else
    helpers.label_tag(@name, label_text, **label_html_options)
  end
end

#render_textareaObject



79
80
81
82
83
84
85
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 79

def render_textarea
  if @form
    @form.text_area(@name, **textarea_html_options)
  else
    helpers.text_area_tag(@name, @value, **textarea_html_options)
  end
end

#textarea_html_optionsObject



67
68
69
70
71
72
73
74
75
76
77
# File 'app/components/lightning_ui_kit/textarea_component.rb', line 67

def textarea_html_options
  {
    rows: @rows,
    cols: @cols,
    multiple: @multiple,
    data: input_data,
    class: "lui:relative lui:block lui:w-full lui:appearance-none lui:rounded-lg lui:px-[calc(--spacing(3.5)-1px)] lui:py-[calc(--spacing(2.5)-1px)] lui:sm:px-[calc(--spacing(3)-1px)] lui:sm:py-[calc(--spacing(1.5)-1px)] lui:text-base/6 lui:text-foreground lui:placeholder:text-foreground-muted lui:sm:text-sm/6 lui:border lui:border-border lui:data-[hover]:border-border-hover lui:bg-transparent lui:focus:outline-hidden lui:data-invalid:border-destructive-border lui:data-invalid:data-[hover]:border-destructive-border lui:data-disabled:border-border-hover",
    disabled: @disabled,
    autofocus: @autofocus
  }
end