@ahoo-wang/fetcher-react 2.12.9 → 2.13.1

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
@@ -31,6 +31,7 @@ robust data fetching capabilities.
31
31
  - [usePromiseState Hook](#usepromisestate-hook)
32
32
  - [useRequestId Hook](#userequestid-hook)
33
33
  - [useLatest Hook](#uselatest-hook)
34
+ - [useRefs Hook](#userefs-hook)
34
35
  - [useKeyStorage Hook](#usekeystorage-hook)
35
36
  - [Wow Query Hooks](#wow-query-hooks)
36
37
  - [useListQuery Hook](#uselistquery-hook)
@@ -282,6 +283,39 @@ const MyComponent = () => {
282
283
  };
283
284
  ```
284
285
 
286
+ ### useRefs Hook
287
+
288
+ The `useRefs` hook provides a Map-like interface for managing multiple React refs dynamically. It allows registering, retrieving, and managing refs by key, with automatic cleanup on component unmount.
289
+
290
+ ```typescript jsx
291
+ import { useRefs } from '@ahoo-wang/fetcher-react';
292
+
293
+ const MyComponent = () => {
294
+ const refs = useRefs<HTMLDivElement>();
295
+
296
+ const handleFocus = (key: string) => {
297
+ const element = refs.get(key);
298
+ element?.focus();
299
+ };
300
+
301
+ return (
302
+ <div>
303
+ <div ref={refs.register('first')} tabIndex={0}>First Element</div>
304
+ <div ref={refs.register('second')} tabIndex={0}>Second Element</div>
305
+ <button onClick={() => handleFocus('first')}>Focus First</button>
306
+ <button onClick={() => handleFocus('second')}>Focus Second</button>
307
+ </div>
308
+ );
309
+ };
310
+ ```
311
+
312
+ Key features:
313
+
314
+ - **Dynamic Registration**: Register refs with string, number, or symbol keys
315
+ - **Map-like API**: Full Map interface with get, set, has, delete, etc.
316
+ - **Automatic Cleanup**: Refs are cleared when component unmounts
317
+ - **Type Safety**: Full TypeScript support for ref types
318
+
285
319
  ### useKeyStorage Hook
286
320
 
287
321
  The `useKeyStorage` hook provides reactive state management for a KeyStorage instance. It subscribes to storage changes and returns the current value along with a setter function. Optionally accepts a default value to use when the storage is empty.
@@ -1369,6 +1403,39 @@ callbacks.
1369
1403
 
1370
1404
  A ref object with a `current` property containing the latest value
1371
1405
 
1406
+ ### useRefs
1407
+
1408
+ ```typescript
1409
+ function useRefs<T>(): UseRefsReturn<T>;
1410
+ ```
1411
+
1412
+ A React hook for managing multiple refs with a Map-like interface, allowing dynamic registration and retrieval of refs by key.
1413
+
1414
+ **Type Parameters:**
1415
+
1416
+ - `T`: The type of the ref instances (e.g., HTMLElement)
1417
+
1418
+ **Returns:**
1419
+
1420
+ An object implementing `UseRefsReturn<T>` with:
1421
+
1422
+ - `register(key: RefKey): (instance: T | null) => void` - Returns a callback to register/unregister a ref
1423
+ - `get(key: RefKey): T | undefined` - Get a ref by key
1424
+ - `set(key: RefKey, value: T): void` - Set a ref value
1425
+ - `has(key: RefKey): boolean` - Check if key exists
1426
+ - `delete(key: RefKey): boolean` - Delete a ref by key
1427
+ - `clear(): void` - Clear all refs
1428
+ - `size: number` - Number of refs
1429
+ - `keys(): IterableIterator<RefKey>` - Iterator over keys
1430
+ - `values(): IterableIterator<T>` - Iterator over values
1431
+ - `entries(): IterableIterator<[RefKey, T]>` - Iterator over entries
1432
+ - `Symbol.iterator`: Iterator for for...of loops
1433
+
1434
+ **Related Types:**
1435
+
1436
+ - `RefKey = string | number | symbol`
1437
+ - `UseRefsReturn<T> extends Iterable<[RefKey, T]>`
1438
+
1372
1439
  ### useKeyStorage
1373
1440
 
1374
1441
  ```typescript
package/README.zh-CN.md CHANGED
@@ -30,6 +30,7 @@
30
30
  - [usePromiseState Hook](#usepromisestate-hook)
31
31
  - [useRequestId Hook](#userequestid-hook)
32
32
  - [useLatest Hook](#uselatest-hook)
33
+ - [useRefs Hook](#userefs-hook)
33
34
  - [useKeyStorage Hook](#usekeystorage-hook)
34
35
  - [Wow 查询 Hooks](#wow-查询-hooks)
35
36
  - [useListQuery Hook](#uselistquery-hook)
@@ -277,6 +278,39 @@ const MyComponent = () => {
277
278
  };
278
279
  ```
279
280
 
281
+ ### useRefs Hook
282
+
283
+ `useRefs` hook 提供 Map-like 接口用于动态管理多个 React refs。它允许通过键注册、检索和管理 refs,并在组件卸载时自动清理。
284
+
285
+ ```typescript jsx
286
+ import { useRefs } from '@ahoo-wang/fetcher-react';
287
+
288
+ const MyComponent = () => {
289
+ const refs = useRefs<HTMLDivElement>();
290
+
291
+ const handleFocus = (key: string) => {
292
+ const element = refs.get(key);
293
+ element?.focus();
294
+ };
295
+
296
+ return (
297
+ <div>
298
+ <div ref={refs.register('first')} tabIndex={0}>第一个元素</div>
299
+ <div ref={refs.register('second')} tabIndex={0}>第二个元素</div>
300
+ <button onClick={() => handleFocus('first')}>聚焦第一个</button>
301
+ <button onClick={() => handleFocus('second')}>聚焦第二个</button>
302
+ </div>
303
+ );
304
+ };
305
+ ```
306
+
307
+ 关键特性:
308
+
309
+ - **动态注册**: 使用字符串、数字或符号键注册 refs
310
+ - **Map-like API**: 完整的 Map 接口,包括 get、set、has、delete 等
311
+ - **自动清理**: 组件卸载时自动清空 refs
312
+ - **类型安全**: 完整的 TypeScript 支持
313
+
280
314
  ### useKeyStorage Hook
281
315
 
282
316
  `useKeyStorage` hook 为 KeyStorage 实例提供反应式状态管理。它订阅存储变化并返回当前值以及设置函数。可选接受默认值以在存储为空时使用。
@@ -738,6 +772,39 @@ function useLatest<T>(value: T): { current: T };
738
772
 
739
773
  包含 `current` 属性(包含最新值)的 ref 对象
740
774
 
775
+ ### useRefs
776
+
777
+ ```typescript
778
+ function useRefs<T>(): UseRefsReturn<T>;
779
+ ```
780
+
781
+ React hook,用于使用 Map-like 接口管理多个 refs,允许通过键动态注册和检索 refs。
782
+
783
+ **类型参数:**
784
+
785
+ - `T`: ref 实例的类型(例如 HTMLElement)
786
+
787
+ **返回值:**
788
+
789
+ 实现 `UseRefsReturn<T>` 的对象,具有:
790
+
791
+ - `register(key: RefKey): (instance: T | null) => void` - 返回用于注册/注销 ref 的回调
792
+ - `get(key: RefKey): T | undefined` - 通过键获取 ref
793
+ - `set(key: RefKey, value: T): void` - 设置 ref 值
794
+ - `has(key: RefKey): boolean` - 检查键是否存在
795
+ - `delete(key: RefKey): boolean` - 通过键删除 ref
796
+ - `clear(): void` - 清空所有 refs
797
+ - `size: number` - refs 数量
798
+ - `keys(): IterableIterator<RefKey>` - 键的迭代器
799
+ - `values(): IterableIterator<T>` - 值的迭代器
800
+ - `entries(): IterableIterator<[RefKey, T]>` - 条目的迭代器
801
+ - `Symbol.iterator`: for...of 循环的迭代器
802
+
803
+ **相关类型:**
804
+
805
+ - `RefKey = string | number | symbol`
806
+ - `UseRefsReturn<T> extends Iterable<[RefKey, T]>`
807
+
741
808
  ### useKeyStorage
742
809
 
743
810
  ```typescript jsx
@@ -3,4 +3,5 @@ export * from './usePromiseState';
3
3
  export * from './useRequestId';
4
4
  export * from './useLatest';
5
5
  export * from './useMounted';
6
+ export * from './useRefs';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}
@@ -1,4 +1,4 @@
1
- import { MutableRefObject } from 'react';
1
+ import { RefObject } from 'react';
2
2
  /**
3
3
  * A React hook that returns a ref containing the latest value, useful for accessing the current value in async callbacks.
4
4
  *
@@ -29,5 +29,5 @@ import { MutableRefObject } from 'react';
29
29
  * };
30
30
  * ```
31
31
  */
32
- export declare function useLatest<T>(value: T): MutableRefObject<T>;
32
+ export declare function useLatest<T>(value: T): RefObject<T>;
33
33
  //# sourceMappingURL=useLatest.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useLatest.d.ts","sourceRoot":"","sources":["../../src/core/useLatest.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,gBAAgB,EAAU,MAAM,OAAO,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAI1D"}
1
+ {"version":3,"file":"useLatest.d.ts","sourceRoot":"","sources":["../../src/core/useLatest.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,SAAS,EAAU,MAAM,OAAO,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAInD"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The key type for refs, supporting string, number, or symbol.
3
+ */
4
+ export type RefKey = string | number | symbol;
5
+ /**
6
+ * Return type of useRefs hook, providing Map-like interface for managing refs.
7
+ * @template T - The type of the ref instances.
8
+ */
9
+ export interface UseRefsReturn<T> extends Iterable<[RefKey, T]> {
10
+ register: (key: RefKey) => (instance: T | null) => void;
11
+ get: (key: RefKey) => T | undefined;
12
+ set: (key: RefKey, value: T) => void;
13
+ delete: (key: RefKey) => boolean;
14
+ has: (key: RefKey) => boolean;
15
+ clear: () => void;
16
+ readonly size: number;
17
+ keys: () => IterableIterator<RefKey>;
18
+ values: () => IterableIterator<T>;
19
+ entries: () => IterableIterator<[RefKey, T]>;
20
+ }
21
+ /**
22
+ * A React hook for managing multiple refs with a Map-like interface.
23
+ * Allows dynamic registration and retrieval of refs by key, with automatic cleanup on unmount.
24
+ *
25
+ * @template T - The type of the ref instances (e.g., HTMLElement).
26
+ * @returns An object with Map methods and a register function for managing refs.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * const refs = useRefs<HTMLDivElement>();
31
+ *
32
+ * // Register a ref
33
+ * const refCallback = refs.register('myDiv');
34
+ * <div ref={refCallback} />
35
+ *
36
+ * // Access the ref
37
+ * const element = refs.get('myDiv');
38
+ * ```
39
+ */
40
+ export declare function useRefs<T>(): UseRefsReturn<T>;
41
+ //# sourceMappingURL=useRefs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRefs.d.ts","sourceRoot":"","sources":["../../src/core/useRefs.ts"],"names":[],"mappings":"AAeA;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAE9C;;;GAGG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,CAAE,SAAQ,QAAQ,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAC7D,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,QAAQ,EAAE,CAAC,GAAG,IAAI,KAAK,IAAI,CAAC;IACxD,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,GAAG,SAAS,CAAC;IACpC,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IACrC,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;IACjC,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;IAC9B,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,MAAM,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,EAAE,MAAM,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAClC,OAAO,EAAE,MAAM,gBAAgB,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;CAC9C;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,OAAO,CAAC,CAAC,KAAK,aAAa,CAAC,CAAC,CAAC,CA+C7C"}