@inquirer/core 11.2.0 → 12.0.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
@@ -49,9 +49,7 @@ const input = createPrompt((config, done) => {
49
49
  });
50
50
 
51
51
  // And it is then called as
52
- const answer = await input({
53
- /* config */
54
- });
52
+ const answer = await input({/* config */});
55
53
  ```
56
54
 
57
55
  ## Hooks
@@ -64,6 +62,14 @@ State lets a component “remember” information like user input. For example,
64
62
 
65
63
  `useState` declares a state variable that you can update directly.
66
64
 
65
+ The setter also accepts an updater function to compute the next state from the current one (mirroring React):
66
+
67
+ ```ts
68
+ const [index, setIndex] = useState(0);
69
+
70
+ setIndex((current) => current + 1);
71
+ ```
72
+
67
73
  ```ts
68
74
  import { createPrompt, useState } from '@inquirer/core';
69
75
 
@@ -2,17 +2,11 @@ import type { InquirerReadline } from '@inquirer/type';
2
2
  export declare function withHooks<T>(rl: InquirerReadline, cb: (cycle: (render: () => void) => void) => T): T;
3
3
  export declare function readline(): InquirerReadline;
4
4
  export declare function withUpdates<Args extends unknown[], R>(fn: (...args: Args) => R): (...args: Args) => R;
5
- type SetPointer<Value> = {
5
+ type Pointer<Value> = {
6
6
  get(): Value;
7
7
  set(value: Value): void;
8
- initialized: true;
8
+ initialized: boolean;
9
9
  };
10
- type UnsetPointer<Value> = {
11
- get(): void;
12
- set(value: Value): void;
13
- initialized: false;
14
- };
15
- type Pointer<Value> = SetPointer<Value> | UnsetPointer<Value>;
16
10
  export declare function withPointer<Value, ReturnValue>(cb: (pointer: Pointer<Value>) => ReturnValue): ReturnValue;
17
11
  export declare function handleChange(): void;
18
12
  export declare const effectScheduler: {
@@ -2,7 +2,7 @@ import { withPointer } from "./hook-engine.js";
2
2
  export function useMemo(fn, dependencies) {
3
3
  return withPointer((pointer) => {
4
4
  const prev = pointer.get();
5
- if (!prev ||
5
+ if (!pointer.initialized ||
6
6
  prev.dependencies.length !== dependencies.length ||
7
7
  prev.dependencies.some((dep, i) => dep !== dependencies[i])) {
8
8
  const value = fn();
@@ -1,4 +1,7 @@
1
1
  type NotFunction<T> = T extends (...args: never) => unknown ? never : T;
2
- export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
3
- export declare function useState<Value>(defaultValue?: NotFunction<Value> | (() => Value)): [Value | undefined, (newValue?: Value) => void];
2
+ type Reducer<Value> = (currentValue: Value) => Value;
3
+ type SetState<Value> = (newValue: NotFunction<Value> | Reducer<Value>) => void;
4
+ type OptionalSetState<Value> = (newValue?: NotFunction<Value> | Reducer<Value>) => void;
5
+ export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, SetState<Value>];
6
+ export declare function useState<Value>(defaultValue?: NotFunction<Value> | (() => Value)): [Value | undefined, OptionalSetState<Value | undefined>];
4
7
  export {};
@@ -3,12 +3,17 @@ import { withPointer, handleChange } from "./hook-engine.js";
3
3
  function isFactory(value) {
4
4
  return typeof value === 'function';
5
5
  }
6
+ function isReducer(value) {
7
+ return typeof value === 'function';
8
+ }
6
9
  export function useState(defaultValue) {
7
10
  return withPointer((pointer) => {
8
11
  const setState = AsyncResource.bind(function setState(newValue) {
12
+ const currentValue = pointer.get();
13
+ const nextValue = isReducer(newValue) ? newValue(currentValue) : newValue;
9
14
  // Noop if the value is still the same.
10
- if (pointer.get() !== newValue) {
11
- pointer.set(newValue);
15
+ if (!Object.is(currentValue, nextValue)) {
16
+ pointer.set(nextValue);
12
17
  // Trigger re-render
13
18
  handleChange();
14
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "11.2.0",
3
+ "version": "12.0.0",
4
4
  "description": "Core Inquirer prompt API",
5
5
  "keywords": [
6
6
  "answer",
@@ -67,17 +67,18 @@
67
67
  "tsc": "tsc"
68
68
  },
69
69
  "dependencies": {
70
- "@inquirer/ansi": "^2.0.6",
71
- "@inquirer/figures": "^2.0.6",
72
- "@inquirer/type": "^4.0.6",
70
+ "@inquirer/ansi": "^2.0.7",
71
+ "@inquirer/figures": "^2.0.8",
72
+ "@inquirer/type": "^4.0.7",
73
73
  "cli-width": "^4.1.0",
74
74
  "fast-wrap-ansi": "^0.2.0",
75
- "mute-stream": "^4.0.0",
75
+ "mute-stream": "^3.0.0",
76
76
  "signal-exit": "^4.1.0"
77
77
  },
78
78
  "devDependencies": {
79
+ "@repo/tsconfig": "0.0.0",
79
80
  "@types/mute-stream": "^0.0.4",
80
- "@types/node": "^25.5.2",
81
+ "@types/node": "^26.1.1",
81
82
  "typescript": "^6.0.2"
82
83
  },
83
84
  "peerDependencies": {
@@ -89,9 +90,9 @@
89
90
  }
90
91
  },
91
92
  "engines": {
92
- "node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
93
+ "node": ">=23.5.0 || ^22.13.0 || ^20.17.0"
93
94
  },
94
95
  "main": "./dist/index.js",
95
96
  "types": "./dist/index.d.ts",
96
- "gitHead": "5ca6d1101d5d3f8fb066cd5b389bccfdafbbe0c0"
97
+ "gitHead": "999706755afbdcae271f62decbd9bcd05560905b"
97
98
  }