@inquirer/select 1.3.0 → 1.3.2

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
@@ -38,7 +38,12 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
38
38
  throw new Error('[select prompt] No selectable choices. All choices are disabled.');
39
39
  return { first, last };
40
40
  }, [items]);
41
- const [active, setActive] = (0, core_1.useState)(bounds.first);
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);
42
47
  // Safe to assume the cursor position always point to a Choice.
43
48
  const selectedChoice = items[active];
44
49
  (0, core_1.useKeypress)((key) => {
@@ -47,23 +52,23 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
47
52
  done(selectedChoice.value);
48
53
  }
49
54
  else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) {
50
- if (!loop && active === bounds.first && (0, core_1.isUpKey)(key))
51
- return;
52
- if (!loop && active === bounds.last && (0, core_1.isDownKey)(key))
53
- return;
54
- const offset = (0, core_1.isUpKey)(key) ? -1 : 1;
55
- let next = active;
56
- do {
57
- next = (next + offset + items.length) % items.length;
58
- } while (!isSelectable(items[next]));
59
- 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
+ }
60
65
  }
61
66
  else if ((0, core_1.isNumberKey)(key)) {
62
67
  const position = Number(key.name) - 1;
63
68
  const item = items[position];
64
- if (item == null || !isSelectable(item))
65
- return;
66
- setActive(position);
69
+ if (item != null && isSelectable(item)) {
70
+ setActive(position);
71
+ }
67
72
  }
68
73
  });
69
74
  let message = chalk_1.default.bold(config.message);
@@ -11,6 +11,7 @@ declare const _default: <Value extends unknown>(config: {
11
11
  choices: readonly (Separator | Choice<Value>)[];
12
12
  pageSize?: number | undefined;
13
13
  loop?: boolean | undefined;
14
+ default?: Value | undefined;
14
15
  }, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
15
16
  export default _default;
16
17
  export { Separator };
@@ -31,7 +31,12 @@ export default createPrompt((config, done) => {
31
31
  throw new Error('[select prompt] No selectable choices. All choices are disabled.');
32
32
  return { first, last };
33
33
  }, [items]);
34
- const [active, setActive] = useState(bounds.first);
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);
35
40
  // Safe to assume the cursor position always point to a Choice.
36
41
  const selectedChoice = items[active];
37
42
  useKeypress((key) => {
@@ -40,23 +45,23 @@ export default createPrompt((config, done) => {
40
45
  done(selectedChoice.value);
41
46
  }
42
47
  else if (isUpKey(key) || isDownKey(key)) {
43
- if (!loop && active === bounds.first && isUpKey(key))
44
- return;
45
- if (!loop && active === bounds.last && isDownKey(key))
46
- return;
47
- const offset = 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);
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
+ }
53
58
  }
54
59
  else if (isNumberKey(key)) {
55
60
  const position = Number(key.name) - 1;
56
61
  const item = items[position];
57
- if (item == null || !isSelectable(item))
58
- return;
59
- setActive(position);
62
+ if (item != null && isSelectable(item)) {
63
+ setActive(position);
64
+ }
60
65
  }
61
66
  });
62
67
  let message = chalk.bold(config.message);
@@ -11,6 +11,7 @@ declare const _default: <Value extends unknown>(config: {
11
11
  choices: readonly (Separator | Choice<Value>)[];
12
12
  pageSize?: number | undefined;
13
13
  loop?: boolean | undefined;
14
+ default?: Value | undefined;
14
15
  }, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
15
16
  export default _default;
16
17
  export { Separator };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/select",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
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.1.0",
58
- "@inquirer/type": "^1.1.5",
57
+ "@inquirer/core": "^5.1.2",
58
+ "@inquirer/type": "^1.1.6",
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.8"
64
+ "@inquirer/testing": "^2.1.10"
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": "c88aaca660e58aa0fb079fe656c1004855e029da"
89
+ "gitHead": "6c0d1d23e6881a10bc3d718d079a0a09ba382dd5"
90
90
  }