mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-01 00:20:07 +00:00
* feat(builders): add checkbox, checkboxgroup, and radiogroup builders * Update packages/builders/src/components/checkbox/Assertions.ts fix incorrect wording about default option count in radio groups Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com> * fix(builders): remove length validators from add/splice options * chore: remove directives * fix(builders): documentation fixes * fix(builders): return Result.err instead of throw in validators --------- Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com> Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import type { APICheckboxComponent } from 'discord-api-types/v10';
|
|
import { ComponentType } from 'discord-api-types/v10';
|
|
import { ComponentBuilder } from '../Component';
|
|
import { checkboxPredicate } from './Assertions';
|
|
|
|
/**
|
|
* A builder that creates API-compatible JSON data for checkboxes.
|
|
*/
|
|
export class CheckboxBuilder extends ComponentBuilder<APICheckboxComponent> {
|
|
/**
|
|
* Creates a new checkbox from API data.
|
|
*
|
|
* @param data - The API data to create this checkbox with
|
|
* @example
|
|
* Creating a checkbox from an API data object:
|
|
* ```ts
|
|
* const checkbox = new CheckboxBuilder({
|
|
* custom_id: 'accept_terms',
|
|
* default: false,
|
|
* });
|
|
* ```
|
|
* @example
|
|
* Creating a checkbox using setters and API data:
|
|
* ```ts
|
|
* const checkbox = new CheckboxBuilder()
|
|
* .setCustomId('subscribe_newsletter')
|
|
* .setDefault(true);
|
|
* ```
|
|
*/
|
|
public constructor(data?: Partial<APICheckboxComponent>) {
|
|
super({ type: ComponentType.Checkbox, ...data });
|
|
}
|
|
|
|
/**
|
|
* Sets the custom id of this checkbox.
|
|
*
|
|
* @param customId - The custom id to use
|
|
*/
|
|
public setCustomId(customId: string) {
|
|
this.data.custom_id = customId;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Sets whether this checkbox is checked by default.
|
|
*
|
|
* @param isDefault - Whether the checkbox should be checked by default
|
|
*/
|
|
public setDefault(isDefault: boolean) {
|
|
this.data.default = isDefault;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc ComponentBuilder.toJSON}
|
|
*/
|
|
public override toJSON(): APICheckboxComponent {
|
|
checkboxPredicate.parse(this.data);
|
|
return {
|
|
...this.data,
|
|
} as APICheckboxComponent;
|
|
}
|
|
}
|