Navigation Menu

Skip to content

Commit

Permalink
Handle delete key in useInput hook (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Jun 23, 2020
1 parent ec9e449 commit 0692fa5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -1144,6 +1144,13 @@ Default: `false`

Backspace key was pressed.

###### key.delete

Type: `boolean`\
Default: `false`

Delete key was pressed.

###### key.meta

Type: `boolean`\
Expand Down
8 changes: 7 additions & 1 deletion src/hooks/use-input.ts
Expand Up @@ -55,6 +55,11 @@ export interface Key {
*/
backspace: boolean;

/**
* Delete key was pressed.
*/
delete: boolean;

/**
* [Meta key](https://en.wikipedia.org/wiki/Meta_key) was pressed.
*/
Expand Down Expand Up @@ -131,6 +136,7 @@ const useInput = (inputHandler: Handler, options: Options = {}) => {
shift: false,
tab: input === '\t' || input === '\u001B[Z',
backspace: input === '\u0008',
delete: input === '\u007F',
meta: false
};

Expand Down Expand Up @@ -158,7 +164,7 @@ const useInput = (inputHandler: Handler, options: Options = {}) => {
key.shift = true;
}

if (key.tab || key.backspace) {
if (key.tab || key.backspace || key.delete) {
input = '';
}

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/use-input.tsx
Expand Up @@ -65,6 +65,11 @@ const UserInput: FC<{test: string}> = ({test}) => {
return;
}

if (test === 'delete' && input === '' && key.delete) {
exit();
return;
}

throw new Error('Crash');
});

Expand Down
14 changes: 13 additions & 1 deletion test/hooks.tsx
Expand Up @@ -23,7 +23,12 @@ const term = (fixture: string, args: string[] = []) => {
});

const result = {
write: (input: string) => ps.write(input),
write: (input: string) => {
// Give TS and Ink time to start up and render UI
setTimeout(() => {
ps.write(input);
}, 1000);
},
output: '',
waitForExit: () => exitPromise
};
Expand Down Expand Up @@ -128,6 +133,13 @@ test('useInput - handle backspace', async t => {
t.true(ps.output.includes('exited'));
});

test('useInput - handle delete', async t => {
const ps = term('use-input', ['delete']);
ps.write('\u007F');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});

test('useInput - ignore input if not active', async t => {
const ps = term('use-input-multiple');
ps.write('x');
Expand Down

0 comments on commit 0692fa5

Please sign in to comment.