Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-donuts-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/core": patch
---

Treat an ideographic space (U+3000) committed by IMEs (e.g. Japanese or Chinese input) as space when toggling options in multi-select prompts.
7 changes: 7 additions & 0 deletions packages/core/src/prompts/group-multiselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export default class GroupMultiSelectPrompt<T extends { value: any }> extends Pr
this.#selectableGroups ? 0 : 1
);

this.on('key', (char, key) => {
// Treat ideographic spaces as if they were normal spaces.
if (key.name === undefined && char === ' ') {
this.toggleValue();
}
});

this.on('cursor', (key) => {
switch (key) {
case 'left':
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/prompts/multi-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ export default class MultiSelectPrompt<T extends OptionLike> extends Prompt<T['v
0
);
this.cursor = this.options[cursor]?.disabled ? findCursor<T>(cursor, 1, this.options) : cursor;
this.on('key', (_char, key) => {
this.on('key', (char, key) => {
if (key.name === 'a') {
this.toggleAll();
}
if (key.name === 'i') {
this.toggleInvert();
}
// Some IMEs (e.g. Japanese or Chinese input) commit an ideographic space (U+3000)
// with no key name when the space key is pressed. Treat it as space so
// toggling options still works.
if (key.name === undefined && char === ' ') {
this.toggleValue();
}
});

this.on('cursor', (key) => {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/prompts/group-multiselect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ describe('GroupMultiSelectPrompt', () => {
expect(output.buffer).to.deep.equal([cursor.hide, 'foo']);
});

test('toggles option with ideographic space (U+3000) committed by IME', () => {
const instance = new GroupMultiSelectPrompt({
input,
output,
render: () => 'foo',
options: {
group: [{ value: 'foo' }, { value: 'bar' }],
},
selectableGroups: false,
});
instance.prompt();

input.emit('keypress', ' ', { sequence: '\u3000' });
expect(instance.value).toEqual(['foo']);
input.emit('keypress', ' ', { sequence: '\u3000' });
expect(instance.value).toEqual([]);
});

test('does not throw if empty options are provided', () => {
const instance = new GroupMultiSelectPrompt({
input,
Expand Down
15 changes: 15 additions & 0 deletions packages/core/test/prompts/multi-select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ describe('MultiSelectPrompt', () => {
expect(instance.value).toEqual(['foo']);
});

test('toggles option with ideographic space (U+3000) committed by IME', () => {
const instance = new MultiSelectPrompt({
input,
output,
render: () => 'foo',
options: [{ value: 'foo' }, { value: 'bar' }],
});
instance.prompt();

input.emit('keypress', ' ', { sequence: '\u3000' });
expect(instance.value).toEqual(['foo']);
input.emit('keypress', ' ', { sequence: '\u3000' });
expect(instance.value).toEqual([]);
});

test('disabled options are skipped', () => {
const instance = new MultiSelectPrompt({
input,
Expand Down
Loading