Skip to content

Commit 16cd04f

Browse files
committed
fix: optional components and types
1 parent 3df8464 commit 16cd04f

13 files changed

Lines changed: 95 additions & 46 deletions

File tree

.npmrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
public-hoist-pattern[]=*commandkit*
1+
prefer-workspace-packages=true
2+
public-hoist-pattern[]=@commandkit/*

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ git checkout -b your-feature-or-bugfix
4444
2. Make your changes. Please make sure to use the
4545
[Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
4646
extension for consistent formatting and comments wherever
47-
necessary. Alternatively, you can run `pnpm prettier:format` after
48-
you've made your changes.
47+
necessary. Alternatively, you can run `pnpm format` after you've
48+
made your changes.
4949

5050
3. Ensure that your changes don't break any existing functionality.
5151
You can test the functionality of your code depending on where

apps/test-bot/src/app/commands/(interactions)/prompt.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
StringSelectMenuOption,
1212
FileUpload,
1313
} from 'commandkit';
14-
import { ComponentType, MessageFlags } from 'discord.js';
14+
import { MessageFlags } from 'discord.js';
1515

1616
export const command: CommandData = {
1717
name: 'prompt',

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"bootstrap": "turbo gen bootstrap --args",
1212
"check-types": "turbo run --filter=\"./packages/*\" check-types",
1313
"build": "turbo run --filter=\"./packages/*\" build",
14-
"docgen": "tsx ./scripts/docs/generate-typescript-docs.ts && pnpm prettier:format",
14+
"docgen": "tsx ./scripts/docs/generate-typescript-docs.ts && pnpm format",
1515
"prettier:check": "prettier --experimental-cli --check . --ignore-path=.prettierignore",
16-
"prettier:format": "prettier --experimental-cli --write . --ignore-path=.prettierignore"
16+
"format": "prettier --experimental-cli --write . --ignore-path=.prettierignore"
1717
},
1818
"devDependencies": {
1919
"@types/node": "^22.10.2",

packages/commandkit/src/components/common/element.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ActionRowBuilder, TextInputBuilder } from 'discord.js';
22
import type { ButtonKit } from '../interactive/button/ButtonKit';
33
import type { ModalKit } from '../interactive/modal/ModalKit';
4+
import { getConfig } from '../../config/config';
45

56
/**
67
* Represents the types of elements that can be used in CommandKit.
@@ -102,3 +103,18 @@ export function createElement(
102103
}
103104

104105
export { createElement as jsx, createElement as jsxs };
106+
107+
let _isOptional: boolean;
108+
109+
export function applyDefaultOptionalComponentBehavior<P>(props: P): P {
110+
if (
111+
props &&
112+
typeof props === 'object' &&
113+
(props as { required?: boolean }).required == null &&
114+
(_isOptional ??= getConfig().jsxDefaultOptionalComponents)
115+
) {
116+
(props as { required?: boolean }).required ??= false;
117+
}
118+
119+
return props;
120+
}

packages/commandkit/src/components/display/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { ComponentBuilder } from 'discord.js';
2+
import { applyDefaultOptionalComponentBehavior } from '../..';
23

34
/**
45
* @private
56
*/
67
export function applyId(props: { id?: number }, component: ComponentBuilder) {
8+
applyDefaultOptionalComponentBehavior(props);
9+
710
if (props.id != null && 'setId' in component) {
811
component.setId(props.id);
912
}

packages/commandkit/src/components/interactive/modal/Modal.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {
55
FileUploadBuilder,
66
} from 'discord.js';
77
import { MaybeArray } from '../../common/types';
8-
import { CommandKitElement } from '../../common/element';
8+
import {
9+
applyDefaultOptionalComponentBehavior,
10+
CommandKitElement,
11+
} from '../../common/element';
912
import {
1013
CommandKitModalBuilderInteractionCollectorDispatchContextData,
1114
ModalKit,
@@ -95,6 +98,7 @@ export interface TextInputProps {
9598
export function TextInput(
9699
props: TextInputProps & { style: TextInputStyle },
97100
): CommandKitElement<'text-input'> {
101+
applyDefaultOptionalComponentBehavior(props);
98102
const input = new TextInputBuilder().setStyle(props.style);
99103

100104
if (props.customId) {
@@ -127,7 +131,7 @@ export function TextInput(
127131
input.setValue(props.value);
128132
}
129133

130-
if (props.required) {
134+
if (props.required != null) {
131135
input.setRequired(props.required);
132136
}
133137

packages/commandkit/src/components/interactive/select-menu/SelectMenu.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
RoleSelectMenuComponentData,
99
RoleSelectMenuInteraction,
1010
SelectMenuComponentOptionData,
11+
Snowflake,
1112
StringSelectMenuInteraction,
1213
StringSelectMenuOptionBuilder,
1314
UserSelectMenuComponentData,
@@ -24,6 +25,7 @@ import {
2425
CommandKitSelectMenuBuilderInteractionCollectorDispatchContextData,
2526
CommandKitSelectMenuBuilderOnEnd,
2627
} from './common';
28+
import { applyDefaultOptionalComponentBehavior } from '../../common/element';
2729

2830
/**
2931
* Type for the common properties shared by all select menu builders.
@@ -39,8 +41,10 @@ export interface CommonSelectMenuProps<T, C> {
3941
* Type for the base select menu component data.
4042
*/
4143
export interface SelectMenuProps<T, C>
42-
extends Partial<Omit<BaseSelectMenuComponentData, 'type'>>,
43-
CommonSelectMenuProps<T, C> {}
44+
extends Partial<Omit<BaseSelectMenuComponentData, 'type' | 'required'>>,
45+
CommonSelectMenuProps<T, C> {
46+
required?: boolean;
47+
}
4448

4549
/**
4650
* The properties for a string select menu component.
@@ -83,9 +87,13 @@ export type ResolveBuilderInteraction<T> = T extends StringSelectMenuKit
8387
*/
8488
function applyPropsToBuilder<B extends CommonBuilderKit>(
8589
builder: B,
86-
props: Partial<Omit<BaseSelectMenuComponentData, 'type'>> &
87-
CommonSelectMenuProps<ResolveBuilderInteraction<B>, B>,
90+
props: Partial<Omit<BaseSelectMenuComponentData, 'type' | 'required'>> &
91+
CommonSelectMenuProps<ResolveBuilderInteraction<B>, B> & {
92+
required?: boolean;
93+
},
8894
) {
95+
applyDefaultOptionalComponentBehavior(props);
96+
8997
builder.setCustomId(props.customId ?? `select-menu::${crypto.randomUUID()}`);
9098

9199
if (props.maxValues != null) {
@@ -116,6 +124,10 @@ function applyPropsToBuilder<B extends CommonBuilderKit>(
116124
// @ts-ignore
117125
builder.onSelect(props.onSelect, props.options);
118126
}
127+
128+
if (props.required != null) {
129+
builder.setRequired(props.required);
130+
}
119131
}
120132

121133
/**
@@ -182,8 +194,10 @@ export function StringSelectMenuOption(props: StringSelectMenuOptionProps) {
182194
* The UserSelectMenu component.
183195
*/
184196
export interface UserSelectMenuProps
185-
extends Partial<Omit<UserSelectMenuComponentData, 'type'>>,
186-
CommonSelectMenuProps<UserSelectMenuInteraction, UserSelectMenuKit> {}
197+
extends Partial<Omit<UserSelectMenuComponentData, 'type' | 'defaultValues'>>,
198+
CommonSelectMenuProps<UserSelectMenuInteraction, UserSelectMenuKit> {
199+
defaultValues?: MaybeArray<string | Snowflake>;
200+
}
187201

188202
/**
189203
* The UserSelectMenu component.

packages/commandkit/src/config/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export function defineConfig(
7777
disablePermissionsMiddleware:
7878
config.disablePermissionsMiddleware ??
7979
defaultConfig.disablePermissionsMiddleware,
80+
jsxDefaultOptionalComponents:
81+
config.jsxDefaultOptionalComponents ??
82+
defaultConfig.jsxDefaultOptionalComponents,
8083
};
8184

8285
return defined;

packages/commandkit/src/config/default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ export const defaultConfig: ResolvedCommandKitConfig = {
3434
development: true,
3535
production: false,
3636
},
37+
jsxDefaultOptionalComponents: true,
3738
};

0 commit comments

Comments
 (0)