Skip to content

Commit c6a79cd

Browse files
IhToNSBoudrias
andauthored
Feature(@inquirer/select): new theme option to display each choice index (#1687)
Signed-off-by: ATAlgaba <[email protected]> Co-authored-by: Simon Boudrias <[email protected]>
1 parent e12335e commit c6a79cd

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

packages/select/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ type Theme = {
144144
cursor: string;
145145
};
146146
helpMode: 'always' | 'never' | 'auto';
147+
indexMode: 'hidden' | 'number';
147148
};
148149
```
149150

@@ -153,6 +154,13 @@ type Theme = {
153154
- `always`: The help tips will always show and never hide.
154155
- `never`: The help tips will never show.
155156

157+
### `theme.indexMode`
158+
159+
Controls how indices are displayed before each choice:
160+
161+
- `hidden` (default): No indices are shown
162+
- `number`: Display a number before each choice (e.g. "1. Option A")
163+
156164
# License
157165

158166
Copyright (c) 2023 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))<br/>

packages/select/select.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -822,4 +822,27 @@ describe('select prompt', () => {
822822
expect(getScreen()).toMatchInlineSnapshot('"✔ Select a number 2"');
823823
});
824824
});
825+
826+
it('Displays the element index', async () => {
827+
const { answer, events, getScreen } = await render(select, {
828+
message: 'Select a number',
829+
choices: numberedChoices,
830+
theme: { indexMode: 'number' },
831+
});
832+
833+
expect(getScreen()).toMatchInlineSnapshot(`
834+
"? Select a number
835+
❯ 1. 1
836+
2. 2
837+
3. 3
838+
4. 4
839+
5. 5
840+
6. 6
841+
7. 7
842+
(Use arrow keys to reveal more choices)"
843+
`);
844+
845+
events.keypress('enter');
846+
await expect(answer).resolves.toEqual(1);
847+
});
825848
});

packages/select/src/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type SelectTheme = {
3030
description: (text: string) => string;
3131
};
3232
helpMode: 'always' | 'never' | 'auto';
33+
indexMode: 'hidden' | 'number';
3334
};
3435

3536
const selectTheme: SelectTheme = {
@@ -39,6 +40,7 @@ const selectTheme: SelectTheme = {
3940
description: (text: string) => colors.cyan(text),
4041
},
4142
helpMode: 'auto',
43+
indexMode: 'hidden',
4244
};
4345

4446
type Choice<Value> = {
@@ -222,20 +224,21 @@ export default createPrompt(
222224
const page = usePagination({
223225
items,
224226
active,
225-
renderItem({ item, isActive }) {
227+
renderItem({ item, isActive, index }) {
226228
if (Separator.isSeparator(item)) {
227229
return ` ${item.separator}`;
228230
}
229231

232+
const indexLabel = theme.indexMode === 'number' ? `${index + 1}. ` : '';
230233
if (item.disabled) {
231234
const disabledLabel =
232235
typeof item.disabled === 'string' ? item.disabled : '(disabled)';
233-
return theme.style.disabled(`${item.name} ${disabledLabel}`);
236+
return theme.style.disabled(`${indexLabel}${item.name} ${disabledLabel}`);
234237
}
235238

236239
const color = isActive ? theme.style.highlight : (x: string) => x;
237240
const cursor = isActive ? theme.icon.cursor : ` `;
238-
return color(`${cursor} ${item.name}`);
241+
return color(`${cursor} ${indexLabel}${item.name}`);
239242
},
240243
pageSize,
241244
loop,

0 commit comments

Comments
 (0)