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.



43
44
45
46
47
48
49
50
51
52
# File 'lib/discorb/components/text_input.rb', line 43

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_id -> String

Returns The custom id of the text input.

Returns:

  • (String)

    The custom id of the text input.



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

def custom_id
  @custom_id
end

#label -> String

Returns The label of the text input.

Returns:

  • (String)

    The label of the text input.



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

def label
  @label
end

#max_length -> Integer?

Returns The maximum length of the text input.

Returns:

  • (Integer, nil)

    The maximum length of the text input.



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

def max_length
  @max_length
end

#min_length -> Integer?

Returns The minimum length of the text input.

Returns:

  • (Integer, nil)

    The minimum length of the text input.



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

def min_length
  @min_length
end

#placeholder -> String?

Returns The placeholder of the text input.

Returns:

  • (String, nil)

    The placeholder of the text input.



29
30
31
# File 'lib/discorb/components/text_input.rb', line 29

def placeholder
  @placeholder
end

#required -> Boolean

Returns Whether the text input is required.

Returns:

  • (Boolean)

    Whether the text input is required.



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

def required
  @required
end

#style -> :short, :paragraph

Returns The style of the text input.

Returns:

  • (:short, :paragraph)

    The style of the text input.



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

def style
  @style
end

#value -> String?

Returns The prefilled value of the text input.

Returns:

  • (String, nil)

    The prefilled value of the text input.



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

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:



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/discorb/components/text_input.rb', line 82

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_hash -> Hash

Converts the select menu to a hash.

Returns:

  • (Hash)

    A hash representation of the text input.

See Also:



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/discorb/components/text_input.rb', line 60

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