@fibery/react 1.1.0 → 1.2.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/package.json
CHANGED
package/src/prevent-default.ts
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {Children, ReactElement, ReactNode, isValidElement} from "react";
|
|
2
|
+
|
|
3
|
+
const nodeToString = (child?: React.ReactNode): string => {
|
|
4
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
5
|
+
return child.toString();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return "";
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const hasChildren = (node: ReactNode): node is ReactElement => isValidElement(node) && Boolean(node.props.children);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* Debug util to get text content of react node. Might be useful for logging
|
|
16
|
+
* based on https://github.com/fernandopasik/react-children-utilities/blob/main/src/lib/onlyText.ts
|
|
17
|
+
*/
|
|
18
|
+
export const reactNodeToString = (node: ReactNode | ReactNode[]) => {
|
|
19
|
+
if (!(node instanceof Array) && !isValidElement(node)) {
|
|
20
|
+
return nodeToString(node);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return Children.toArray(node).reduce((text: string, child: ReactNode): string => {
|
|
24
|
+
let newText = "";
|
|
25
|
+
|
|
26
|
+
if (hasChildren(child)) {
|
|
27
|
+
newText = reactNodeToString(child.props.children);
|
|
28
|
+
} else if (isValidElement(child) && !hasChildren(child)) {
|
|
29
|
+
newText = "";
|
|
30
|
+
} else {
|
|
31
|
+
newText = nodeToString(child);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return text.concat(newText);
|
|
35
|
+
}, "");
|
|
36
|
+
};
|
|
@@ -27,19 +27,24 @@ export const useControllableState = <TState>({
|
|
|
27
27
|
|
|
28
28
|
const setState = useCallback<Dispatch<SetStateAction<TState>>>(
|
|
29
29
|
(nextValue) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
if (isControlled) {
|
|
31
|
+
const nextState = nextValue instanceof Function ? nextValue(value) : nextValue;
|
|
32
|
+
|
|
33
|
+
if (value === nextState) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
onChangeState(nextState as TState);
|
|
37
|
+
} else {
|
|
38
|
+
setUncontrolledState((prevValue) => {
|
|
39
|
+
// should it be in effect to be more predictable?
|
|
40
|
+
if (prevValue !== nextValue) {
|
|
41
|
+
onChangeState(nextValue as TState);
|
|
42
|
+
}
|
|
43
|
+
return nextValue as TState;
|
|
44
|
+
});
|
|
38
45
|
}
|
|
39
|
-
|
|
40
|
-
onChangeState(nextState as TState);
|
|
41
46
|
},
|
|
42
|
-
[isControlled, onChangeState,
|
|
47
|
+
[isControlled, onChangeState, value]
|
|
43
48
|
);
|
|
44
49
|
|
|
45
50
|
return [state, setState];
|