@inquirer/select 2.0.0 → 2.1.0

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/dist/cjs/index.js CHANGED
@@ -22,6 +22,7 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
22
22
  const theme = (0, core_1.makeTheme)(selectTheme, config.theme);
23
23
  const prefix = (0, core_1.usePrefix)({ theme });
24
24
  const [status, setStatus] = (0, core_1.useState)('pending');
25
+ const searchTimeoutRef = (0, core_1.useRef)(undefined);
25
26
  const bounds = (0, core_1.useMemo)(() => {
26
27
  const first = items.findIndex(isSelectable);
27
28
  // TODO: Replace with `findLastIndex` when it's available.
@@ -38,12 +39,14 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
38
39
  const [active, setActive] = (0, core_1.useState)(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
39
40
  // Safe to assume the cursor position always point to a Choice.
40
41
  const selectedChoice = items[active];
41
- (0, core_1.useKeypress)((key) => {
42
+ (0, core_1.useKeypress)((key, rl) => {
43
+ clearTimeout(searchTimeoutRef.current);
42
44
  if ((0, core_1.isEnterKey)(key)) {
43
45
  setStatus('done');
44
46
  done(selectedChoice.value);
45
47
  }
46
48
  else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) {
49
+ rl.clearLine(0);
47
50
  if (loop ||
48
51
  ((0, core_1.isUpKey)(key) && active !== bounds.first) ||
49
52
  ((0, core_1.isDownKey)(key) && active !== bounds.last)) {
@@ -56,12 +59,33 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
56
59
  }
57
60
  }
58
61
  else if ((0, core_1.isNumberKey)(key)) {
62
+ rl.clearLine(0);
59
63
  const position = Number(key.name) - 1;
60
64
  const item = items[position];
61
65
  if (item != null && isSelectable(item)) {
62
66
  setActive(position);
63
67
  }
64
68
  }
69
+ else if ((0, core_1.isBackspaceKey)(key)) {
70
+ rl.clearLine(0);
71
+ }
72
+ else {
73
+ // Default to search
74
+ const searchTerm = rl.line.toLowerCase();
75
+ const matchIndex = items.findIndex((item) => {
76
+ if (core_1.Separator.isSeparator(item) || !isSelectable(item))
77
+ return false;
78
+ return String(item.name || item.value)
79
+ .toLowerCase()
80
+ .startsWith(searchTerm);
81
+ });
82
+ if (matchIndex >= 0) {
83
+ setActive(matchIndex);
84
+ }
85
+ searchTimeoutRef.current = setTimeout(() => {
86
+ rl.clearLine(0);
87
+ }, 700);
88
+ }
65
89
  });
66
90
  const message = theme.style.message(config.message);
67
91
  let helpTip;
@@ -1,4 +1,4 @@
1
- import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, useMemo, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, makeTheme, } from '@inquirer/core';
1
+ import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, useMemo, isBackspaceKey, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, makeTheme, } from '@inquirer/core';
2
2
  import chalk from 'chalk';
3
3
  import figures from 'figures';
4
4
  import ansiEscapes from 'ansi-escapes';
@@ -15,6 +15,7 @@ export default createPrompt((config, done) => {
15
15
  const theme = makeTheme(selectTheme, config.theme);
16
16
  const prefix = usePrefix({ theme });
17
17
  const [status, setStatus] = useState('pending');
18
+ const searchTimeoutRef = useRef(undefined);
18
19
  const bounds = useMemo(() => {
19
20
  const first = items.findIndex(isSelectable);
20
21
  // TODO: Replace with `findLastIndex` when it's available.
@@ -31,12 +32,14 @@ export default createPrompt((config, done) => {
31
32
  const [active, setActive] = useState(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
32
33
  // Safe to assume the cursor position always point to a Choice.
33
34
  const selectedChoice = items[active];
34
- useKeypress((key) => {
35
+ useKeypress((key, rl) => {
36
+ clearTimeout(searchTimeoutRef.current);
35
37
  if (isEnterKey(key)) {
36
38
  setStatus('done');
37
39
  done(selectedChoice.value);
38
40
  }
39
41
  else if (isUpKey(key) || isDownKey(key)) {
42
+ rl.clearLine(0);
40
43
  if (loop ||
41
44
  (isUpKey(key) && active !== bounds.first) ||
42
45
  (isDownKey(key) && active !== bounds.last)) {
@@ -49,12 +52,33 @@ export default createPrompt((config, done) => {
49
52
  }
50
53
  }
51
54
  else if (isNumberKey(key)) {
55
+ rl.clearLine(0);
52
56
  const position = Number(key.name) - 1;
53
57
  const item = items[position];
54
58
  if (item != null && isSelectable(item)) {
55
59
  setActive(position);
56
60
  }
57
61
  }
62
+ else if (isBackspaceKey(key)) {
63
+ rl.clearLine(0);
64
+ }
65
+ else {
66
+ // Default to search
67
+ const searchTerm = rl.line.toLowerCase();
68
+ const matchIndex = items.findIndex((item) => {
69
+ if (Separator.isSeparator(item) || !isSelectable(item))
70
+ return false;
71
+ return String(item.name || item.value)
72
+ .toLowerCase()
73
+ .startsWith(searchTerm);
74
+ });
75
+ if (matchIndex >= 0) {
76
+ setActive(matchIndex);
77
+ }
78
+ searchTimeoutRef.current = setTimeout(() => {
79
+ rl.clearLine(0);
80
+ }, 700);
81
+ }
58
82
  });
59
83
  const message = theme.style.message(config.message);
60
84
  let helpTip;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/select",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
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": "^7.0.0",
57
+ "@inquirer/core": "^7.0.1",
58
58
  "@inquirer/type": "^1.2.0",
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.11"
64
+ "@inquirer/testing": "^2.1.12"
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": "44016a40bc9e93455dfdb9fa6c25c27c1c109bd3"
89
+ "gitHead": "c91196e66689cfc05e9237763e6f95f3eb5342a1"
90
90
  }