@dune2/tools 1.0.1 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dune2/tools",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "i18n"
@@ -2,7 +2,7 @@
2
2
  import type { ComponentType, FC, PropsWithChildren } from "react";
3
3
  import React, { useContext } from "react";
4
4
 
5
- interface Params<T> {
5
+ interface Params<P, T> {
6
6
  /**
7
7
  * context 的名字 方便 debug
8
8
  */
@@ -10,7 +10,7 @@ interface Params<T> {
10
10
  /**
11
11
  * 传入一个 hooks 返回值会给 context 的 value
12
12
  */
13
- useValueHooks: () => T;
13
+ useValueHooks: (props: P) => T;
14
14
  defaultValue?: T;
15
15
  }
16
16
 
@@ -18,12 +18,12 @@ interface Params<T> {
18
18
  * 快速创建一个 context 和 Provider
19
19
  * 通过给定的 hooks 返回值创建 context 的 value
20
20
  */
21
- export function createStateContext<T>(params: Params<T>) {
21
+ export function createStateContext<P, T>(params: Params<P, T>) {
22
22
  const { useValueHooks, defaultValue, name } = params;
23
23
  const Context = React.createContext(defaultValue as T);
24
24
 
25
- const Provider: FC<PropsWithChildren> = (props) => {
26
- const value = useValueHooks();
25
+ const Provider: FC<PropsWithChildren<P>> = (props) => {
26
+ const value = useValueHooks(props);
27
27
  return <Context.Provider value={value}>{props.children}</Context.Provider>;
28
28
  };
29
29
  Provider.displayName = `${name}Provider`;
@@ -32,10 +32,13 @@ export function createStateContext<T>(params: Params<T>) {
32
32
  return useContext(Context)!;
33
33
  }
34
34
 
35
- function withProvider<P extends object>(Comp: ComponentType<P>) {
36
- function WithProvider(props: P) {
35
+ function withProvider<C extends object>(
36
+ Comp: ComponentType<C>,
37
+ providerProps: P = {} as P,
38
+ ) {
39
+ function WithProvider(props: C) {
37
40
  return (
38
- <Provider>
41
+ <Provider {...providerProps}>
39
42
  <Comp {...props}></Comp>
40
43
  </Provider>
41
44
  );