Skip to content

Commit f2bc46b

Browse files
kai-natori43081j
andauthored
fix: treat IME-committed ideographic space as the space key (#608)
Co-authored-by: James Garbutt <43081j@users.noreply.github.com>
1 parent be35482 commit f2bc46b

5 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎.changeset/olive-donuts-switch.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/core": patch
3+
---
4+
5+
Treat an ideographic space (U+3000) committed by IMEs (e.g. Japanese or Chinese input) as space when toggling options in multi-select prompts.

‎packages/core/src/prompts/group-multiselect.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ export default class GroupMultiSelectPrompt<T extends { value: any }> extends Pr
6767
this.#selectableGroups ? 0 : 1
6868
);
6969

70+
this.on('key', (char, key) => {
71+
// Treat ideographic spaces as if they were normal spaces.
72+
if (key.name === undefined && char === ' ') {
73+
this.toggleValue();
74+
}
75+
});
76+
7077
this.on('cursor', (key) => {
7178
switch (key) {
7279
case 'left':

‎packages/core/src/prompts/multi-select.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,19 @@ export default class MultiSelectPrompt<T extends OptionLike> extends Prompt<T['v
6060
0
6161
);
6262
this.cursor = this.options[cursor]?.disabled ? findCursor<T>(cursor, 1, this.options) : cursor;
63-
this.on('key', (_char, key) => {
63+
this.on('key', (char, key) => {
6464
if (key.name === 'a') {
6565
this.toggleAll();
6666
}
6767
if (key.name === 'i') {
6868
this.toggleInvert();
6969
}
70+
// Some IMEs (e.g. Japanese or Chinese input) commit an ideographic space (U+3000)
71+
// with no key name when the space key is pressed. Treat it as space so
72+
// toggling options still works.
73+
if (key.name === undefined && char === ' ') {
74+
this.toggleValue();
75+
}
7076
});
7177

7278
this.on('cursor', (key) => {

‎packages/core/test/prompts/group-multiselect.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ describe('GroupMultiSelectPrompt', () => {
3030
expect(output.buffer).to.deep.equal([cursor.hide, 'foo']);
3131
});
3232

33+
test('toggles option with ideographic space (U+3000) committed by IME', () => {
34+
const instance = new GroupMultiSelectPrompt({
35+
input,
36+
output,
37+
render: () => 'foo',
38+
options: {
39+
group: [{ value: 'foo' }, { value: 'bar' }],
40+
},
41+
selectableGroups: false,
42+
});
43+
instance.prompt();
44+
45+
input.emit('keypress', ' ', { sequence: '\u3000' });
46+
expect(instance.value).toEqual(['foo']);
47+
input.emit('keypress', ' ', { sequence: '\u3000' });
48+
expect(instance.value).toEqual([]);
49+
});
50+
3351
test('does not throw if empty options are provided', () => {
3452
const instance = new GroupMultiSelectPrompt({
3553
input,

‎packages/core/test/prompts/multi-select.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ describe('MultiSelectPrompt', () => {
143143
expect(instance.value).toEqual(['foo']);
144144
});
145145

146+
test('toggles option with ideographic space (U+3000) committed by IME', () => {
147+
const instance = new MultiSelectPrompt({
148+
input,
149+
output,
150+
render: () => 'foo',
151+
options: [{ value: 'foo' }, { value: 'bar' }],
152+
});
153+
instance.prompt();
154+
155+
input.emit('keypress', ' ', { sequence: '\u3000' });
156+
expect(instance.value).toEqual(['foo']);
157+
input.emit('keypress', ' ', { sequence: '\u3000' });
158+
expect(instance.value).toEqual([]);
159+
});
160+
146161
test('disabled options are skipped', () => {
147162
const instance = new MultiSelectPrompt({
148163
input,

0 commit comments

Comments
 (0)