Class: Discorb::TextInput

Inherits:
Component show all
Defined in:
lib/discorb/components/text_input.rb

Overview

Represents a text input component.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Component

#inspect, to_payload

Constructor Details

#initialize(label, custom_id, style, min_length: nil, max_length: nil, required: false, value: nil, placeholder: nil) ⇒ TextInput

Initialize a new text input component.

Parameters:

  • label (String)

    The label of the text input.

  • custom_id (String)

    The custom id of the text input.

  • style (:short, :paragraph)

    The style of the text input.

  • min_length (Integer, nil) (defaults to: nil)

    The minimum length of the text input.

  • max_length (Integer, nil) (defaults to: nil)

    The maximum length of the text input.

  • required (Boolean) (defaults to: false)

    Whether the text input is required.

  • value (String, nil) (defaults to: nil)

    The prefilled value of the text input.

  • placeholder (String, nil) (defaults to: nil)

    The placeholder of the text input.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/discorb/components/text_input.rb', line 40

def initialize(
  label,
  custom_id,
  style,
  min_length: nil,
  max_length: nil,
  required: false,
  value: nil,
  placeholder: nil
)
  @label = label
  @custom_id = custom_id
  @style = style
  @min_length = min_length
  @max_length = max_length
  @required = required
  @value = value
  @placeholder = placeholder
end

Instance Attribute Details

#custom_idString

Returns The custom id of the text input.

Returns:

  • (String)

    The custom id of the text input.



14
15
16
# File 'lib/discorb/components/text_input.rb', line 14

def custom_id
  @custom_id
end

#labelString

Returns The label of the text input.

Returns:

  • (String)

    The label of the text input.



12
13
14
# File 'lib/discorb/components/text_input.rb', line 12

def label
  @label
end

#max_lengthInteger?

Returns The maximum length of the text input.

Returns:

  • (Integer, nil)

    The maximum length of the text input.



20
21
22
# File 'lib/discorb/components/text_input.rb', line 20

def max_length
  @max_length
end

#min_lengthInteger?

Returns The minimum length of the text input.

Returns:

  • (Integer, nil)

    The minimum length of the text input.



18
19
20
# File 'lib/discorb/components/text_input.rb', line 18

def min_length
  @min_length
end

#placeholderString?

Returns The placeholder of the text input.

Returns:

  • (String, nil)

    The placeholder of the text input.



26
27
28
# File 'lib/discorb/components/text_input.rb', line 26

def placeholder
  @placeholder
end

#requiredBoolean

Returns Whether the text input is required.

Returns:

  • (Boolean)

    Whether the text input is required.



22
23
24
# File 'lib/discorb/components/text_input.rb', line 22

def required
  @required
end

#style:short, :paragraph

Returns The style of the text input.

Returns:

  • (:short, :paragraph)

    The style of the text input.



16
17
18
# File 'lib/discorb/components/text_input.rb', line 16

def style
  @style
end

#valueString?

Returns The prefilled value of the text input.

Returns:

  • (String, nil)

    The prefilled value of the text input.



24
25
26
# File 'lib/discorb/components/text_input.rb', line 24

def value
  @value
end

Class Method Details

.from_hash(data) ⇒ Discorb::TextInput

Creates a new text input from a hash.

Parameters:

  • data (Hash)

    The hash to create the text input from.

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/discorb/components/text_input.rb', line 89

def from_hash(data)
  new(
    data[:label],
    data[:custom_id],
    STYLES.key(data[:style]),
    min_length: data[:min_length],
    max_length: data[:max_length],
    required: data[:required],
    value: data[:value],
    placeholder: data[:placeholder]
  )
end

Instance Method Details

#to_hashHash

Converts the select menu to a hash.

Returns:

  • (Hash)

    A hash representation of the text input.

See Also:



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/discorb/components/text_input.rb', line 67

def to_hash
  {
    type: 4,
    label: @label,
    style: STYLES[@style],
    custom_id: @custom_id,
    min_length: @min_length,
    max_length: @max_length,
    required: @required,
    value: @value,
    placeholder: @placeholder
  }
end