@inquirer/select 1.2.12 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -51,7 +51,9 @@ const answer = await select({
51
51
  | -------- | ---------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
52
52
  | message | `string` | yes | The question to ask |
53
53
  | choices | `Array<{ value: string, name?: string, description?: string, disabled?: boolean \| string } \| Separator>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. The `description` will be displayed under the prompt when the cursor land over the choice. |
54
+ | default | `string` | no | Defines in front of which item the cursor will initially appear. When omitted, the cursor will appear on the first selectable item. |
54
55
  | pageSize | `number` | no | By default, lists of choice longer than 7 will be paginated. Use this option to control how many choices will appear on the screen at once. |
56
+ | loop | `boolean` | no | Defaults to `true`. When set to `false`, the cursor will be constrained to the top and bottom of the choice list without looping. |
55
57
 
56
58
  The `Separator` object can be used to render non-selectable lines in the choice list. By default it'll render a line, but you can provide the text as argument (`new Separator('-- Dependencies --')`). This option is often used to add labels to groups within long list of options.
57
59
 
package/dist/cjs/index.js CHANGED
@@ -26,16 +26,24 @@ function renderItem({ item, isActive }) {
26
26
  return color(`${prefix} ${line}`);
27
27
  }
28
28
  exports.default = (0, core_1.createPrompt)((config, done) => {
29
- const { choices: items, pageSize } = config;
29
+ const { choices: items, loop = true, pageSize } = config;
30
30
  const firstRender = (0, core_1.useRef)(true);
31
31
  const prefix = (0, core_1.usePrefix)();
32
32
  const [status, setStatus] = (0, core_1.useState)('pending');
33
- const [active, setActive] = (0, core_1.useState)(() => {
34
- const selected = items.findIndex(isSelectable);
35
- if (selected < 0)
33
+ const bounds = (0, core_1.useMemo)(() => {
34
+ const first = items.findIndex(isSelectable);
35
+ // TODO: Replace with `findLastIndex` when it's available.
36
+ const last = items.length - 1 - [...items].reverse().findIndex(isSelectable);
37
+ if (first < 0)
36
38
  throw new Error('[select prompt] No selectable choices. All choices are disabled.');
37
- return selected;
38
- });
39
+ return { first, last };
40
+ }, [items]);
41
+ const defaultItemIndex = (0, core_1.useMemo)(() => {
42
+ if (!('default' in config))
43
+ return -1;
44
+ return items.findIndex((item) => isSelectable(item) && item.value === config.default);
45
+ }, [config.default, items]);
46
+ const [active, setActive] = (0, core_1.useState)(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
39
47
  // Safe to assume the cursor position always point to a Choice.
40
48
  const selectedChoice = items[active];
41
49
  (0, core_1.useKeypress)((key) => {
@@ -44,19 +52,23 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
44
52
  done(selectedChoice.value);
45
53
  }
46
54
  else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) {
47
- const offset = (0, core_1.isUpKey)(key) ? -1 : 1;
48
- let next = active;
49
- do {
50
- next = (next + offset + items.length) % items.length;
51
- } while (!isSelectable(items[next]));
52
- setActive(next);
55
+ if (loop ||
56
+ ((0, core_1.isUpKey)(key) && active !== bounds.first) ||
57
+ ((0, core_1.isDownKey)(key) && active !== bounds.last)) {
58
+ const offset = (0, core_1.isUpKey)(key) ? -1 : 1;
59
+ let next = active;
60
+ do {
61
+ next = (next + offset + items.length) % items.length;
62
+ } while (!isSelectable(items[next]));
63
+ setActive(next);
64
+ }
53
65
  }
54
66
  else if ((0, core_1.isNumberKey)(key)) {
55
67
  const position = Number(key.name) - 1;
56
68
  const item = items[position];
57
- if (item == null || !isSelectable(item))
58
- return;
59
- setActive(position);
69
+ if (item != null && isSelectable(item)) {
70
+ setActive(position);
71
+ }
60
72
  }
61
73
  });
62
74
  let message = chalk_1.default.bold(config.message);
@@ -64,12 +76,12 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
64
76
  firstRender.current = false;
65
77
  message += chalk_1.default.dim(' (Use arrow keys)');
66
78
  }
67
- const lines = items
68
- .map((item, index) => renderItem({ item, isActive: index === active }))
69
- .join('\n');
70
- const page = (0, core_1.usePagination)(lines, {
79
+ const page = (0, core_1.usePagination)({
80
+ items,
71
81
  active,
82
+ renderItem,
72
83
  pageSize,
84
+ loop,
73
85
  });
74
86
  if (status === 'done') {
75
87
  return `${prefix} ${message} ${chalk_1.default.cyan(selectedChoice.name || selectedChoice.value)}`;
@@ -10,6 +10,8 @@ declare const _default: <Value extends unknown>(config: {
10
10
  message: string | Promise<string> | (() => Promise<string>);
11
11
  choices: readonly (Separator | Choice<Value>)[];
12
12
  pageSize?: number | undefined;
13
+ loop?: boolean | undefined;
14
+ default?: Value | undefined;
13
15
  }, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
14
16
  export default _default;
15
17
  export { Separator };
@@ -1,4 +1,4 @@
1
- import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, } from '@inquirer/core';
1
+ import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, useMemo, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, } from '@inquirer/core';
2
2
  import chalk from 'chalk';
3
3
  import figures from 'figures';
4
4
  import ansiEscapes from 'ansi-escapes';
@@ -19,16 +19,24 @@ function renderItem({ item, isActive }) {
19
19
  return color(`${prefix} ${line}`);
20
20
  }
21
21
  export default createPrompt((config, done) => {
22
- const { choices: items, pageSize } = config;
22
+ const { choices: items, loop = true, pageSize } = config;
23
23
  const firstRender = useRef(true);
24
24
  const prefix = usePrefix();
25
25
  const [status, setStatus] = useState('pending');
26
- const [active, setActive] = useState(() => {
27
- const selected = items.findIndex(isSelectable);
28
- if (selected < 0)
26
+ const bounds = useMemo(() => {
27
+ const first = items.findIndex(isSelectable);
28
+ // TODO: Replace with `findLastIndex` when it's available.
29
+ const last = items.length - 1 - [...items].reverse().findIndex(isSelectable);
30
+ if (first < 0)
29
31
  throw new Error('[select prompt] No selectable choices. All choices are disabled.');
30
- return selected;
31
- });
32
+ return { first, last };
33
+ }, [items]);
34
+ const defaultItemIndex = useMemo(() => {
35
+ if (!('default' in config))
36
+ return -1;
37
+ return items.findIndex((item) => isSelectable(item) && item.value === config.default);
38
+ }, [config.default, items]);
39
+ const [active, setActive] = useState(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
32
40
  // Safe to assume the cursor position always point to a Choice.
33
41
  const selectedChoice = items[active];
34
42
  useKeypress((key) => {
@@ -37,19 +45,23 @@ export default createPrompt((config, done) => {
37
45
  done(selectedChoice.value);
38
46
  }
39
47
  else if (isUpKey(key) || isDownKey(key)) {
40
- const offset = isUpKey(key) ? -1 : 1;
41
- let next = active;
42
- do {
43
- next = (next + offset + items.length) % items.length;
44
- } while (!isSelectable(items[next]));
45
- setActive(next);
48
+ if (loop ||
49
+ (isUpKey(key) && active !== bounds.first) ||
50
+ (isDownKey(key) && active !== bounds.last)) {
51
+ const offset = isUpKey(key) ? -1 : 1;
52
+ let next = active;
53
+ do {
54
+ next = (next + offset + items.length) % items.length;
55
+ } while (!isSelectable(items[next]));
56
+ setActive(next);
57
+ }
46
58
  }
47
59
  else if (isNumberKey(key)) {
48
60
  const position = Number(key.name) - 1;
49
61
  const item = items[position];
50
- if (item == null || !isSelectable(item))
51
- return;
52
- setActive(position);
62
+ if (item != null && isSelectable(item)) {
63
+ setActive(position);
64
+ }
53
65
  }
54
66
  });
55
67
  let message = chalk.bold(config.message);
@@ -57,12 +69,12 @@ export default createPrompt((config, done) => {
57
69
  firstRender.current = false;
58
70
  message += chalk.dim(' (Use arrow keys)');
59
71
  }
60
- const lines = items
61
- .map((item, index) => renderItem({ item, isActive: index === active }))
62
- .join('\n');
63
- const page = usePagination(lines, {
72
+ const page = usePagination({
73
+ items,
64
74
  active,
75
+ renderItem,
65
76
  pageSize,
77
+ loop,
66
78
  });
67
79
  if (status === 'done') {
68
80
  return `${prefix} ${message} ${chalk.cyan(selectedChoice.name || selectedChoice.value)}`;
@@ -10,6 +10,8 @@ declare const _default: <Value extends unknown>(config: {
10
10
  message: string | Promise<string> | (() => Promise<string>);
11
11
  choices: readonly (Separator | Choice<Value>)[];
12
12
  pageSize?: number | undefined;
13
+ loop?: boolean | undefined;
14
+ default?: Value | undefined;
13
15
  }, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
14
16
  export default _default;
15
17
  export { Separator };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/select",
3
- "version": "1.2.12",
3
+ "version": "1.3.1",
4
4
  "description": "Inquirer select/list prompt",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "typings": "./dist/cjs/types/index.d.ts",
@@ -54,14 +54,14 @@
54
54
  "license": "MIT",
55
55
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/select/README.md",
56
56
  "dependencies": {
57
- "@inquirer/core": "^5.0.1",
57
+ "@inquirer/core": "^5.1.1",
58
58
  "@inquirer/type": "^1.1.5",
59
59
  "ansi-escapes": "^4.3.2",
60
60
  "chalk": "^4.1.2",
61
61
  "figures": "^3.2.0"
62
62
  },
63
63
  "devDependencies": {
64
- "@inquirer/testing": "^2.1.7"
64
+ "@inquirer/testing": "^2.1.9"
65
65
  },
66
66
  "scripts": {
67
67
  "tsc": "yarn run tsc:esm && yarn run tsc:cjs",
@@ -86,5 +86,5 @@
86
86
  }
87
87
  }
88
88
  },
89
- "gitHead": "85784061d702778bc9dd48ca08f09ee9976b06ee"
89
+ "gitHead": "a318aec57d33b53131c0b03cc8dd6ab4efae3e50"
90
90
  }