|
26 | 26 | from __future__ import annotations
|
27 | 27 |
|
28 | 28 | from typing import Any, ClassVar, Dict, List, Optional, TYPE_CHECKING, Tuple, Type, TypeVar, Union
|
29 |
| -from .enums import try_enum, ComponentType, ButtonStyle |
| 29 | +from .enums import try_enum, ComponentType, ButtonStyle, InputTextStyle |
30 | 30 | from .utils import get_slots, MISSING
|
31 | 31 | from .partial_emoji import PartialEmoji, _EmojiTag
|
32 | 32 |
|
33 | 33 | if TYPE_CHECKING:
|
34 | 34 | from .types.components import (
|
35 | 35 | Component as ComponentPayload,
|
| 36 | + InputText as InputTextComponentPayload, |
36 | 37 | ButtonComponent as ButtonComponentPayload,
|
37 | 38 | SelectMenu as SelectMenuPayload,
|
38 | 39 | SelectOption as SelectOptionPayload,
|
@@ -128,6 +129,82 @@ def to_dict(self) -> ActionRowPayload:
|
128 | 129 | } # type: ignore
|
129 | 130 |
|
130 | 131 |
|
| 132 | +class InputText(Component): |
| 133 | + """Represents an Input Text field from the Discord Bot UI Kit. |
| 134 | + This inherits from :class:`Component`. |
| 135 | + Attributes |
| 136 | + ---------- |
| 137 | + style: :class:`.InputTextStyle` |
| 138 | + The style of the input text field. |
| 139 | + custom_id: Optional[:class:`str`] |
| 140 | + The ID of the input text field that gets received during an interaction. |
| 141 | + label: Optional[:class:`str`] |
| 142 | + The label for the input text field, if any. |
| 143 | + placeholder: Optional[:class:`str`] |
| 144 | + The placeholder text that is shown if nothing is selected, if any. |
| 145 | + min_length: Optional[:class:`int`] |
| 146 | + The minimum number of characters that must be entered |
| 147 | + Defaults to 0 |
| 148 | + max_length: Optional[:class:`int`] |
| 149 | + The maximum number of characters that can be entered |
| 150 | + required: Optional[:class:`bool`] |
| 151 | + Whether the input text field is required or not. Defaults to `True`. |
| 152 | + value: Optional[:class:`str`] |
| 153 | + The value that has been entered in the input text field. |
| 154 | + """ |
| 155 | + |
| 156 | + __slots__: Tuple[str, ...] = ( |
| 157 | + "type", |
| 158 | + "style", |
| 159 | + "custom_id", |
| 160 | + "label", |
| 161 | + "placeholder", |
| 162 | + "min_length", |
| 163 | + "max_length", |
| 164 | + "required", |
| 165 | + "value", |
| 166 | + ) |
| 167 | + |
| 168 | + __repr_info__: ClassVar[Tuple[str, ...]] = __slots__ |
| 169 | + |
| 170 | + def __init__(self, data: InputTextComponentPayload): |
| 171 | + self.type = ComponentType.input_text |
| 172 | + self.style: InputTextStyle = try_enum(InputTextStyle, data["style"]) |
| 173 | + self.custom_id = data["custom_id"] |
| 174 | + self.label: Optional[str] = data.get("label", None) |
| 175 | + self.placeholder: Optional[str] = data.get("placeholder", None) |
| 176 | + self.min_length: Optional[int] = data.get("min_length", None) |
| 177 | + self.max_length: Optional[int] = data.get("max_length", None) |
| 178 | + self.required: bool = data.get("required", True) |
| 179 | + self.value: Optional[str] = data.get("value", None) |
| 180 | + |
| 181 | + def to_dict(self) -> InputTextComponentPayload: |
| 182 | + payload = { |
| 183 | + "type": 4, |
| 184 | + "style": self.style.value, |
| 185 | + "label": self.label, |
| 186 | + } |
| 187 | + if self.custom_id: |
| 188 | + payload["custom_id"] = self.custom_id |
| 189 | + |
| 190 | + if self.placeholder: |
| 191 | + payload["placeholder"] = self.placeholder |
| 192 | + |
| 193 | + if self.min_length: |
| 194 | + payload["min_length"] = self.min_length |
| 195 | + |
| 196 | + if self.max_length: |
| 197 | + payload["max_length"] = self.max_length |
| 198 | + |
| 199 | + if not self.required: |
| 200 | + payload["required"] = self.required |
| 201 | + |
| 202 | + if self.value: |
| 203 | + payload["value"] = self.value |
| 204 | + |
| 205 | + return payload # type: ignore |
| 206 | + |
| 207 | + |
131 | 208 | class Button(Component):
|
132 | 209 | """Represents a button from the Discord Bot UI Kit.
|
133 | 210 |
|
|
0 commit comments