|
| 1 | +import { ActionRowBuilder, TextInputBuilder, TextInputStyle } from 'discord.js'; |
| 2 | +import { MaybeArray } from '../common/types'; |
| 3 | +import { CommandKitElement } from '../common/element'; |
| 4 | +import { |
| 5 | + CommandKitModalBuilderInteractionCollectorDispatchContextData, |
| 6 | + ModalKit, |
| 7 | + OnModalKitEnd, |
| 8 | + OnModalKitSubmit, |
| 9 | +} from './ModalKit'; |
| 10 | + |
| 11 | +export interface ModalProps { |
| 12 | + customId?: string; |
| 13 | + title: string; |
| 14 | + children?: MaybeArray<TextInputBuilder | ActionRowBuilder>; |
| 15 | + onSubmit?: OnModalKitSubmit; |
| 16 | + onEnd?: OnModalKitEnd; |
| 17 | + options?: CommandKitModalBuilderInteractionCollectorDispatchContextData; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * The modal component. |
| 22 | + * @param props The modal properties. |
| 23 | + * @returns The commandkit element. |
| 24 | + * @example <Modal title="My Modal" onSubmit={onSubmit}>...</Modal> |
| 25 | + */ |
| 26 | +export function Modal(props: ModalProps): CommandKitElement<'modal'> { |
| 27 | + const modal = new ModalKit(); |
| 28 | + |
| 29 | + if (props.title) { |
| 30 | + modal.setTitle(props.title); |
| 31 | + } |
| 32 | + |
| 33 | + if (props.onSubmit) { |
| 34 | + props.customId ??= `modalkit::${crypto.randomUUID()}`; |
| 35 | + } |
| 36 | + |
| 37 | + if (props.customId) { |
| 38 | + modal.setCustomId(props.customId); |
| 39 | + } |
| 40 | + |
| 41 | + if (props.onSubmit) { |
| 42 | + modal.onSubmit(props.onSubmit, props.options); |
| 43 | + } |
| 44 | + |
| 45 | + if (props.children) { |
| 46 | + const childs = ( |
| 47 | + Array.isArray(props.children) ? props.children : [props.children] |
| 48 | + ) |
| 49 | + .map((c) => { |
| 50 | + if (c instanceof ActionRowBuilder) return c; |
| 51 | + if (c instanceof TextInputBuilder) |
| 52 | + return new ActionRowBuilder().addComponents(c); |
| 53 | + }) |
| 54 | + .filter((c): c is ActionRowBuilder<TextInputBuilder> => c != null); |
| 55 | + |
| 56 | + modal.addComponents(childs); |
| 57 | + } |
| 58 | + |
| 59 | + if (props.onEnd) { |
| 60 | + modal.onEnd(props.onEnd); |
| 61 | + } |
| 62 | + |
| 63 | + return modal; |
| 64 | +} |
| 65 | + |
| 66 | +export interface TextInputProps { |
| 67 | + customId: string; |
| 68 | + label: string; |
| 69 | + placeholder?: string; |
| 70 | + maxLength?: number; |
| 71 | + minLength?: number; |
| 72 | + value?: string; |
| 73 | + required?: boolean; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * The text input component. |
| 78 | + * @param props The text input properties. |
| 79 | + * @returns The commandkit element. |
| 80 | + * @example <TextInput customId="input" label="Input" style={TextInputStyle.Short} /> |
| 81 | + */ |
| 82 | +export function TextInput( |
| 83 | + props: TextInputProps & { style: TextInputStyle }, |
| 84 | +): CommandKitElement<'text-input'> { |
| 85 | + const input = new TextInputBuilder().setStyle(props.style); |
| 86 | + |
| 87 | + if (props.customId) { |
| 88 | + input.setCustomId(props.customId); |
| 89 | + } |
| 90 | + |
| 91 | + if (props.label) { |
| 92 | + input.setLabel(props.label); |
| 93 | + } |
| 94 | + |
| 95 | + if (props.placeholder) { |
| 96 | + input.setPlaceholder(props.placeholder); |
| 97 | + } |
| 98 | + |
| 99 | + if (props.maxLength) { |
| 100 | + input.setMaxLength(props.maxLength); |
| 101 | + } |
| 102 | + |
| 103 | + if (props.minLength) { |
| 104 | + input.setMinLength(props.minLength); |
| 105 | + } |
| 106 | + |
| 107 | + if (props.value) { |
| 108 | + input.setValue(props.value); |
| 109 | + } |
| 110 | + |
| 111 | + if (props.required) { |
| 112 | + input.setRequired(props.required); |
| 113 | + } |
| 114 | + |
| 115 | + return input; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * The short text input component. |
| 120 | + * @param props The text input properties. |
| 121 | + * @returns The commandkit element. |
| 122 | + * @example <ShortInput customId="input" label="Input" /> |
| 123 | + */ |
| 124 | +export function ShortInput( |
| 125 | + props: TextInputProps, |
| 126 | +): CommandKitElement<'text-input'> { |
| 127 | + return TextInput({ ...props, style: TextInputStyle.Short }); |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * The paragraph text input component. |
| 132 | + * @param props The text input properties. |
| 133 | + * @returns The commandkit element. |
| 134 | + * @example <ParagraphInput customId="input" label="Input" /> |
| 135 | + */ |
| 136 | +export function ParagraphInput( |
| 137 | + props: TextInputProps, |
| 138 | +): CommandKitElement<'text-input'> { |
| 139 | + return TextInput({ ...props, style: TextInputStyle.Paragraph }); |
| 140 | +} |
0 commit comments