@uxf/core-react 11.35.0 → 11.36.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/README.md +15 -0
- package/hooks/use-increment.d.ts +2 -0
- package/hooks/use-increment.js +11 -0
- package/hooks/use-increment.test.d.ts +1 -0
- package/hooks/use-increment.test.js +61 -0
- package/hooks/use-toggle.d.ts +2 -0
- package/hooks/use-toggle.js +11 -0
- package/hooks/use-toggle.test.d.ts +1 -0
- package/hooks/use-toggle.test.js +46 -0
- package/package.json +23 -23
package/README.md
CHANGED
|
@@ -245,3 +245,18 @@ useEffect(() => {
|
|
|
245
245
|
previousState.current(); // use state of 'someUnstableWhatever' from previous render without affecting this effetct update
|
|
246
246
|
}, [previousState])
|
|
247
247
|
```
|
|
248
|
+
```tsx
|
|
249
|
+
import { useToggle } from "@uxf/core-react/hooks/use-toggle";
|
|
250
|
+
|
|
251
|
+
const [isVisible, toggleIsVisible, setVisible, setInvisible] = useToggle(false);
|
|
252
|
+
|
|
253
|
+
<button onClick={toggleIsVisible}>Toggle visibility</button>
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
```tsx
|
|
257
|
+
import { useIncrement } from "@uxf/core-react/hooks/use-increment";
|
|
258
|
+
|
|
259
|
+
const [count, increment, decrement, reset] = useIncrement(5, 1);
|
|
260
|
+
|
|
261
|
+
<button onClick={increment}>Toggle visibility</button>
|
|
262
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIncrement = useIncrement;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useIncrement(defaultValue, step = 1) {
|
|
6
|
+
const [value, setValue] = (0, react_1.useState)(defaultValue);
|
|
7
|
+
const incrementHandler = (0, react_1.useCallback)(() => setValue((prev) => prev + step), [step]);
|
|
8
|
+
const decrementHandler = (0, react_1.useCallback)(() => setValue((prev) => prev - step), [step]);
|
|
9
|
+
const resetHandler = (0, react_1.useCallback)(() => setValue(defaultValue), [defaultValue]);
|
|
10
|
+
return [value, incrementHandler, decrementHandler, resetHandler];
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("@testing-library/react");
|
|
4
|
+
const use_increment_1 = require("./use-increment"); // Adjust the import based on your file structure
|
|
5
|
+
describe("useIncrement Hook", () => {
|
|
6
|
+
it("should initialize with the default value", () => {
|
|
7
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(5));
|
|
8
|
+
const [value] = result.current;
|
|
9
|
+
expect(value).toBe(5);
|
|
10
|
+
});
|
|
11
|
+
it("should increment the value by the step value", () => {
|
|
12
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(5, 2));
|
|
13
|
+
const [value, incrementHandler] = result.current;
|
|
14
|
+
expect(value).toBe(5);
|
|
15
|
+
(0, react_1.act)(() => {
|
|
16
|
+
incrementHandler();
|
|
17
|
+
});
|
|
18
|
+
const [newValue] = result.current;
|
|
19
|
+
expect(newValue).toBe(7); // 5 + 2
|
|
20
|
+
});
|
|
21
|
+
it("should decrement the value by the step value", () => {
|
|
22
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(10, 3));
|
|
23
|
+
const [value, , decrementHandler] = result.current;
|
|
24
|
+
expect(value).toBe(10);
|
|
25
|
+
(0, react_1.act)(() => {
|
|
26
|
+
decrementHandler();
|
|
27
|
+
});
|
|
28
|
+
const [newValue] = result.current;
|
|
29
|
+
expect(newValue).toBe(7); // 10 - 3
|
|
30
|
+
});
|
|
31
|
+
it("should reset the value to the default value", () => {
|
|
32
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(20, 5));
|
|
33
|
+
const [value, incrementHandler, , resetHandler] = result.current;
|
|
34
|
+
expect(value).toBe(20);
|
|
35
|
+
(0, react_1.act)(() => {
|
|
36
|
+
incrementHandler();
|
|
37
|
+
});
|
|
38
|
+
const [newValue] = result.current;
|
|
39
|
+
expect(newValue).toBe(25); // 20 + 5
|
|
40
|
+
(0, react_1.act)(() => {
|
|
41
|
+
resetHandler();
|
|
42
|
+
});
|
|
43
|
+
const [resetValue] = result.current;
|
|
44
|
+
expect(resetValue).toBe(20); // Reset back to default
|
|
45
|
+
});
|
|
46
|
+
it("should use a step of 1 if no step is provided", () => {
|
|
47
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(10));
|
|
48
|
+
const [value, incrementHandler, decrementHandler] = result.current;
|
|
49
|
+
expect(value).toBe(10);
|
|
50
|
+
(0, react_1.act)(() => {
|
|
51
|
+
incrementHandler();
|
|
52
|
+
});
|
|
53
|
+
const [newValue] = result.current;
|
|
54
|
+
expect(newValue).toBe(11); // 10 + 1 (default step)
|
|
55
|
+
(0, react_1.act)(() => {
|
|
56
|
+
decrementHandler();
|
|
57
|
+
});
|
|
58
|
+
const [finalValue] = result.current;
|
|
59
|
+
expect(finalValue).toBe(10); // 11 - 1
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useToggle = useToggle;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useToggle(defaultValue) {
|
|
6
|
+
const [isOn, setIsOn] = (0, react_1.useState)(defaultValue);
|
|
7
|
+
const toggleHandler = (0, react_1.useCallback)(() => setIsOn((prev) => !prev), []);
|
|
8
|
+
const setTrueHandler = (0, react_1.useCallback)(() => setIsOn(true), []);
|
|
9
|
+
const setFalseHandler = (0, react_1.useCallback)(() => setIsOn(false), []);
|
|
10
|
+
return [isOn, toggleHandler, setTrueHandler, setFalseHandler];
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("@testing-library/react");
|
|
4
|
+
const use_toggle_1 = require("./use-toggle");
|
|
5
|
+
describe("useToggle Hook", () => {
|
|
6
|
+
it("should initialize with the default value", () => {
|
|
7
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_toggle_1.useToggle)(false));
|
|
8
|
+
const [isOn] = result.current;
|
|
9
|
+
expect(isOn).toBe(false);
|
|
10
|
+
});
|
|
11
|
+
it("should toggle the value when toggleHandler is called", () => {
|
|
12
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_toggle_1.useToggle)(false));
|
|
13
|
+
const [isOn, toggleHandler] = result.current;
|
|
14
|
+
expect(isOn).toBe(false);
|
|
15
|
+
(0, react_1.act)(() => {
|
|
16
|
+
toggleHandler();
|
|
17
|
+
});
|
|
18
|
+
const [isOnNew] = result.current;
|
|
19
|
+
expect(isOnNew).toBe(true);
|
|
20
|
+
(0, react_1.act)(() => {
|
|
21
|
+
toggleHandler();
|
|
22
|
+
});
|
|
23
|
+
const [isOnFinal] = result.current;
|
|
24
|
+
expect(isOnFinal).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
it("should set the value to true when setTrueHandler is called", () => {
|
|
27
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_toggle_1.useToggle)(false));
|
|
28
|
+
const [isOn, , setTrueHandler] = result.current;
|
|
29
|
+
expect(isOn).toBe(false);
|
|
30
|
+
(0, react_1.act)(() => {
|
|
31
|
+
setTrueHandler();
|
|
32
|
+
});
|
|
33
|
+
const [isOnNew] = result.current;
|
|
34
|
+
expect(isOnNew).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
it("should set the value to false when setFalseHandler is called", () => {
|
|
37
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_toggle_1.useToggle)(true));
|
|
38
|
+
const [isOn, , , setFalseHandler] = result.current;
|
|
39
|
+
expect(isOn).toBe(true);
|
|
40
|
+
(0, react_1.act)(() => {
|
|
41
|
+
setFalseHandler();
|
|
42
|
+
});
|
|
43
|
+
const [isOnNew] = result.current;
|
|
44
|
+
expect(isOnNew).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
});
|
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
2
|
+
"name": "@uxf/core-react",
|
|
3
|
+
"version": "11.36.0",
|
|
4
|
+
"description": "UXF Core",
|
|
5
|
+
"author": "UX Fans s.r.o",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc -P tsconfig.json",
|
|
12
|
+
"typecheck": "tsc --noEmit --skipLibCheck"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@uxf/core": "11.35.0"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"react": ">=18.2.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/react": "18.3.5",
|
|
22
|
+
"react": "18.3.1"
|
|
23
|
+
}
|
|
24
|
+
}
|