Skip to content

Commit 8ccf932

Browse files
committed
feat(@inquirer/search): Support providing custom instructions. Fix #1773
1 parent aa7f74b commit 8ccf932

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/search/search.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,38 @@ describe('search prompt', () => {
558558
events.keypress('enter');
559559
await expect(answer).resolves.toEqual('QC');
560560
});
561+
562+
it('supports custom instructions', async () => {
563+
const { answer, events, getScreen } = await render(search, {
564+
message: 'Select a Canadian province',
565+
source: getListSearch(PROVINCES),
566+
pageSize: 3,
567+
instructions: {
568+
navigation: 'Utiliser les flèches directionnelles',
569+
pager: 'Utiliser les flèches pour révéler plus de choix',
570+
},
571+
});
572+
573+
// Test custom pager instruction when results exceed pageSize
574+
expect(getScreen()).toMatchInlineSnapshot(`
575+
"? Select a Canadian province
576+
❯ Alberta
577+
British Columbia
578+
Manitoba
579+
(Utiliser les flèches pour révéler plus de choix)"
580+
`);
581+
582+
// Test custom navigation instruction when results fit in pageSize
583+
events.type('New');
584+
await Promise.resolve();
585+
expect(getScreen()).toMatchInlineSnapshot(`
586+
"? Select a Canadian province New
587+
❯ New Brunswick
588+
Newfoundland and Labrador
589+
(Utiliser les flèches directionnelles)"
590+
`);
591+
592+
events.keypress('enter');
593+
await expect(answer).resolves.toEqual('NB');
594+
});
561595
});

packages/search/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ type SearchConfig<
7171
| Promise<ReadonlyArray<Choice<Value> | Separator>>;
7272
validate?: (value: Value) => boolean | string | Promise<string | boolean>;
7373
pageSize?: number;
74+
instructions?: {
75+
navigation: string;
76+
pager: string;
77+
};
7478
theme?: PartialDeep<Theme<SearchTheme>>;
7579
};
7680

@@ -226,8 +230,8 @@ export default createPrompt(
226230
) {
227231
helpTip =
228232
searchResults.length > pageSize
229-
? `\n${theme.style.help('(Use arrow keys to reveal more choices)')}`
230-
: `\n${theme.style.help('(Use arrow keys)')}`;
233+
? `\n${theme.style.help(`(${config.instructions?.pager ?? 'Use arrow keys to reveal more choices'})`)}`
234+
: `\n${theme.style.help(`(${config.instructions?.navigation ?? 'Use arrow keys'})`)}`;
231235
}
232236

233237
// TODO: What to do if no results are found? Should we display a message?

0 commit comments

Comments
 (0)