|
| 1 | +import type { APICheckboxGroupComponent, APICheckboxGroupOption } from 'discord-api-types/v10'; |
| 2 | +import { ComponentType } from 'discord-api-types/v10'; |
| 3 | +import type { RestOrArray } from '../../util/normalizeArray'; |
| 4 | +import { normalizeArray } from '../../util/normalizeArray'; |
| 5 | +import { ComponentBuilder } from '../Component'; |
| 6 | +import { |
| 7 | + checkboxGroupOptionPredicate, |
| 8 | + checkboxGroupOptionsLengthValidator, |
| 9 | + checkboxGroupPredicate, |
| 10 | +} from './Assertions'; |
| 11 | +import { CheckboxGroupOptionBuilder } from './CheckboxGroupOption'; |
| 12 | + |
| 13 | +/** |
| 14 | + * A builder that creates API-compatible JSON data for checkbox groups. |
| 15 | + */ |
| 16 | +export class CheckboxGroupBuilder extends ComponentBuilder<APICheckboxGroupComponent> { |
| 17 | + /** |
| 18 | + * The options within this checkbox group. |
| 19 | + */ |
| 20 | + public readonly options: CheckboxGroupOptionBuilder[]; |
| 21 | + |
| 22 | + /** |
| 23 | + * Creates a new checkbox group from API data. |
| 24 | + * |
| 25 | + * @param data - The API data to create this checkbox group with |
| 26 | + * @example |
| 27 | + * Creating a checkbox group from an API data object: |
| 28 | + * ```ts |
| 29 | + * const checkboxGroup = new CheckboxGroupBuilder({ |
| 30 | + * custom_id: 'select_options', |
| 31 | + * options: [ |
| 32 | + * { label: 'Option 1', value: 'option_1' }, |
| 33 | + * { label: 'Option 2', value: 'option_2' }, |
| 34 | + * ], |
| 35 | + * }); |
| 36 | + * ``` |
| 37 | + * @example |
| 38 | + * Creating a checkbox group using setters and API data: |
| 39 | + * ```ts |
| 40 | + * const checkboxGroup = new CheckboxGroupBuilder() |
| 41 | + * .setCustomId('choose_items') |
| 42 | + * .setOptions([ |
| 43 | + * { label: 'Item A', value: 'item_a' }, |
| 44 | + * { label: 'Item B', value: 'item_b' }, |
| 45 | + * ]) |
| 46 | + * .setMinValues(1) |
| 47 | + * .setMaxValues(2); |
| 48 | + * ``` |
| 49 | + */ |
| 50 | + public constructor(data?: Partial<APICheckboxGroupComponent>) { |
| 51 | + const { options, ...initData } = data ?? {}; |
| 52 | + super({ ...initData, type: ComponentType.CheckboxGroup }); |
| 53 | + this.options = options?.map((option: APICheckboxGroupOption) => new CheckboxGroupOptionBuilder(option)) ?? []; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets the custom ID of this checkbox group. |
| 58 | + * |
| 59 | + * @param customId - The custom ID to use |
| 60 | + */ |
| 61 | + public setCustomId(customId: string) { |
| 62 | + this.data.custom_id = customId; |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Adds options to this checkbox group. |
| 68 | + * |
| 69 | + * @param options - The options to add |
| 70 | + */ |
| 71 | + public addOptions(...options: RestOrArray<APICheckboxGroupOption | CheckboxGroupOptionBuilder>) { |
| 72 | + const normalizedOptions = normalizeArray(options); |
| 73 | + |
| 74 | + checkboxGroupOptionsLengthValidator.parse(this.options.length + normalizedOptions.length); |
| 75 | + this.options.push( |
| 76 | + ...normalizedOptions.map((normalizedOption) => { |
| 77 | + // I do this because TS' duck typing causes issues, |
| 78 | + // if I put in a RadioGroupOption, TS lets it pass but |
| 79 | + // it fails to convert to a checkbox group option at runtime |
| 80 | + const json = 'toJSON' in normalizedOption ? normalizedOption.toJSON() : normalizedOption; |
| 81 | + const option = new CheckboxGroupOptionBuilder(json); |
| 82 | + checkboxGroupOptionPredicate.parse(option.toJSON()); |
| 83 | + return option; |
| 84 | + }), |
| 85 | + ); |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Sets the options for this checkbox group. |
| 91 | + * |
| 92 | + * @param options - The options to use |
| 93 | + */ |
| 94 | + public setOptions(options: RestOrArray<APICheckboxGroupOption | CheckboxGroupOptionBuilder>) { |
| 95 | + return this.spliceOptions(0, this.options.length, ...options); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Removes, replaces, or inserts options for this checkbox group. |
| 100 | + * |
| 101 | + * @remarks |
| 102 | + * This method behaves similarly |
| 103 | + * to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice | Array.prototype.splice()}. |
| 104 | + * It's useful for modifying and adjusting the order of existing options. |
| 105 | + * @param index - The index to start at |
| 106 | + * @param deleteCount - The number of options to remove |
| 107 | + * @param options - The replacing option objects or builders |
| 108 | + */ |
| 109 | + public spliceOptions( |
| 110 | + index: number, |
| 111 | + deleteCount: number, |
| 112 | + ...options: RestOrArray<APICheckboxGroupOption | CheckboxGroupOptionBuilder> |
| 113 | + ) { |
| 114 | + const normalizedOptions = normalizeArray(options); |
| 115 | + |
| 116 | + const clone = [...this.options]; |
| 117 | + |
| 118 | + clone.splice( |
| 119 | + index, |
| 120 | + deleteCount, |
| 121 | + ...normalizedOptions.map((normalizedOption) => { |
| 122 | + // I do this because TS' duck typing causes issues, |
| 123 | + // if I put in a RadioGroupOption, TS lets it pass but |
| 124 | + // it fails to convert to a checkbox group option at runtime |
| 125 | + const json = 'toJSON' in normalizedOption ? normalizedOption.toJSON() : normalizedOption; |
| 126 | + const option = new CheckboxGroupOptionBuilder(json); |
| 127 | + checkboxGroupOptionPredicate.parse(option.toJSON()); |
| 128 | + return option; |
| 129 | + }), |
| 130 | + ); |
| 131 | + |
| 132 | + checkboxGroupOptionsLengthValidator.parse(clone.length); |
| 133 | + this.options.splice(0, this.options.length, ...clone); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sets the minimum number of options that must be selected. |
| 139 | + * |
| 140 | + * @param minValues - The minimum number of options that must be selected |
| 141 | + */ |
| 142 | + public setMinValues(minValues: number) { |
| 143 | + this.data.min_values = minValues; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the maximum number of options that can be selected. |
| 149 | + * |
| 150 | + * @param maxValues - The maximum number of options that can be selected |
| 151 | + */ |
| 152 | + public setMaxValues(maxValues: number) { |
| 153 | + this.data.max_values = maxValues; |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets whether selecting options is required. |
| 159 | + * |
| 160 | + * @param required - Whether selecting options is required |
| 161 | + */ |
| 162 | + public setRequired(required: boolean) { |
| 163 | + this.data.required = required; |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * {@inheritDoc ComponentBuilder.toJSON} |
| 169 | + */ |
| 170 | + public override toJSON(): APICheckboxGroupComponent { |
| 171 | + const data = { |
| 172 | + ...this.data, |
| 173 | + options: this.options.map((option) => option.toJSON()), |
| 174 | + }; |
| 175 | + |
| 176 | + checkboxGroupPredicate.parse(data); |
| 177 | + |
| 178 | + return data as APICheckboxGroupComponent; |
| 179 | + } |
| 180 | +} |
0 commit comments