Files
discord.js/packages/builders/src/components/checkbox/Checkbox.ts
faceboy ca7719e822 feat(builders): add checkbox, checkboxgroup, and radiogroup builders (#11410)
* 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>
2026-03-11 13:39:16 +01:00

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;
}
}