Skip to content

Commit 714fa25

Browse files
committed
[FIX] various types problem
1 parent ea6b71a commit 714fa25

144 files changed

Lines changed: 302 additions & 285 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Go to [Demo](https://ndriadev.github.io/react-tools) to see and try all implemen
208208

209209
A state management hook implemented on Publish-Subscribe pattern. It allows components to subscribe to state changes and receive updates whenever the state is modified, providing a scalable and decoupled state management solution.__N.B.: to work properly, objects like Set, Map, Date or more generally objects without _Symbol.iterator_ must be treated as immutable__. [See demo](https://ndriadev.github.io/react-tools/#/hooks/state/createPubSubStore)
210210
```tsx
211-
createPubSubStore<T extends Record<string, unknown>, E extends Record<string, (store: T, ...args: any) => void>>(obj: T, mutatorsFn?: E, persist?: boolean): { getStore: () => T; mutateStore: (cb: (globStore: T) => void) => void; mutators: Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>, usePubSubStore: { (subscribe?: undefined): [T, (store: T | ((currStore: T) => T)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [C, (store: C | ((currStore: C) => C)) => void, () => C, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [T | C, (store: T | C | ((currStore: T) => T) | ((currStore: C) => C)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>] }}
211+
createPubSubStore<T extends object, E extends Record<string, (store: T, ...args: any) => void>>(obj: T, mutatorsFn?: E, persist?: boolean): { getStore: () => T; mutateStore: (cb: (globStore: T) => void) => void; mutators: Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>, usePubSubStore: { (subscribe?: undefined): [T, (store: T | ((currStore: T) => T)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [C, (store: C | ((currStore: C) => C)) => void, () => C, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [T | C, (store: T | C | ((currStore: T) => T) | ((currStore: C) => C)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>] }}
212212
```
213213
214214
### useArray
@@ -313,7 +313,7 @@ useStateHistoryGetter<T>(initialState: T | (() => T), capacity: number | "no-lim
313313
314314
Custom _useState_ hook that validates state on every update. [See demo](https://ndriadev.github.io/react-tools/#/hooks/state/useStateValidator)
315315
```tsx
316-
useStateValidator<T>(initialState: T | (() => T), validator: StateValidator<T>): [T, Dispatch<SetStateAction<T>>, T extends Record<string, unknown> ? {[k in keyof T]:{invalid: boolean, message?: string}} : {invalid: boolean, message?: string}]
316+
useStateValidator<T>(initialState: T | (() => T), validator: StateValidator<T>): [T, Dispatch<SetStateAction<T>>, T extends object ? {[k in keyof T]:{invalid: boolean, message?: string}} : {invalid: boolean, message?: string}]
317317
```
318318
319319
### useSyncExternalStore
@@ -795,7 +795,7 @@ useGeolocation({mode, locationOptions, onError}: { locationOptions?: PositionOpt
795795
796796
Hook to invoke a callback when the browser is idle. Refer to [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) in React. The __options__ parameter differs from _IdleRequestOptions_ type: it adds the possibility to pass another property __unsupportedBehavior__ to specify what do if requestIdleCallback is not supported. [See demo](https://ndriadev.github.io/react-tools/#/hooks/api-dom/useIdleCallback)
797797
```tsx
798-
useIdleCallback(cb: (deadline?: IdleDeadline | DOMHighResTimeStamp | void) => void, opts?: {timeout: number , unsupportedBehavior?: "animationFrame" | "timeout" | "immediatly" }): [() => void, () => void]
798+
useIdleCallback(cb: (deadline?: IdleDeadline | DOMHighResTimeStamp | void) => void, opts?: {timeout: number , unsupportedBehavior?: "animationFrame" | "timeout" | "immediatly" }): [boolean, () => void, () => void]
799799
```
800800
801801
### useInterval

apps/react-tools-demo/src/markdown/For.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ For = memo(<T extends unknown>({ of, children, filter, map, sort, elementKey, fa
4848
component properties object.
4949
> - __props.of__: _T[]_
5050
array of elements.
51-
> - __props.elementKey?__: _T extends Record<string,unknown> ? keyof T|((item:T)=> string|number) : never_
52-
if elements are objects, this prop is a key of the array elements or a function with one parameter which type is the type of the elements in __of__ prop and returns a string or a number, otherwise this prop is unsettable.
53-
> - __props.children__: _(item: T, index: T extends Record<string,unknown> ? number | T[keyof T] | string : number) => ReactNode_
51+
> - __props.elementKey?__: _T extends object ? keyof T | ((item: T) => string | number) : string | number | ((item: T) => string | number)_
52+
if the elements are objects, this prop is a key of the array elements or a function with one parameter which type is the type of the elements in __of__ prop and returns a string or a number, otherwise this prop can be the function described before, a string or a number.
53+
> - __props.children__: _(item: T, index: T extends object ? number | T[keyof T] | string : number) => ReactNode_
5454
it's a function that takes the current item as first argument and optionally a second argument that is number if element of array aren't object, otherwise it can be a number or the value of the element key specified in the _elementKey_ prop.
5555
> - __props.fallback?__: _ReactNode_
5656
optional element to render when _of_ prop is an empty array.

apps/react-tools-demo/src/markdown/createPubSubStore.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ export const CreatePubSubStore = () => {
8181
## API
8282

8383
```tsx
84-
createPubSubStore<T extends Record<string, unknown>, E extends Record<string, (store: T, ...args: any) => void>>(obj: T, mutatorsFn?: E, persist?: boolean): { getStore: () => T; mutateStore: (cb: (globStore: T) => void) => void; mutators: Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>, usePubSubStore: { (subscribe?: undefined): [T, (store: T | ((currStore: T) => T)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [C, (store: C | ((currStore: C) => C)) => void, () => C, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [T | C, (store: T | C | ((currStore: T) => T) | ((currStore: C) => C)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>] }}
84+
createPubSubStore<T extends object, E extends Record<string, (store: T, ...args: any) => void>>(obj: T, mutatorsFn?: E, persist?: boolean): { getStore: () => T; mutateStore: (cb: (globStore: T) => void) => void; mutators: Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>, usePubSubStore: { (subscribe?: undefined): [T, (store: T | ((currStore: T) => T)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [C, (store: C | ((currStore: C) => C)) => void, () => C, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>]; <C>(subscribe?: (store: T) => C): [T | C, (store: T | C | ((currStore: T) => T) | ((currStore: C) => C)) => void, () => T, Record<keyof E, (...args: ExtractTail<Parameters<E[keyof E]>>) => void>] }}
8585
```
8686
8787
> ### Params
8888
>
89-
> - __obj__: _T extends Record<string, unknown>_
89+
> - __obj__: _T extends object_
9090
Object that rapresent the initialState of the store.
9191
> - __mutatorsFn?__: _E extends Record<string, (store: T, ...args: any) => void>_
9292
Object that contains specified void function to mutate the store value, not the store itself, that receives the store as first parameter and other optional parameters.

apps/react-tools-demo/src/markdown/getBase64.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ getBase64<T>(target: string | Blob | ArrayBuffer | HTMLCanvasElement | HTMLImage
99

1010
> ### Params
1111
>
12-
> - __target__: _target: string | Blob | ArrayBuffer | HTMLCanvasElement | HTMLImageElement | T | T[]_
12+
> - __target__: _string | Blob | ArrayBuffer | HTMLCanvasElement | HTMLImageElement | T | T[]_
1313
> - __options?__: _ToDataURLOptions | UseBase64ObjectOptions<T>_
1414
>
1515

apps/react-tools-demo/src/markdown/useBattery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const UseBattery = () => {
1010
return (<div style={{ textAlign: "center" }}>
1111
{
1212
Object.keys(status).map(el => (
13-
<p key={el}>{el}: {status[el as keyof typeof status]}</p>
13+
<p key={el}>{el}: {el === "isSupported" ? status.isSupported ? "Yes" : "No": status[el as keyof typeof status]}</p>
1414
))
1515
}
1616
</div>)

apps/react-tools-demo/src/markdown/useIdleCallback.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Hook to invoke a callback when the browser is idle. Refer to [requestIdleCallbac
77
export const UseIdleCallback = () => {
88
const [iterations, setIterations] = useState(0);
99
const [log, setLog] = useState("");
10-
const [invoke] = useIdleCallback(() => setLog("RequestIdleCallback executed"));
10+
const [isSupported, invoke] = useIdleCallback(() => setLog("RequestIdleCallback executed"));
1111

1212
const start = async() => {
1313
invoke();
@@ -20,6 +20,7 @@ export const UseIdleCallback = () => {
2020
setIterations(0);
2121
}
2222
return (<div>
23+
<p>Supported: {isSupported ? "Yes" : "No"}</p>
2324
<p>Iterations are: {iterations}</p>
2425
<p>Log is: {log}</p>
2526
<button onClick={start}>START</button>
@@ -38,7 +39,7 @@ export const UseIdleCallback = () => {
3839
## API
3940

4041
```tsx
41-
useIdleCallback(cb: (deadline?: IdleDeadline | DOMHighResTimeStamp | void) => void, opts?: {timeout: number , unsupportedBehavior?: "animationFrame" | "timeout" | "immediatly" }): [() => void, () => void]
42+
useIdleCallback(cb: (deadline?: IdleDeadline | DOMHighResTimeStamp | void) => void, opts?: {timeout: number , unsupportedBehavior?: "animationFrame" | "timeout" | "immediatly" }): [boolean, () => void, () => void]
4243
```
4344

4445
> ### Params
@@ -51,8 +52,11 @@ Contains optional configuration parameters.
5152

5253
> ### Returns
5354
>
54-
> __result__: array where functions to invoke and cancel execution.
55-
> - __Array__:
56-
> - _()=>void_
57-
> - _()=>void_
55+
> __result__: __Array__:
56+
- _()=>void_
57+
- _()=>void_
58+
> Array with three elements:
59+
> - first element: __isSupported__; boolean value that indicates if _requestIdleCallback_ is supported or not.
60+
> - second element: __invoke__: function to invoke execution.
61+
> - third element: __cancel__: function to cancel execution.
5862
>

apps/react-tools-demo/src/markdown/useStateValidator.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const UseStateValidator = () => {
4747
## API
4848

4949
```tsx
50-
useStateValidator<T>(initialState: T | (() => T), validator: StateValidator<T>): [T, Dispatch<SetStateAction<T>>, T extends Record<string, unknown> ? {[k in keyof T]:{invalid: boolean, message?: string}} : {invalid: boolean, message?: string}]
50+
useStateValidator<T>(initialState: T | (() => T), validator: StateValidator<T>): [T, Dispatch<SetStateAction<T>>, T extends object ? {[k in keyof T]:{invalid: boolean, message?: string}} : {invalid: boolean, message?: string}]
5151
```
5252

5353
> ### Params
@@ -64,7 +64,7 @@ function that will be executed to validate state.
6464
> __} invalid__: __Array__:
6565
- _T_
6666
- _Dispatch<SetStateAction<T>>_
67-
- _T extends Record<string, unknown> ? {[k in keyof T]:{invalid: boolean, message?: string}} : {invalid: boolean, message?: strin_
67+
- _T extends object ? {[k in keyof T]:{invalid: boolean, message?: string}} : {invalid: boolean, message?: strin_
6868
> Array with:
6969
> - first element: __state__ value.
7070
> - second element: __setState__ function to update state.

apps/react-tools-demo/src/pages/hooks/api-dom/useBattery/UseBattery.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const UseBattery = () => {
99
return (<div style={{ textAlign: "center" }}>
1010
{
1111
Object.keys(status).map(el => (
12-
<p key={el}>{el}: {status[el as keyof typeof status]}</p>
12+
<p key={el}>{el}: {el === "isSupported" ? status.isSupported ? "Yes" : "No": status[el as keyof typeof status]}</p>
1313
))
1414
}
1515
</div>)

apps/react-tools-demo/src/pages/hooks/api-dom/useIdleCallback/UseIdleCallback.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The component has:
1212
export const UseIdleCallback = () => {
1313
const [iterations, setIterations] = useState(0);
1414
const [log, setLog] = useState("");
15-
const [invoke] = useIdleCallback(() => setLog("RequestIdleCallback executed"));
15+
const [isSupported, invoke] = useIdleCallback(() => setLog("RequestIdleCallback executed"));
1616

1717
const start = async() => {
1818
invoke();
@@ -25,6 +25,7 @@ export const UseIdleCallback = () => {
2525
setIterations(0);
2626
}
2727
return (<div>
28+
<p>Supported: {isSupported ? "Yes" : "No"}</p>
2829
<p>Iterations are: {iterations}</p>
2930
<p>Log is: {log}</p>
3031
<button onClick={start}>START</button>
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)