@jobber/hooks 2.17.2 → 2.17.3
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/dist/index.js +34 -18
- package/dist/useBool/index.js +5 -1
- package/dist/useBool/useBool.js +9 -6
- package/dist/useBool/useBool.test.js +11 -9
- package/dist/useBreakpoints/index.js +20 -2
- package/dist/useBreakpoints/mockViewportWidth/index.js +17 -1
- package/dist/useBreakpoints/mockViewportWidth/mockViewportWidth.js +6 -2
- package/dist/useBreakpoints/mockViewportWidth/mockViewportWidth.test.js +6 -4
- package/dist/useBreakpoints/useBreakpoints.js +13 -9
- package/dist/useBreakpoints/useBreakpoints.test.js +35 -30
- package/dist/useBreakpoints/useMediaQuery.js +9 -5
- package/dist/useCallbackRef/index.js +5 -1
- package/dist/useCallbackRef/useCallbackRef.js +8 -5
- package/dist/useCallbackRef/useCallbackRef.test.js +6 -4
- package/dist/useCollectionQuery/index.js +5 -1
- package/dist/useCollectionQuery/mdxUtils.js +12 -8
- package/dist/useCollectionQuery/test-utilities/index.js +19 -3
- package/dist/useCollectionQuery/test-utilities/mocks.js +30 -19
- package/dist/useCollectionQuery/test-utilities/queries.js +7 -4
- package/dist/useCollectionQuery/test-utilities/utils.js +4 -1
- package/dist/useCollectionQuery/uniqueEdges.js +6 -2
- package/dist/useCollectionQuery/uniqueNodes.js +4 -1
- package/dist/useCollectionQuery/useCollectionQuery.js +34 -27
- package/dist/useCollectionQuery/useCollectionQuery.test.js +128 -126
- package/dist/useDebounce/index.js +5 -1
- package/dist/useDebounce/useDebounce.js +11 -8
- package/dist/useDebounce/useDebounce.test.js +70 -42
- package/dist/useFocusTrap/index.js +5 -1
- package/dist/useFocusTrap/useFocusTrap.js +7 -4
- package/dist/useFocusTrap/useFocusTrap.test.js +31 -26
- package/dist/useFormState/index.js +5 -1
- package/dist/useFormState/useFormState.js +6 -3
- package/dist/useInView/index.js +17 -1
- package/dist/useInView/useInView.js +9 -6
- package/dist/useInView/useInView.test.js +17 -12
- package/dist/useIsMounted/index.js +5 -1
- package/dist/useIsMounted/useIsMounted.js +8 -5
- package/dist/useIsMounted/useIsMounted.test.js +6 -4
- package/dist/useLiveAnnounce/index.js +5 -1
- package/dist/useLiveAnnounce/useLiveAnnounce.js +7 -4
- package/dist/useLiveAnnounce/useLiveAnnounce.test.js +24 -19
- package/dist/useOnKeyDown/index.js +5 -1
- package/dist/useOnKeyDown/useOnKeyDown.js +6 -3
- package/dist/useOnKeyDown/useOnKeyDown.test.js +12 -7
- package/dist/useOnMount/index.js +5 -1
- package/dist/useOnMount/useOnMount.js +10 -6
- package/dist/useOnMount/useOnMount.test.js +6 -4
- package/dist/useRefocusOnActivator/index.js +5 -1
- package/dist/useRefocusOnActivator/useRefocusOnActivator.js +6 -3
- package/dist/useResizeObserver/index.js +17 -1
- package/dist/useResizeObserver/useResizeObserver.js +16 -9
- package/dist/useSafeLayoutEffect/index.js +5 -1
- package/dist/useSafeLayoutEffect/useSafeLayoutEffect.js +7 -4
- package/dist/useShowClear/index.js +5 -1
- package/dist/useShowClear/useShowClear.js +4 -1
- package/dist/useShowClear/useShowClear.test.js +8 -6
- package/dist/useStepper/index.js +5 -1
- package/dist/useStepper/useStepper.js +15 -9
- package/dist/useStepper/useStepper.test.js +17 -15
- package/dist/useWindowDimensions/index.js +5 -1
- package/dist/useWindowDimensions/useWIndowDimensions.test.js +8 -6
- package/dist/useWindowDimensions/useWindowDimensions.js +7 -4
- package/package.json +5 -102
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
1
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -7,10 +31,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
32
|
});
|
|
9
33
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const react_2 = require("@testing-library/react");
|
|
40
|
+
const user_event_1 = __importDefault(require("@testing-library/user-event"));
|
|
41
|
+
const useDebounce_1 = require("./useDebounce");
|
|
14
42
|
const DEBOUNCE_WAIT = 300;
|
|
15
43
|
describe("useDebounce", () => {
|
|
16
44
|
beforeEach(() => {
|
|
@@ -21,10 +49,10 @@ describe("useDebounce", () => {
|
|
|
21
49
|
});
|
|
22
50
|
it("should debounce the function call", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
51
|
const mockFn = jest.fn();
|
|
24
|
-
const { result } = renderHook(() => useDebounce(mockFn, DEBOUNCE_WAIT));
|
|
52
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
25
53
|
result.current("test");
|
|
26
54
|
expect(mockFn).not.toHaveBeenCalled();
|
|
27
|
-
act(() => {
|
|
55
|
+
(0, react_2.act)(() => {
|
|
28
56
|
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
29
57
|
});
|
|
30
58
|
expect(mockFn).toHaveBeenCalledWith("test");
|
|
@@ -32,23 +60,23 @@ describe("useDebounce", () => {
|
|
|
32
60
|
}));
|
|
33
61
|
it("should cancel pending debounced calls on unmount", () => {
|
|
34
62
|
const mockFn = jest.fn();
|
|
35
|
-
const { result, unmount } = renderHook(() => useDebounce(mockFn, DEBOUNCE_WAIT));
|
|
63
|
+
const { result, unmount } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
36
64
|
result.current("test");
|
|
37
65
|
unmount();
|
|
38
|
-
act(() => {
|
|
66
|
+
(0, react_2.act)(() => {
|
|
39
67
|
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
40
68
|
});
|
|
41
69
|
expect(mockFn).not.toHaveBeenCalled();
|
|
42
70
|
});
|
|
43
71
|
it("should handle multiple calls within the debounce period", () => {
|
|
44
72
|
const mockFn = jest.fn();
|
|
45
|
-
const { result } = renderHook(() => useDebounce(mockFn, DEBOUNCE_WAIT));
|
|
73
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
46
74
|
result.current("first");
|
|
47
|
-
act(() => {
|
|
75
|
+
(0, react_2.act)(() => {
|
|
48
76
|
jest.advanceTimersByTime(DEBOUNCE_WAIT / 2);
|
|
49
77
|
});
|
|
50
78
|
result.current("second");
|
|
51
|
-
act(() => {
|
|
79
|
+
(0, react_2.act)(() => {
|
|
52
80
|
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
53
81
|
});
|
|
54
82
|
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
@@ -58,7 +86,7 @@ describe("useDebounce", () => {
|
|
|
58
86
|
const mockFn = jest.fn();
|
|
59
87
|
const debounceEgdesOption = ["trailing"];
|
|
60
88
|
// Use a function that returns a new options object each time
|
|
61
|
-
const { result, rerender } = renderHook(({ options }) => useDebounce(mockFn, DEBOUNCE_WAIT, options), { initialProps: { options: { edges: debounceEgdesOption } } });
|
|
89
|
+
const { result, rerender } = (0, react_2.renderHook)(({ options }) => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT, options), { initialProps: { options: { edges: debounceEgdesOption } } });
|
|
62
90
|
const debounceRef = result.current;
|
|
63
91
|
rerender({ options: { edges: debounceEgdesOption } });
|
|
64
92
|
expect(debounceRef).toBe(result.current);
|
|
@@ -70,16 +98,16 @@ describe("useDebounce", () => {
|
|
|
70
98
|
// Start with trailing edge
|
|
71
99
|
const debounceEgdesOption = ["trailing"];
|
|
72
100
|
// Use a function that returns a new options object each time
|
|
73
|
-
const { result, rerender } = renderHook(({ options }) => useDebounce(mockFn, DEBOUNCE_WAIT, options), { initialProps: { options: { edges: debounceEgdesOption } } });
|
|
101
|
+
const { result, rerender } = (0, react_2.renderHook)(({ options }) => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT, options), { initialProps: { options: { edges: debounceEgdesOption } } });
|
|
74
102
|
result.current("first");
|
|
75
|
-
act(() => {
|
|
103
|
+
(0, react_2.act)(() => {
|
|
76
104
|
jest.advanceTimersByTime(TIME_INCREMENT_LESSER_THAN_DEBOUNCE_WAIT);
|
|
77
105
|
});
|
|
78
106
|
expect(mockFn).not.toHaveBeenCalled();
|
|
79
107
|
// This means it calls immediately at the leading edge of the timeout.
|
|
80
108
|
rerender({ options: { edges: ["leading"] } });
|
|
81
109
|
result.current("second");
|
|
82
|
-
act(() => {
|
|
110
|
+
(0, react_2.act)(() => {
|
|
83
111
|
jest.advanceTimersByTime(TIME_INCREMENT_LESSER_THAN_DEBOUNCE_WAIT);
|
|
84
112
|
});
|
|
85
113
|
// The config change should be ignored, options are hardcoded
|
|
@@ -87,52 +115,52 @@ describe("useDebounce", () => {
|
|
|
87
115
|
});
|
|
88
116
|
it("should work with React components", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
117
|
function DebouncedComponent() {
|
|
90
|
-
const [value, setValue] = useState("");
|
|
91
|
-
const [debouncedValue, setDebouncedValue] = useState("");
|
|
92
|
-
const debouncedSetValue = useDebounce((newValue) => {
|
|
118
|
+
const [value, setValue] = (0, react_1.useState)("");
|
|
119
|
+
const [debouncedValue, setDebouncedValue] = (0, react_1.useState)("");
|
|
120
|
+
const debouncedSetValue = (0, useDebounce_1.useDebounce)((newValue) => {
|
|
93
121
|
setDebouncedValue(newValue);
|
|
94
122
|
}, DEBOUNCE_WAIT);
|
|
95
|
-
useEffect(() => {
|
|
123
|
+
(0, react_1.useEffect)(() => {
|
|
96
124
|
debouncedSetValue(value);
|
|
97
125
|
}, [value, debouncedSetValue]);
|
|
98
|
-
return (
|
|
99
|
-
|
|
100
|
-
|
|
126
|
+
return (react_1.default.createElement("div", null,
|
|
127
|
+
react_1.default.createElement("input", { "data-testid": "input", value: value, onChange: e => setValue(e.target.value) }),
|
|
128
|
+
react_1.default.createElement("div", { "data-testid": "debounced-value" }, debouncedValue)));
|
|
101
129
|
}
|
|
102
|
-
render(
|
|
103
|
-
const input = screen.getByTestId("input");
|
|
104
|
-
const debouncedValue = screen.getByTestId("debounced-value");
|
|
105
|
-
const user =
|
|
130
|
+
(0, react_2.render)(react_1.default.createElement(DebouncedComponent, null));
|
|
131
|
+
const input = react_2.screen.getByTestId("input");
|
|
132
|
+
const debouncedValue = react_2.screen.getByTestId("debounced-value");
|
|
133
|
+
const user = user_event_1.default.setup({ advanceTimers: jest.advanceTimersByTime });
|
|
106
134
|
yield user.type(input, "test");
|
|
107
135
|
expect(debouncedValue.textContent).toBe("");
|
|
108
|
-
act(() => {
|
|
136
|
+
(0, react_2.act)(() => {
|
|
109
137
|
jest.advanceTimersByTime(DEBOUNCE_WAIT + 100);
|
|
110
138
|
});
|
|
111
139
|
expect(debouncedValue.textContent).toBe("test");
|
|
112
140
|
}), 10000);
|
|
113
141
|
it("should properly handle options object", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
114
142
|
function DebouncedComponent() {
|
|
115
|
-
const [count, setCount] = useState(0);
|
|
116
|
-
const [debouncedCount, setDebouncedCount] = useState(0);
|
|
143
|
+
const [count, setCount] = (0, react_1.useState)(0);
|
|
144
|
+
const [debouncedCount, setDebouncedCount] = (0, react_1.useState)(0);
|
|
117
145
|
const options = {
|
|
118
146
|
edges: ["leading", "trailing"],
|
|
119
147
|
};
|
|
120
|
-
const debouncedSetCount = useDebounce((value) => {
|
|
148
|
+
const debouncedSetCount = (0, useDebounce_1.useDebounce)((value) => {
|
|
121
149
|
setDebouncedCount(value);
|
|
122
150
|
}, DEBOUNCE_WAIT, options);
|
|
123
|
-
return (
|
|
124
|
-
|
|
151
|
+
return (react_1.default.createElement("div", null,
|
|
152
|
+
react_1.default.createElement("button", { "data-testid": "increment", onClick: () => {
|
|
125
153
|
const newCount = count + 1;
|
|
126
154
|
setCount(newCount);
|
|
127
155
|
debouncedSetCount(newCount);
|
|
128
156
|
}, type: "button" }, "Increment"),
|
|
129
|
-
|
|
130
|
-
|
|
157
|
+
react_1.default.createElement("div", { "data-testid": "count" }, count),
|
|
158
|
+
react_1.default.createElement("div", { "data-testid": "debounced-count" }, debouncedCount)));
|
|
131
159
|
}
|
|
132
|
-
render(
|
|
133
|
-
const incrementButton = screen.getByTestId("increment");
|
|
134
|
-
const debouncedCount = screen.getByTestId("debounced-count");
|
|
135
|
-
const user =
|
|
160
|
+
(0, react_2.render)(react_1.default.createElement(DebouncedComponent, null));
|
|
161
|
+
const incrementButton = react_2.screen.getByTestId("increment");
|
|
162
|
+
const debouncedCount = react_2.screen.getByTestId("debounced-count");
|
|
163
|
+
const user = user_event_1.default.setup({ advanceTimers: jest.advanceTimersByTime });
|
|
136
164
|
yield user.click(incrementButton);
|
|
137
165
|
// With leading edge, the value should be updated immediately
|
|
138
166
|
expect(debouncedCount.textContent).toBe("1");
|
|
@@ -141,7 +169,7 @@ describe("useDebounce", () => {
|
|
|
141
169
|
// Additional clicks shouldn't update immediately (debounced)
|
|
142
170
|
expect(debouncedCount.textContent).toBe("1");
|
|
143
171
|
// After the debounce period, the trailing edge should update with the latest value
|
|
144
|
-
act(() => {
|
|
172
|
+
(0, react_2.act)(() => {
|
|
145
173
|
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
146
174
|
});
|
|
147
175
|
expect(debouncedCount.textContent).toBe("3");
|
|
@@ -149,12 +177,12 @@ describe("useDebounce", () => {
|
|
|
149
177
|
it("should abort debounced function when signal is aborted", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
150
178
|
const mockFn = jest.fn();
|
|
151
179
|
const controller = new AbortController();
|
|
152
|
-
const { result } = renderHook(() => useDebounce(mockFn, DEBOUNCE_WAIT, { signal: controller.signal }));
|
|
180
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT, { signal: controller.signal }));
|
|
153
181
|
result.current("test");
|
|
154
|
-
act(() => {
|
|
182
|
+
(0, react_2.act)(() => {
|
|
155
183
|
controller.abort();
|
|
156
184
|
});
|
|
157
|
-
act(() => {
|
|
185
|
+
(0, react_2.act)(() => {
|
|
158
186
|
jest.advanceTimersByTime(DEBOUNCE_WAIT + 100);
|
|
159
187
|
});
|
|
160
188
|
expect(mockFn).not.toHaveBeenCalled();
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useFocusTrap = void 0;
|
|
4
|
+
var useFocusTrap_1 = require("./useFocusTrap");
|
|
5
|
+
Object.defineProperty(exports, "useFocusTrap", { enumerable: true, get: function () { return useFocusTrap_1.useFocusTrap; } });
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useFocusTrap = useFocusTrap;
|
|
4
|
+
const react_1 = require("react");
|
|
2
5
|
/**
|
|
3
6
|
* Traps the focus within the children of the ref element.
|
|
4
7
|
*
|
|
@@ -7,11 +10,11 @@ import { useEffect, useRef } from "react";
|
|
|
7
10
|
*
|
|
8
11
|
* @returns ref
|
|
9
12
|
*/
|
|
10
|
-
|
|
13
|
+
function useFocusTrap(active) {
|
|
11
14
|
// There's an ongoing issue with useRef return type clashing with an element's
|
|
12
15
|
// ref prop type. TLDR: Use null because useRef doesn't expect undefined.
|
|
13
16
|
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/35572
|
|
14
|
-
const ref = useRef(null);
|
|
17
|
+
const ref = (0, react_1.useRef)(null);
|
|
15
18
|
function handleKeyDown(event) {
|
|
16
19
|
if (!(active && ref.current) || event.key !== "Tab") {
|
|
17
20
|
return;
|
|
@@ -30,7 +33,7 @@ export function useFocusTrap(active) {
|
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
35
|
}
|
|
33
|
-
useEffect(() => {
|
|
36
|
+
(0, react_1.useEffect)(() => {
|
|
34
37
|
if (active && ref.current) {
|
|
35
38
|
const { firstElement } = getElements(ref.current);
|
|
36
39
|
firstElement.focus();
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -7,50 +8,54 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
9
|
});
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const react_1 = __importDefault(require("react"));
|
|
16
|
+
const react_2 = require("@testing-library/react");
|
|
17
|
+
const user_event_1 = __importDefault(require("@testing-library/user-event"));
|
|
18
|
+
const useFocusTrap_1 = require("./useFocusTrap");
|
|
14
19
|
const targetId = "target";
|
|
15
20
|
const firstFocusableChild = "first-element";
|
|
16
21
|
const lastFocusableChild = "last-element";
|
|
17
22
|
it("should focus on the ref target on mount", () => {
|
|
18
|
-
const { getByTestId } = render(
|
|
23
|
+
const { getByTestId } = (0, react_2.render)(react_1.default.createElement(TestComponent, null));
|
|
19
24
|
expect(getByTestId(targetId)).toHaveFocus();
|
|
20
25
|
});
|
|
21
26
|
it("should focus on the ref target when tabbing out of the last focusable element and ignore the tabindex'=-1'", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
-
const { getByTestId } = render(
|
|
27
|
+
const { getByTestId } = (0, react_2.render)(react_1.default.createElement(TestComponent, null));
|
|
23
28
|
getByTestId(lastFocusableChild).focus();
|
|
24
|
-
yield
|
|
29
|
+
yield user_event_1.default.tab();
|
|
25
30
|
expect(getByTestId(targetId)).toHaveFocus();
|
|
26
31
|
}));
|
|
27
32
|
it("should focus on the first focusable element", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
-
const { getByTestId } = render(
|
|
29
|
-
yield
|
|
33
|
+
const { getByTestId } = (0, react_2.render)(react_1.default.createElement(TestComponent, null));
|
|
34
|
+
yield user_event_1.default.tab();
|
|
30
35
|
expect(getByTestId(firstFocusableChild)).toHaveFocus();
|
|
31
36
|
}));
|
|
32
37
|
it("should focus on the last focusable element", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
-
const { getByTestId } = render(
|
|
34
|
-
yield
|
|
38
|
+
const { getByTestId } = (0, react_2.render)(react_1.default.createElement(TestComponent, null));
|
|
39
|
+
yield user_event_1.default.tab({ shift: true });
|
|
35
40
|
expect(getByTestId(lastFocusableChild)).toHaveFocus();
|
|
36
41
|
}));
|
|
37
42
|
it("should not trap the tabbing and focus on the first child node", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
-
const { getByTestId } = render(
|
|
39
|
-
yield
|
|
43
|
+
const { getByTestId } = (0, react_2.render)(react_1.default.createElement(TestComponent, { trap: false }));
|
|
44
|
+
yield user_event_1.default.tab();
|
|
40
45
|
expect(getByTestId(targetId).previousElementSibling).toHaveFocus();
|
|
41
46
|
}));
|
|
42
47
|
function TestComponent({ trap = true }) {
|
|
43
|
-
const testRef = useFocusTrap(trap);
|
|
44
|
-
return (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
const testRef = (0, useFocusTrap_1.useFocusTrap)(trap);
|
|
49
|
+
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
50
|
+
react_1.default.createElement("input", { type: "number" }),
|
|
51
|
+
react_1.default.createElement("div", { ref: testRef, "data-testid": targetId, tabIndex: 0 },
|
|
52
|
+
react_1.default.createElement("button", { "data-testid": firstFocusableChild }, "Click me"),
|
|
53
|
+
react_1.default.createElement("a", { href: "#" }),
|
|
54
|
+
react_1.default.createElement("input", { type: "text" }),
|
|
55
|
+
react_1.default.createElement("select", null,
|
|
56
|
+
react_1.default.createElement("option", { value: "A" })),
|
|
57
|
+
react_1.default.createElement("textarea", null),
|
|
58
|
+
react_1.default.createElement("span", { tabIndex: 0, "data-testId": lastFocusableChild }),
|
|
59
|
+
react_1.default.createElement("span", { tabIndex: -1 })),
|
|
60
|
+
react_1.default.createElement("input", { type: "calendar" })));
|
|
56
61
|
}
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useFormState = void 0;
|
|
4
|
+
var useFormState_1 = require("./useFormState");
|
|
5
|
+
Object.defineProperty(exports, "useFormState", { enumerable: true, get: function () { return useFormState_1.useFormState; } });
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useFormState = useFormState;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useFormState() {
|
|
6
|
+
const [formState, setFormState] = (0, react_1.useState)({
|
|
4
7
|
isDirty: false,
|
|
5
8
|
isValid: false,
|
|
6
9
|
});
|
package/dist/useInView/index.js
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useInView"), exports);
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useInView = useInView;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useInView() {
|
|
6
|
+
const ref = (0, react_1.useRef)(null);
|
|
7
|
+
const [isInView, setIsInView] = (0, react_1.useState)(false);
|
|
8
|
+
const handleIntersection = (0, react_1.useCallback)(entries => setIsInView(entries[0].isIntersecting), [setIsInView]);
|
|
9
|
+
(0, react_1.useEffect)(() => {
|
|
7
10
|
if (!window.IntersectionObserver)
|
|
8
11
|
return;
|
|
9
12
|
const observer = new IntersectionObserver(handleIntersection);
|
|
@@ -1,24 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
const react_2 = require("@testing-library/react");
|
|
8
|
+
const jsdom_testing_mocks_1 = require("jsdom-testing-mocks");
|
|
9
|
+
const useInView_1 = require("./useInView");
|
|
10
|
+
(0, jsdom_testing_mocks_1.configMocks)({ act: react_2.act });
|
|
11
|
+
const observer = (0, jsdom_testing_mocks_1.mockIntersectionObserver)();
|
|
7
12
|
describe("useInView", () => {
|
|
8
13
|
it("should return true when the target element is in view", () => {
|
|
9
|
-
const { result, rerender } = renderHook(() => useInView());
|
|
14
|
+
const { result, rerender } = (0, react_2.renderHook)(() => (0, useInView_1.useInView)());
|
|
10
15
|
const [ref, isInView] = result.current;
|
|
11
|
-
render(
|
|
16
|
+
(0, react_2.render)(react_1.default.createElement("button", { ref: ref }));
|
|
12
17
|
rerender();
|
|
13
18
|
expect(result.current[0].current).toBeInstanceOf(HTMLButtonElement);
|
|
14
|
-
observer.leaveNode(screen.getByRole("button"));
|
|
19
|
+
observer.leaveNode(react_2.screen.getByRole("button"));
|
|
15
20
|
expect(isInView).toBe(false);
|
|
16
21
|
});
|
|
17
22
|
it("should return false when the target element is in view", () => {
|
|
18
|
-
const { result, rerender } = renderHook(() => useInView());
|
|
19
|
-
render(
|
|
23
|
+
const { result, rerender } = (0, react_2.renderHook)(() => (0, useInView_1.useInView)());
|
|
24
|
+
(0, react_2.render)(react_1.default.createElement("button", { ref: result.current[0] }));
|
|
20
25
|
rerender();
|
|
21
|
-
observer.enterNode(screen.getByRole("button"));
|
|
26
|
+
observer.enterNode(react_2.screen.getByRole("button"));
|
|
22
27
|
expect(result.current[1]).toBe(true);
|
|
23
28
|
});
|
|
24
29
|
});
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIsMounted = void 0;
|
|
4
|
+
var useIsMounted_1 = require("./useIsMounted");
|
|
5
|
+
Object.defineProperty(exports, "useIsMounted", { enumerable: true, get: function () { return useIsMounted_1.useIsMounted; } });
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIsMounted = useIsMounted;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useSafeLayoutEffect_1 = require("../useSafeLayoutEffect");
|
|
3
6
|
/**
|
|
4
7
|
* If you are using this hook in order to only perform an action once after mounting (for example sending
|
|
5
8
|
* analytics events), use `useOnMount` instead
|
|
@@ -19,9 +22,9 @@ import { useSafeLayoutEffect } from "../useSafeLayoutEffect";
|
|
|
19
22
|
* When the component unmounts, it calls the cleanup function that sets `isMounted` to false.
|
|
20
23
|
* This `useLayoutEffect` hook will only be run once.
|
|
21
24
|
*/
|
|
22
|
-
|
|
23
|
-
const isMounted = useRef(false);
|
|
24
|
-
useSafeLayoutEffect(() => {
|
|
25
|
+
function useIsMounted() {
|
|
26
|
+
const isMounted = (0, react_1.useRef)(false);
|
|
27
|
+
(0, useSafeLayoutEffect_1.useSafeLayoutEffect)(() => {
|
|
25
28
|
isMounted.current = true;
|
|
26
29
|
return () => {
|
|
27
30
|
isMounted.current = false;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_hooks_1 = require("@testing-library/react-hooks");
|
|
4
|
+
const useIsMounted_1 = require("./useIsMounted");
|
|
3
5
|
it("should return true when the component is currently mounted", () => {
|
|
4
|
-
const { result } = renderHook(() => useIsMounted());
|
|
6
|
+
const { result } = (0, react_hooks_1.renderHook)(() => (0, useIsMounted_1.useIsMounted)());
|
|
5
7
|
const isMounted = result.current;
|
|
6
8
|
expect(isMounted.current).toBe(true);
|
|
7
9
|
});
|
|
8
10
|
it("should return false when the component is unmounted", () => {
|
|
9
|
-
const { result, unmount } = renderHook(() => useIsMounted());
|
|
11
|
+
const { result, unmount } = (0, react_hooks_1.renderHook)(() => (0, useIsMounted_1.useIsMounted)());
|
|
10
12
|
const isMounted = result.current;
|
|
11
13
|
unmount();
|
|
12
14
|
expect(isMounted.current).toBe(false);
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLiveAnnounce = void 0;
|
|
4
|
+
var useLiveAnnounce_1 = require("./useLiveAnnounce");
|
|
5
|
+
Object.defineProperty(exports, "useLiveAnnounce", { enumerable: true, get: function () { return useLiveAnnounce_1.useLiveAnnounce; } });
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLiveAnnounce = useLiveAnnounce;
|
|
4
|
+
const react_1 = require("react");
|
|
2
5
|
/**
|
|
3
6
|
* Announce a message on voice over whenever you do an action. This is
|
|
4
7
|
* especially helpful when you have an action that adds or deletes an element
|
|
5
8
|
* from the screen.
|
|
6
9
|
*/
|
|
7
|
-
|
|
8
|
-
const [announcedMessage, setAnnouncedMessage] = useState("");
|
|
9
|
-
useEffect(() => {
|
|
10
|
+
function useLiveAnnounce() {
|
|
11
|
+
const [announcedMessage, setAnnouncedMessage] = (0, react_1.useState)("");
|
|
12
|
+
(0, react_1.useEffect)(() => {
|
|
10
13
|
let target;
|
|
11
14
|
if (announcedMessage) {
|
|
12
15
|
target = createAnnouncedElement();
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -7,26 +8,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
9
|
});
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const react_1 = __importDefault(require("react"));
|
|
16
|
+
const react_2 = require("@testing-library/react");
|
|
17
|
+
const _1 = require(".");
|
|
13
18
|
function setupHook() {
|
|
14
19
|
const returnVal = {
|
|
15
20
|
liveAnnounce: jest.fn,
|
|
16
21
|
};
|
|
17
22
|
function TestComponent() {
|
|
18
|
-
Object.assign(returnVal, useLiveAnnounce());
|
|
19
|
-
return
|
|
23
|
+
Object.assign(returnVal, (0, _1.useLiveAnnounce)());
|
|
24
|
+
return react_1.default.createElement(react_1.default.Fragment, null);
|
|
20
25
|
}
|
|
21
|
-
const { rerender } = render(
|
|
22
|
-
return Object.assign(Object.assign({}, returnVal), { rerenderComponent: () => rerender(
|
|
26
|
+
const { rerender } = (0, react_2.render)(react_1.default.createElement(TestComponent, null));
|
|
27
|
+
return Object.assign(Object.assign({}, returnVal), { rerenderComponent: () => rerender(react_1.default.createElement(TestComponent, null)) });
|
|
23
28
|
}
|
|
24
29
|
it("should render a div to announce", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
30
|
const { liveAnnounce } = setupHook();
|
|
26
31
|
const message = "Huzzah";
|
|
27
|
-
act(() => liveAnnounce(message));
|
|
28
|
-
yield waitFor(() => {
|
|
29
|
-
const expectedElement = screen.queryByRole("status");
|
|
32
|
+
(0, react_2.act)(() => liveAnnounce(message));
|
|
33
|
+
yield (0, react_2.waitFor)(() => {
|
|
34
|
+
const expectedElement = react_2.screen.queryByRole("status");
|
|
30
35
|
expect(expectedElement).toBeInTheDocument();
|
|
31
36
|
expect(expectedElement === null || expectedElement === void 0 ? void 0 : expectedElement.textContent).toBe(message);
|
|
32
37
|
expect(expectedElement).toHaveAttribute("role", "status");
|
|
@@ -36,20 +41,20 @@ it("should render a div to announce", () => __awaiter(void 0, void 0, void 0, fu
|
|
|
36
41
|
}));
|
|
37
42
|
it("should not render the announced div", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
43
|
setupHook();
|
|
39
|
-
expect(screen.queryByRole("status")).not.toBeInTheDocument();
|
|
44
|
+
expect(react_2.screen.queryByRole("status")).not.toBeInTheDocument();
|
|
40
45
|
}));
|
|
41
46
|
it("should only have 1 div to announce a message on a single instance of the hook", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
47
|
const { liveAnnounce } = setupHook();
|
|
43
48
|
const firstMessage = "I am first";
|
|
44
49
|
const secondMessage = "I am second";
|
|
45
|
-
act(() => liveAnnounce(firstMessage));
|
|
46
|
-
yield waitFor(() => {
|
|
47
|
-
expect(screen.queryAllByRole("status")).toHaveLength(1);
|
|
48
|
-
expect(screen.getByRole("status").textContent).toBe(firstMessage);
|
|
50
|
+
(0, react_2.act)(() => liveAnnounce(firstMessage));
|
|
51
|
+
yield (0, react_2.waitFor)(() => {
|
|
52
|
+
expect(react_2.screen.queryAllByRole("status")).toHaveLength(1);
|
|
53
|
+
expect(react_2.screen.getByRole("status").textContent).toBe(firstMessage);
|
|
49
54
|
});
|
|
50
|
-
act(() => liveAnnounce(secondMessage));
|
|
51
|
-
yield waitFor(() => {
|
|
52
|
-
expect(screen.queryAllByRole("status")).toHaveLength(1);
|
|
53
|
-
expect(screen.getByRole("status").textContent).toBe(secondMessage);
|
|
55
|
+
(0, react_2.act)(() => liveAnnounce(secondMessage));
|
|
56
|
+
yield (0, react_2.waitFor)(() => {
|
|
57
|
+
expect(react_2.screen.queryAllByRole("status")).toHaveLength(1);
|
|
58
|
+
expect(react_2.screen.getByRole("status").textContent).toBe(secondMessage);
|
|
54
59
|
});
|
|
55
60
|
}));
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnKeyDown = void 0;
|
|
4
|
+
var useOnKeyDown_1 = require("./useOnKeyDown");
|
|
5
|
+
Object.defineProperty(exports, "useOnKeyDown", { enumerable: true, get: function () { return useOnKeyDown_1.useOnKeyDown; } });
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnKeyDown = useOnKeyDown;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useOnKeyDown(callback, keys) {
|
|
6
|
+
(0, react_1.useEffect)(() => {
|
|
4
7
|
window.addEventListener("keydown", handler);
|
|
5
8
|
return () => {
|
|
6
9
|
window.removeEventListener("keydown", handler);
|