|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BaseMessageComponent = require('./BaseMessageComponent'); |
| 4 | +const { RangeError } = require('../errors'); |
| 5 | +const { TextInputStyles, MessageComponentTypes } = require('../util/Constants'); |
| 6 | +const Util = require('../util/Util'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Represents a text input component in a modal |
| 10 | + * @extends {BaseMessageComponent} |
| 11 | + */ |
| 12 | + |
| 13 | +class TextInputComponent extends BaseMessageComponent { |
| 14 | + /** |
| 15 | + * @typedef {BaseMessageComponentOptions} TextInputComponentOptions |
| 16 | + * @property {string} [customId] A unique string to be sent in the interaction when submitted |
| 17 | + * @property {string} [label] The text to be displayed above this text input component |
| 18 | + * @property {number} [maxLength] Maximum length of text that can be entered |
| 19 | + * @property {number} [minLength] Minimum length of text required to be entered |
| 20 | + * @property {string} [placeholder] Custom placeholder text to display when no text is entered |
| 21 | + * @property {boolean} [required] Whether or not this text input component is required |
| 22 | + * @property {TextInputStyleResolvable} [style] The style of this text input component |
| 23 | + * @property {string} [value] Value of this text input component |
| 24 | + */ |
| 25 | + |
| 26 | + /** |
| 27 | + * @param {TextInputComponent|TextInputComponentOptions} [data={}] TextInputComponent to clone or raw data |
| 28 | + */ |
| 29 | + constructor(data = {}) { |
| 30 | + super({ type: 'TEXT_INPUT' }); |
| 31 | + |
| 32 | + this.setup(data); |
| 33 | + } |
| 34 | + |
| 35 | + setup(data) { |
| 36 | + /** |
| 37 | + * A unique string to be sent in the interaction when submitted |
| 38 | + * @type {?string} |
| 39 | + */ |
| 40 | + this.customId = data.custom_id ?? data.customId ?? null; |
| 41 | + |
| 42 | + /** |
| 43 | + * The text to be displayed above this text input component |
| 44 | + * @type {?string} |
| 45 | + */ |
| 46 | + this.label = data.label ?? null; |
| 47 | + |
| 48 | + /** |
| 49 | + * Maximum length of text that can be entered |
| 50 | + * @type {?number} |
| 51 | + */ |
| 52 | + this.maxLength = data.max_length ?? data.maxLength ?? null; |
| 53 | + |
| 54 | + /** |
| 55 | + * Minimum length of text required to be entered |
| 56 | + * @type {?string} |
| 57 | + */ |
| 58 | + this.minLength = data.min_length ?? data.minLength ?? null; |
| 59 | + |
| 60 | + /** |
| 61 | + * Custom placeholder text to display when no text is entered |
| 62 | + * @type {?string} |
| 63 | + */ |
| 64 | + this.placeholder = data.placeholder ?? null; |
| 65 | + |
| 66 | + /** |
| 67 | + * Whether or not this text input component is required |
| 68 | + * @type {?boolean} |
| 69 | + */ |
| 70 | + this.required = data.required ?? false; |
| 71 | + |
| 72 | + /** |
| 73 | + * The style of this text input component |
| 74 | + * @type {?TextInputStyle} |
| 75 | + */ |
| 76 | + this.style = data.style ? TextInputComponent.resolveStyle(data.style) : null; |
| 77 | + |
| 78 | + /** |
| 79 | + * Value of this text input component |
| 80 | + * @type {?string} |
| 81 | + */ |
| 82 | + this.value = data.value ?? null; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Sets the custom id of this text input component |
| 87 | + * @param {string} customId A unique string to be sent in the interaction when submitted |
| 88 | + * @returns {TextInputComponent} |
| 89 | + */ |
| 90 | + setCustomId(customId) { |
| 91 | + this.customId = Util.verifyString(customId, RangeError, 'TEXT_INPUT_CUSTOM_ID'); |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sets the label of this text input component |
| 97 | + * @param {string} label The text to be displayed above this text input component |
| 98 | + * @returns {TextInputComponent} |
| 99 | + */ |
| 100 | + setLabel(label) { |
| 101 | + this.label = Util.verifyString(label, RangeError, 'TEXT_INPUT_LABEL'); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Sets the text input component to be required for modal submission |
| 107 | + * @param {boolean} [required=true] Whether this text input component is required |
| 108 | + * @returns {TextInputComponent} |
| 109 | + */ |
| 110 | + setRequired(required = true) { |
| 111 | + this.required = required; |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets the maximum length of text input required in this text input component |
| 117 | + * @param {number} maxLength Maximum length of text to be required |
| 118 | + * @returns {TextInputComponent} |
| 119 | + */ |
| 120 | + setMaxLength(maxLength) { |
| 121 | + this.maxLength = maxLength; |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Sets the minimum length of text input required in this text input component |
| 127 | + * @param {number} minLength Minimum length of text to be required |
| 128 | + * @returns {TextInputComponent} |
| 129 | + */ |
| 130 | + setMinLength(minLength) { |
| 131 | + this.minLength = minLength; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the placeholder of this text input component |
| 137 | + * @param {string} placeholder Custom placeholder text to display when no text is entered |
| 138 | + * @returns {TextInputComponent} |
| 139 | + */ |
| 140 | + setPlaceholder(placeholder) { |
| 141 | + this.placeholder = Util.verifyString(placeholder, RangeError, 'TEXT_INPUT_PLACEHOLDER'); |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Sets the style of this text input component |
| 147 | + * @param {TextInputStyleResolvable} style The style of this text input component |
| 148 | + * @returns {TextInputComponent} |
| 149 | + */ |
| 150 | + setStyle(style) { |
| 151 | + this.style = TextInputComponent.resolveStyle(style); |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Sets the value of this text input component |
| 157 | + * @param {string} value Value of this text input component |
| 158 | + * @returns {TextInputComponent} |
| 159 | + */ |
| 160 | + setValue(value) { |
| 161 | + this.value = Util.verifyString(value, RangeError, 'TEXT_INPUT_VALUE'); |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Transforms the text input component into a plain object |
| 167 | + * @returns {APITextInput} The raw data of this text input component |
| 168 | + */ |
| 169 | + toJSON() { |
| 170 | + return { |
| 171 | + custom_id: this.customId, |
| 172 | + label: this.label, |
| 173 | + max_length: this.maxLength, |
| 174 | + min_length: this.minLength, |
| 175 | + placeholder: this.placeholder, |
| 176 | + required: this.required, |
| 177 | + style: TextInputStyles[this.style], |
| 178 | + type: MessageComponentTypes[this.type], |
| 179 | + value: this.value, |
| 180 | + }; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Data that can be resolved to a TextInputStyle. This can be |
| 185 | + * * TextInputStyle |
| 186 | + * * number |
| 187 | + * @typedef {number|TextInputStyle} TextInputStyleResolvable |
| 188 | + */ |
| 189 | + |
| 190 | + /** |
| 191 | + * Resolves the style of a text input component |
| 192 | + * @param {TextInputStyleResolvable} style The style to resolve |
| 193 | + * @returns {TextInputStyle} |
| 194 | + * @private |
| 195 | + */ |
| 196 | + static resolveStyle(style) { |
| 197 | + return typeof style === 'string' ? style : TextInputStyles[style]; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +module.exports = TextInputComponent; |
0 commit comments