@inquirer/expand 2.1.21 → 2.2.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
@@ -86,13 +86,31 @@ const answer = await expand({
86
86
 
87
87
  ## Options
88
88
 
89
- | Property | Type | Required | Description |
90
- | -------- | ------------------------------------------------------ | -------- | ----------------------------------------------------------------------------------------- |
91
- | message | `string` | yes | The question to ask |
92
- | choices | `Array<{ key: string, name: string, value?: string }>` | yes | Array of the different allowed choices. The `h`/help option is always provided by default |
93
- | default | `string` | no | Default choices to be selected. (value must be one of the choices `key`) |
94
- | expanded | `boolean` | no | Expand the choices by default |
95
- | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
89
+ | Property | Type | Required | Description |
90
+ | -------- | ----------------------- | -------- | ----------------------------------------------------------------------------------------- |
91
+ | message | `string` | yes | The question to ask |
92
+ | choices | `Choice[]` | yes | Array of the different allowed choices. The `h`/help option is always provided by default |
93
+ | default | `string` | no | Default choices to be selected. (value must be one of the choices `key`) |
94
+ | expanded | `boolean` | no | Expand the choices by default |
95
+ | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
96
+
97
+ ### `Choice` object
98
+
99
+ The `Choice` object is typed as
100
+
101
+ ```ts
102
+ type Choice<Value> = {
103
+ value: Value;
104
+ name?: string;
105
+ key: string;
106
+ };
107
+ ```
108
+
109
+ Here's each property:
110
+
111
+ - `value`: The value is what will be returned by `await expand()`.
112
+ - `name`: The string displayed in the choice list. It'll default to the stringify `value`.
113
+ - `key`: The input the use must provide to select the choice. Must be a lowercase single alpha-numeric character string.
96
114
 
97
115
  ## Theming
98
116
 
package/dist/cjs/index.js CHANGED
@@ -5,26 +5,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const core_1 = require("@inquirer/core");
7
7
  const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs"));
8
+ function normalizeChoices(choices) {
9
+ return choices.map((choice) => {
10
+ const name = 'name' in choice ? choice.name : String(choice.value);
11
+ const value = 'value' in choice ? choice.value : name;
12
+ return {
13
+ value: value,
14
+ name,
15
+ key: choice.key.toLowerCase(),
16
+ };
17
+ });
18
+ }
8
19
  const helpChoice = {
9
20
  key: 'h',
10
21
  name: 'Help, list all options',
11
22
  value: undefined,
12
23
  };
13
- function getChoiceKey(choice, key) {
14
- if (key === 'name') {
15
- if ('name' in choice)
16
- return choice.name;
17
- return choice.value;
18
- }
19
- if ('value' in choice)
20
- return choice.value;
21
- return choice.name;
22
- }
23
24
  exports.default = (0, core_1.createPrompt)((config, done) => {
24
- const { choices, default: defaultKey = 'h', expanded: defaultExpandState = false, } = config;
25
+ var _a;
26
+ const { default: defaultKey = 'h' } = config;
27
+ const choices = (0, core_1.useMemo)(() => normalizeChoices(config.choices), [config.choices]);
25
28
  const [status, setStatus] = (0, core_1.useState)('pending');
26
29
  const [value, setValue] = (0, core_1.useState)('');
27
- const [expanded, setExpanded] = (0, core_1.useState)(defaultExpandState);
30
+ const [expanded, setExpanded] = (0, core_1.useState)((_a = config.expanded) !== null && _a !== void 0 ? _a : false);
28
31
  const [errorMsg, setError] = (0, core_1.useState)();
29
32
  const theme = (0, core_1.makeTheme)(config.theme);
30
33
  const prefix = (0, core_1.usePrefix)({ theme });
@@ -37,10 +40,10 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
37
40
  else {
38
41
  const selectedChoice = choices.find(({ key }) => key === answer);
39
42
  if (selectedChoice) {
40
- const finalValue = getChoiceKey(selectedChoice, 'value');
41
- setValue(finalValue);
42
43
  setStatus('done');
43
- done(finalValue);
44
+ // Set the value as we might've selected the default one.
45
+ setValue(answer);
46
+ done(selectedChoice.value);
44
47
  }
45
48
  else if (value === '') {
46
49
  setError('Please input a value');
@@ -57,8 +60,9 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
57
60
  });
58
61
  const message = theme.style.message(config.message);
59
62
  if (status === 'done') {
60
- // TODO: `value` should be the display name instead of the raw value.
61
- return `${prefix} ${message} ${theme.style.answer(value)}`;
63
+ // If the prompt is done, it's safe to assume there is a selected value.
64
+ const selectedChoice = choices.find(({ key }) => key === value);
65
+ return `${prefix} ${message} ${theme.style.answer(selectedChoice.name)}`;
62
66
  }
63
67
  const allChoices = expanded ? choices : [...choices, helpChoice];
64
68
  // Collapsed display style
@@ -77,7 +81,7 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
77
81
  shortChoices = '';
78
82
  longChoices = allChoices
79
83
  .map((choice) => {
80
- const line = ` ${choice.key}) ${getChoiceKey(choice, 'name')}`;
84
+ const line = ` ${choice.key}) ${choice.name}`;
81
85
  if (choice.key === value.toLowerCase()) {
82
86
  return theme.style.highlight(line);
83
87
  }
@@ -88,7 +92,7 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
88
92
  let helpTip = '';
89
93
  const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
90
94
  if (currentOption) {
91
- helpTip = `${yoctocolors_cjs_1.default.cyan('>>')} ${getChoiceKey(currentOption, 'name')}`;
95
+ helpTip = `${yoctocolors_cjs_1.default.cyan('>>')} ${currentOption.name}`;
92
96
  }
93
97
  let error = '';
94
98
  if (errorMsg) {
@@ -1,22 +1,22 @@
1
1
  import { type Theme } from '@inquirer/core';
2
2
  import type { PartialDeep } from '@inquirer/type';
3
- type ExpandChoice = {
4
- key: string;
5
- name: string;
6
- } | {
7
- key: string;
8
- value: string;
3
+ type Key = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
4
+ type Choice<Value> = {
5
+ key: Key;
6
+ value: Value;
9
7
  } | {
10
- key: string;
8
+ key: Key;
11
9
  name: string;
12
- value: string;
10
+ value: Value;
13
11
  };
14
- type ExpandConfig = {
12
+ declare const _default: <Value>(config: {
15
13
  message: string;
16
- choices: ReadonlyArray<ExpandChoice>;
17
- default?: string;
18
- expanded?: boolean;
19
- theme?: PartialDeep<Theme>;
20
- };
21
- declare const _default: import("@inquirer/type").Prompt<string, ExpandConfig>;
14
+ choices: readonly {
15
+ key: Key;
16
+ name: string;
17
+ }[] | readonly Choice<Value>[];
18
+ default?: (Key | "h") | undefined;
19
+ expanded?: boolean | undefined;
20
+ theme?: PartialDeep<Theme> | undefined;
21
+ }, context?: import("@inquirer/type").Context) => import("@inquirer/type").CancelablePromise<Value>;
22
22
  export default _default;
@@ -1,25 +1,27 @@
1
- import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, makeTheme, } from '@inquirer/core';
1
+ import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, makeTheme, } from '@inquirer/core';
2
2
  import colors from 'yoctocolors-cjs';
3
+ function normalizeChoices(choices) {
4
+ return choices.map((choice) => {
5
+ const name = 'name' in choice ? choice.name : String(choice.value);
6
+ const value = 'value' in choice ? choice.value : name;
7
+ return {
8
+ value: value,
9
+ name,
10
+ key: choice.key.toLowerCase(),
11
+ };
12
+ });
13
+ }
3
14
  const helpChoice = {
4
15
  key: 'h',
5
16
  name: 'Help, list all options',
6
17
  value: undefined,
7
18
  };
8
- function getChoiceKey(choice, key) {
9
- if (key === 'name') {
10
- if ('name' in choice)
11
- return choice.name;
12
- return choice.value;
13
- }
14
- if ('value' in choice)
15
- return choice.value;
16
- return choice.name;
17
- }
18
19
  export default createPrompt((config, done) => {
19
- const { choices, default: defaultKey = 'h', expanded: defaultExpandState = false, } = config;
20
+ const { default: defaultKey = 'h' } = config;
21
+ const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
20
22
  const [status, setStatus] = useState('pending');
21
23
  const [value, setValue] = useState('');
22
- const [expanded, setExpanded] = useState(defaultExpandState);
24
+ const [expanded, setExpanded] = useState(config.expanded ?? false);
23
25
  const [errorMsg, setError] = useState();
24
26
  const theme = makeTheme(config.theme);
25
27
  const prefix = usePrefix({ theme });
@@ -32,10 +34,10 @@ export default createPrompt((config, done) => {
32
34
  else {
33
35
  const selectedChoice = choices.find(({ key }) => key === answer);
34
36
  if (selectedChoice) {
35
- const finalValue = getChoiceKey(selectedChoice, 'value');
36
- setValue(finalValue);
37
37
  setStatus('done');
38
- done(finalValue);
38
+ // Set the value as we might've selected the default one.
39
+ setValue(answer);
40
+ done(selectedChoice.value);
39
41
  }
40
42
  else if (value === '') {
41
43
  setError('Please input a value');
@@ -52,8 +54,9 @@ export default createPrompt((config, done) => {
52
54
  });
53
55
  const message = theme.style.message(config.message);
54
56
  if (status === 'done') {
55
- // TODO: `value` should be the display name instead of the raw value.
56
- return `${prefix} ${message} ${theme.style.answer(value)}`;
57
+ // If the prompt is done, it's safe to assume there is a selected value.
58
+ const selectedChoice = choices.find(({ key }) => key === value);
59
+ return `${prefix} ${message} ${theme.style.answer(selectedChoice.name)}`;
57
60
  }
58
61
  const allChoices = expanded ? choices : [...choices, helpChoice];
59
62
  // Collapsed display style
@@ -72,7 +75,7 @@ export default createPrompt((config, done) => {
72
75
  shortChoices = '';
73
76
  longChoices = allChoices
74
77
  .map((choice) => {
75
- const line = ` ${choice.key}) ${getChoiceKey(choice, 'name')}`;
78
+ const line = ` ${choice.key}) ${choice.name}`;
76
79
  if (choice.key === value.toLowerCase()) {
77
80
  return theme.style.highlight(line);
78
81
  }
@@ -83,7 +86,7 @@ export default createPrompt((config, done) => {
83
86
  let helpTip = '';
84
87
  const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
85
88
  if (currentOption) {
86
- helpTip = `${colors.cyan('>>')} ${getChoiceKey(currentOption, 'name')}`;
89
+ helpTip = `${colors.cyan('>>')} ${currentOption.name}`;
87
90
  }
88
91
  let error = '';
89
92
  if (errorMsg) {
@@ -1,22 +1,22 @@
1
1
  import { type Theme } from '@inquirer/core';
2
2
  import type { PartialDeep } from '@inquirer/type';
3
- type ExpandChoice = {
4
- key: string;
5
- name: string;
6
- } | {
7
- key: string;
8
- value: string;
3
+ type Key = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
4
+ type Choice<Value> = {
5
+ key: Key;
6
+ value: Value;
9
7
  } | {
10
- key: string;
8
+ key: Key;
11
9
  name: string;
12
- value: string;
10
+ value: Value;
13
11
  };
14
- type ExpandConfig = {
12
+ declare const _default: <Value>(config: {
15
13
  message: string;
16
- choices: ReadonlyArray<ExpandChoice>;
17
- default?: string;
18
- expanded?: boolean;
19
- theme?: PartialDeep<Theme>;
20
- };
21
- declare const _default: import("@inquirer/type").Prompt<string, ExpandConfig>;
14
+ choices: readonly {
15
+ key: Key;
16
+ name: string;
17
+ }[] | readonly Choice<Value>[];
18
+ default?: (Key | "h") | undefined;
19
+ expanded?: boolean | undefined;
20
+ theme?: PartialDeep<Theme> | undefined;
21
+ }, context?: import("@inquirer/type").Context) => import("@inquirer/type").CancelablePromise<Value>;
22
22
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/expand",
3
- "version": "2.1.21",
3
+ "version": "2.2.0",
4
4
  "description": "Inquirer checkbox 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/expand/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
  }