@inquirer/expand 2.1.22 → 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
@@ -86,13 +86,33 @@ 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
+ `Separator` objects can be used in the `choices` array 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.
98
+
99
+ ### `Choice` object
100
+
101
+ The `Choice` object is typed as
102
+
103
+ ```ts
104
+ type Choice<Value> = {
105
+ value: Value;
106
+ name?: string;
107
+ key: string;
108
+ };
109
+ ```
110
+
111
+ Here's each property:
112
+
113
+ - `value`: The value is what will be returned by `await expand()`.
114
+ - `name`: The string displayed in the choice list. It'll default to the stringify `value`.
115
+ - `key`: The input the use must provide to select the choice. Must be a lowercase single alpha-numeric character string.
96
116
 
97
117
  ## Theming
98
118
 
package/dist/cjs/index.js CHANGED
@@ -3,28 +3,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Separator = void 0;
6
7
  const core_1 = require("@inquirer/core");
7
8
  const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs"));
9
+ function normalizeChoices(choices) {
10
+ return choices.map((choice) => {
11
+ if (core_1.Separator.isSeparator(choice)) {
12
+ return choice;
13
+ }
14
+ const name = 'name' in choice ? choice.name : String(choice.value);
15
+ const value = 'value' in choice ? choice.value : name;
16
+ return {
17
+ value: value,
18
+ name,
19
+ key: choice.key.toLowerCase(),
20
+ };
21
+ });
22
+ }
8
23
  const helpChoice = {
9
24
  key: 'h',
10
25
  name: 'Help, list all options',
11
26
  value: undefined,
12
27
  };
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
28
  exports.default = (0, core_1.createPrompt)((config, done) => {
24
- const { choices, default: defaultKey = 'h', expanded: defaultExpandState = false, } = config;
29
+ var _a;
30
+ const { default: defaultKey = 'h' } = config;
31
+ const choices = (0, core_1.useMemo)(() => normalizeChoices(config.choices), [config.choices]);
25
32
  const [status, setStatus] = (0, core_1.useState)('pending');
26
33
  const [value, setValue] = (0, core_1.useState)('');
27
- const [expanded, setExpanded] = (0, core_1.useState)(defaultExpandState);
34
+ const [expanded, setExpanded] = (0, core_1.useState)((_a = config.expanded) !== null && _a !== void 0 ? _a : false);
28
35
  const [errorMsg, setError] = (0, core_1.useState)();
29
36
  const theme = (0, core_1.makeTheme)(config.theme);
30
37
  const prefix = (0, core_1.usePrefix)({ theme });
@@ -35,12 +42,12 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
35
42
  setExpanded(true);
36
43
  }
37
44
  else {
38
- const selectedChoice = choices.find(({ key }) => key === answer);
45
+ const selectedChoice = choices.find((choice) => !core_1.Separator.isSeparator(choice) && choice.key === answer);
39
46
  if (selectedChoice) {
40
- const finalValue = getChoiceKey(selectedChoice, 'value');
41
- setValue(finalValue);
42
47
  setStatus('done');
43
- done(finalValue);
48
+ // Set the value as we might've selected the default one.
49
+ setValue(answer);
50
+ done(selectedChoice.value);
44
51
  }
45
52
  else if (value === '') {
46
53
  setError('Please input a value');
@@ -57,14 +64,17 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
57
64
  });
58
65
  const message = theme.style.message(config.message);
59
66
  if (status === 'done') {
60
- // TODO: `value` should be the display name instead of the raw value.
61
- return `${prefix} ${message} ${theme.style.answer(value)}`;
67
+ // If the prompt is done, it's safe to assume there is a selected value.
68
+ const selectedChoice = choices.find((choice) => !core_1.Separator.isSeparator(choice) && choice.key === value.toLowerCase());
69
+ return `${prefix} ${message} ${theme.style.answer(selectedChoice.name)}`;
62
70
  }
63
71
  const allChoices = expanded ? choices : [...choices, helpChoice];
64
72
  // Collapsed display style
65
73
  let longChoices = '';
66
74
  let shortChoices = allChoices
67
75
  .map((choice) => {
76
+ if (core_1.Separator.isSeparator(choice))
77
+ return '';
68
78
  if (choice.key === defaultKey) {
69
79
  return choice.key.toUpperCase();
70
80
  }
@@ -77,7 +87,10 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
77
87
  shortChoices = '';
78
88
  longChoices = allChoices
79
89
  .map((choice) => {
80
- const line = ` ${choice.key}) ${getChoiceKey(choice, 'name')}`;
90
+ if (core_1.Separator.isSeparator(choice)) {
91
+ return ` ${choice.separator}`;
92
+ }
93
+ const line = ` ${choice.key}) ${choice.name}`;
81
94
  if (choice.key === value.toLowerCase()) {
82
95
  return theme.style.highlight(line);
83
96
  }
@@ -86,9 +99,9 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
86
99
  .join('\n');
87
100
  }
88
101
  let helpTip = '';
89
- const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
102
+ const currentOption = choices.find((choice) => !core_1.Separator.isSeparator(choice) && choice.key === value.toLowerCase());
90
103
  if (currentOption) {
91
- helpTip = `${yoctocolors_cjs_1.default.cyan('>>')} ${getChoiceKey(currentOption, 'name')}`;
104
+ helpTip = `${yoctocolors_cjs_1.default.cyan('>>')} ${currentOption.name}`;
92
105
  }
93
106
  let error = '';
94
107
  if (errorMsg) {
@@ -99,3 +112,5 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
99
112
  [longChoices, helpTip, error].filter(Boolean).join('\n'),
100
113
  ];
101
114
  });
115
+ var core_2 = require("@inquirer/core");
116
+ Object.defineProperty(exports, "Separator", { enumerable: true, get: function () { return core_2.Separator; } });
@@ -1,22 +1,23 @@
1
- import { type Theme } from '@inquirer/core';
1
+ import { Separator, 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 (Separator | 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;
23
+ export { Separator } from '@inquirer/core';
@@ -1,25 +1,30 @@
1
- import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, makeTheme, } from '@inquirer/core';
1
+ import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, makeTheme, Separator, } from '@inquirer/core';
2
2
  import colors from 'yoctocolors-cjs';
3
+ function normalizeChoices(choices) {
4
+ return choices.map((choice) => {
5
+ if (Separator.isSeparator(choice)) {
6
+ return choice;
7
+ }
8
+ const name = 'name' in choice ? choice.name : String(choice.value);
9
+ const value = 'value' in choice ? choice.value : name;
10
+ return {
11
+ value: value,
12
+ name,
13
+ key: choice.key.toLowerCase(),
14
+ };
15
+ });
16
+ }
3
17
  const helpChoice = {
4
18
  key: 'h',
5
19
  name: 'Help, list all options',
6
20
  value: undefined,
7
21
  };
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
22
  export default createPrompt((config, done) => {
19
- const { choices, default: defaultKey = 'h', expanded: defaultExpandState = false, } = config;
23
+ const { default: defaultKey = 'h' } = config;
24
+ const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
20
25
  const [status, setStatus] = useState('pending');
21
26
  const [value, setValue] = useState('');
22
- const [expanded, setExpanded] = useState(defaultExpandState);
27
+ const [expanded, setExpanded] = useState(config.expanded ?? false);
23
28
  const [errorMsg, setError] = useState();
24
29
  const theme = makeTheme(config.theme);
25
30
  const prefix = usePrefix({ theme });
@@ -30,12 +35,12 @@ export default createPrompt((config, done) => {
30
35
  setExpanded(true);
31
36
  }
32
37
  else {
33
- const selectedChoice = choices.find(({ key }) => key === answer);
38
+ const selectedChoice = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === answer);
34
39
  if (selectedChoice) {
35
- const finalValue = getChoiceKey(selectedChoice, 'value');
36
- setValue(finalValue);
37
40
  setStatus('done');
38
- done(finalValue);
41
+ // Set the value as we might've selected the default one.
42
+ setValue(answer);
43
+ done(selectedChoice.value);
39
44
  }
40
45
  else if (value === '') {
41
46
  setError('Please input a value');
@@ -52,14 +57,17 @@ export default createPrompt((config, done) => {
52
57
  });
53
58
  const message = theme.style.message(config.message);
54
59
  if (status === 'done') {
55
- // TODO: `value` should be the display name instead of the raw value.
56
- return `${prefix} ${message} ${theme.style.answer(value)}`;
60
+ // If the prompt is done, it's safe to assume there is a selected value.
61
+ const selectedChoice = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === value.toLowerCase());
62
+ return `${prefix} ${message} ${theme.style.answer(selectedChoice.name)}`;
57
63
  }
58
64
  const allChoices = expanded ? choices : [...choices, helpChoice];
59
65
  // Collapsed display style
60
66
  let longChoices = '';
61
67
  let shortChoices = allChoices
62
68
  .map((choice) => {
69
+ if (Separator.isSeparator(choice))
70
+ return '';
63
71
  if (choice.key === defaultKey) {
64
72
  return choice.key.toUpperCase();
65
73
  }
@@ -72,7 +80,10 @@ export default createPrompt((config, done) => {
72
80
  shortChoices = '';
73
81
  longChoices = allChoices
74
82
  .map((choice) => {
75
- const line = ` ${choice.key}) ${getChoiceKey(choice, 'name')}`;
83
+ if (Separator.isSeparator(choice)) {
84
+ return ` ${choice.separator}`;
85
+ }
86
+ const line = ` ${choice.key}) ${choice.name}`;
76
87
  if (choice.key === value.toLowerCase()) {
77
88
  return theme.style.highlight(line);
78
89
  }
@@ -81,9 +92,9 @@ export default createPrompt((config, done) => {
81
92
  .join('\n');
82
93
  }
83
94
  let helpTip = '';
84
- const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
95
+ const currentOption = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === value.toLowerCase());
85
96
  if (currentOption) {
86
- helpTip = `${colors.cyan('>>')} ${getChoiceKey(currentOption, 'name')}`;
97
+ helpTip = `${colors.cyan('>>')} ${currentOption.name}`;
87
98
  }
88
99
  let error = '';
89
100
  if (errorMsg) {
@@ -94,3 +105,4 @@ export default createPrompt((config, done) => {
94
105
  [longChoices, helpTip, error].filter(Boolean).join('\n'),
95
106
  ];
96
107
  });
108
+ export { Separator } from '@inquirer/core';
@@ -1,22 +1,23 @@
1
- import { type Theme } from '@inquirer/core';
1
+ import { Separator, 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 (Separator | 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;
23
+ export { Separator } from '@inquirer/core';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/expand",
3
- "version": "2.1.22",
3
+ "version": "2.3.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.10",
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": "273b16eeb96fab346854b9b92257c930d7ca7661"
89
+ "gitHead": "4937ea3a74152b59bf4198dbaa803119ed4ef8e2"
90
90
  }