@inquirer/rawlist 2.2.3 → 2.3.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/README.md CHANGED
@@ -90,11 +90,13 @@ type Choice<Value> = {
90
90
 
91
91
  Here's each property:
92
92
 
93
- - `value`: The value is what will be returned by `await select()`.
93
+ - `value`: The value is what will be returned by `await rawlist()`.
94
94
  - `name`: This is the string displayed in the choice list.
95
95
  - `short`: Once the prompt is done (press enter), we'll use `short` if defined to render next to the question. By default we'll use `name`.
96
96
  - `key`: The key of the choice. Displayed as `key) name`.
97
97
 
98
+ `choices` can also be an array of string, in which case the string will be used both as the `value` and the `name`.
99
+
98
100
  ## Theming
99
101
 
100
102
  You can theme a prompt by passing a `theme` object option. The theme object only need to includes the keys you wish to modify, we'll fallback on the defaults for the rest.
package/dist/cjs/index.js CHANGED
@@ -10,8 +10,32 @@ const numberRegex = /\d+/;
10
10
  function isSelectableChoice(choice) {
11
11
  return choice != null && !core_1.Separator.isSeparator(choice);
12
12
  }
13
+ function normalizeChoices(choices) {
14
+ let index = 0;
15
+ return choices.map((choice) => {
16
+ var _a, _b, _c;
17
+ if (core_1.Separator.isSeparator(choice))
18
+ return choice;
19
+ index += 1;
20
+ if (typeof choice === 'string') {
21
+ return {
22
+ value: choice,
23
+ name: choice,
24
+ short: choice,
25
+ key: String(index),
26
+ };
27
+ }
28
+ const name = (_a = choice.name) !== null && _a !== void 0 ? _a : String(choice.value);
29
+ return {
30
+ value: choice.value,
31
+ name,
32
+ short: (_b = choice.short) !== null && _b !== void 0 ? _b : name,
33
+ key: (_c = choice.key) !== null && _c !== void 0 ? _c : String(index),
34
+ };
35
+ });
36
+ }
13
37
  exports.default = (0, core_1.createPrompt)((config, done) => {
14
- const { choices } = config;
38
+ const choices = (0, core_1.useMemo)(() => normalizeChoices(config.choices), [config.choices]);
15
39
  const [status, setStatus] = (0, core_1.useState)('pending');
16
40
  const [value, setValue] = (0, core_1.useState)('');
17
41
  const [errorMsg, setError] = (0, core_1.useState)();
@@ -49,15 +73,13 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
49
73
  if (status === 'done') {
50
74
  return `${prefix} ${message} ${theme.style.answer(value)}`;
51
75
  }
52
- let index = 0;
53
76
  const choicesStr = choices
54
77
  .map((choice) => {
55
78
  if (core_1.Separator.isSeparator(choice)) {
56
79
  return ` ${choice.separator}`;
57
80
  }
58
- index += 1;
59
- const line = ` ${choice.key || index}) ${String(choice.name || choice.value)}`;
60
- if (choice.key === value.toLowerCase() || String(index) === value) {
81
+ const line = ` ${choice.key}) ${choice.name}`;
82
+ if (choice.key === value.toLowerCase()) {
61
83
  return theme.style.highlight(line);
62
84
  }
63
85
  return line;
@@ -8,7 +8,7 @@ type Choice<Value> = {
8
8
  };
9
9
  declare const _default: <Value>(config: {
10
10
  message: string;
11
- choices: readonly (Separator | Choice<Value>)[];
11
+ choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
12
12
  theme?: PartialDeep<Theme> | undefined;
13
13
  }, context?: import("@inquirer/type").Context) => import("@inquirer/type").CancelablePromise<Value>;
14
14
  export default _default;
@@ -1,11 +1,34 @@
1
- import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, Separator, makeTheme, } from '@inquirer/core';
1
+ import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, Separator, makeTheme, } from '@inquirer/core';
2
2
  import colors from 'yoctocolors-cjs';
3
3
  const numberRegex = /\d+/;
4
4
  function isSelectableChoice(choice) {
5
5
  return choice != null && !Separator.isSeparator(choice);
6
6
  }
7
+ function normalizeChoices(choices) {
8
+ let index = 0;
9
+ return choices.map((choice) => {
10
+ if (Separator.isSeparator(choice))
11
+ return choice;
12
+ index += 1;
13
+ if (typeof choice === 'string') {
14
+ return {
15
+ value: choice,
16
+ name: choice,
17
+ short: choice,
18
+ key: String(index),
19
+ };
20
+ }
21
+ const name = choice.name ?? String(choice.value);
22
+ return {
23
+ value: choice.value,
24
+ name,
25
+ short: choice.short ?? name,
26
+ key: choice.key ?? String(index),
27
+ };
28
+ });
29
+ }
7
30
  export default createPrompt((config, done) => {
8
- const { choices } = config;
31
+ const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
9
32
  const [status, setStatus] = useState('pending');
10
33
  const [value, setValue] = useState('');
11
34
  const [errorMsg, setError] = useState();
@@ -42,15 +65,13 @@ export default createPrompt((config, done) => {
42
65
  if (status === 'done') {
43
66
  return `${prefix} ${message} ${theme.style.answer(value)}`;
44
67
  }
45
- let index = 0;
46
68
  const choicesStr = choices
47
69
  .map((choice) => {
48
70
  if (Separator.isSeparator(choice)) {
49
71
  return ` ${choice.separator}`;
50
72
  }
51
- index += 1;
52
- const line = ` ${choice.key || index}) ${String(choice.name || choice.value)}`;
53
- if (choice.key === value.toLowerCase() || String(index) === value) {
73
+ const line = ` ${choice.key}) ${choice.name}`;
74
+ if (choice.key === value.toLowerCase()) {
54
75
  return theme.style.highlight(line);
55
76
  }
56
77
  return line;
@@ -8,7 +8,7 @@ type Choice<Value> = {
8
8
  };
9
9
  declare const _default: <Value>(config: {
10
10
  message: string;
11
- choices: readonly (Separator | Choice<Value>)[];
11
+ choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
12
12
  theme?: PartialDeep<Theme> | undefined;
13
13
  }, context?: import("@inquirer/type").Context) => import("@inquirer/type").CancelablePromise<Value>;
14
14
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/rawlist",
3
- "version": "2.2.3",
3
+ "version": "2.3.0",
4
4
  "description": "Inquirer rawlist prompt",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "typings": "./dist/cjs/types/index.d.ts",
@@ -54,12 +54,12 @@
54
54
  "license": "MIT",
55
55
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/main/packages/rawlist/README.md",
56
56
  "dependencies": {
57
- "@inquirer/core": "^9.0.9",
58
- "@inquirer/type": "^1.5.2",
57
+ "@inquirer/core": "^9.1.0",
58
+ "@inquirer/type": "^1.5.3",
59
59
  "yoctocolors-cjs": "^2.1.2"
60
60
  },
61
61
  "devDependencies": {
62
- "@inquirer/testing": "^2.1.31"
62
+ "@inquirer/testing": "^2.1.32"
63
63
  },
64
64
  "scripts": {
65
65
  "tsc": "yarn run tsc:esm && yarn run tsc:cjs",
@@ -86,5 +86,5 @@
86
86
  }
87
87
  },
88
88
  "sideEffects": false,
89
- "gitHead": "056e26aa6497e0036864d032f9eb8d821de821e7"
89
+ "gitHead": "0c039599ef88fe9eb804fe083ee386ec906a856f"
90
90
  }