@fibery/react 1.0.3 → 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/react",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "private": false,
5
5
  "license": "UNLICENSED",
6
6
  "files": [
@@ -10,7 +10,7 @@
10
10
  "react": "^18.2.0"
11
11
  },
12
12
  "dependencies": {
13
- "@fibery/helpers": "1.1.0"
13
+ "@fibery/helpers": "1.2.0"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@testing-library/react": "13.4.0",
@@ -23,9 +23,9 @@
23
23
  "react": "18.2.0",
24
24
  "react-dom": "18.2.0",
25
25
  "react-test-renderer": "18.2.0",
26
- "typescript": "5.0.3",
27
- "@fibery/babel-preset": "7.2.0",
28
- "@fibery/eslint-config": "8.3.0"
26
+ "typescript": "5.1.6",
27
+ "@fibery/babel-preset": "7.3.0",
28
+ "@fibery/eslint-config": "8.4.0"
29
29
  },
30
30
  "jest": {
31
31
  "coveragePathIgnorePatterns": [
@@ -43,7 +43,7 @@
43
43
  "scripts": {
44
44
  "lint": "eslint .",
45
45
  "test": "jest",
46
- "test:ci": "yarn test --reporters=default --reporters=jest-junit",
47
- "test:coverage": "yarn test --coverage --coverageDirectory=${JEST_COVERAGE_RESULT_DIR:-$(pwd)}/coverage/react --reporters=default --reporters=jest-junit"
46
+ "test:ci": "pnpm run test --reporters=default --reporters=jest-junit",
47
+ "test:coverage": "pnpm run test --coverage --coverageDirectory=${JEST_COVERAGE_RESULT_DIR:-$(pwd)}/coverage/react --reporters=default --reporters=jest-junit"
48
48
  }
49
49
  }
@@ -1,3 +1,3 @@
1
- export const preventDefault = (e: React.SyntheticEvent): void => {
1
+ export const preventDefault = <Event extends {preventDefault: () => void} = React.SyntheticEvent>(e: Event): void => {
2
2
  e.preventDefault();
3
3
  };
@@ -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
+ };
@@ -1,6 +1,6 @@
1
1
  import React, {useCallback} from "react";
2
2
 
3
- const composeRefs = <T>(...refs: React.Ref<T>[]) => {
3
+ const composeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => {
4
4
  return function setRefs(node: T) {
5
5
  refs.forEach((ref) => {
6
6
  if (!ref) {
@@ -20,7 +20,7 @@ const composeRefs = <T>(...refs: React.Ref<T>[]) => {
20
20
  * Hook to compose multiple refs
21
21
  * Accepts callback refs and RefObject(s)
22
22
  */
23
- export const useComposedRefs = <T>(...refs: React.Ref<T>[]) => {
23
+ export const useComposedRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => {
24
24
  // eslint-disable-next-line react-hooks/exhaustive-deps
25
25
  return useCallback(composeRefs(...refs), refs);
26
26
  };
@@ -27,19 +27,24 @@ export const useControllableState = <TState>({
27
27
 
28
28
  const setState = useCallback<Dispatch<SetStateAction<TState>>>(
29
29
  (nextValue) => {
30
- const nextState = nextValue instanceof Function ? nextValue(state) : nextValue;
31
-
32
- if (state === nextState) {
33
- return;
34
- }
35
-
36
- if (!isControlled) {
37
- setUncontrolledState(nextState);
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, state]
47
+ [isControlled, onChangeState, value]
43
48
  );
44
49
 
45
50
  return [state, setState];