@websolutespa/bom-mixer-hooks 0.2.0 → 0.2.2
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/CHANGELOG.md +12 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +7 -5
- package/src/index.ts +60 -0
- package/src/useApi/useApi.test.tsx +193 -0
- package/src/useApi/useApi.ts +58 -0
- package/src/useBodyScroll/useBodyScroll.test.ts +112 -0
- package/src/useBodyScroll/useBodyScroll.ts +89 -0
- package/src/useBreakpoint/useBreakpoint.test.tsx +45 -0
- package/src/useBreakpoint/useBreakpoint.ts +44 -0
- package/src/useCart/useCart.test.tsx +89 -0
- package/src/useCart/useCart.tsx +132 -0
- package/src/useCheckout/useCheckout.test.tsx +53 -0
- package/src/useCheckout/useCheckout.tsx +95 -0
- package/src/useClickAnyWhere/useClickAnyWhere.test.ts +15 -0
- package/src/useClickAnyWhere/useClickAnyWhere.ts +9 -0
- package/src/useClickOut/useClickOut.test.tsx +78 -0
- package/src/useClickOut/useClickOut.ts +25 -0
- package/src/useCurrency/useCurrency.test.tsx +33 -0
- package/src/useCurrency/useCurrency.ts +42 -0
- package/src/useCurrentState/useCurrentState.test.tsx +56 -0
- package/src/useCurrentState/useCurrentState.ts +22 -0
- package/src/useDateTimeFormat/useDateTimeFormat.test.tsx +47 -0
- package/src/useDateTimeFormat/useDateTimeFormat.ts +22 -0
- package/src/useDebounce/useDebounce.test.ts +49 -0
- package/src/useDebounce/useDebounce.ts +15 -0
- package/src/useDebounceCallback/useDebounceCallback.test.ts +34 -0
- package/src/useDebounceCallback/useDebounceCallback.ts +102 -0
- package/src/useDebugChangedProps/useDebugChangedProps.test.tsx +21 -0
- package/src/useDebugChangedProps/useDebugChangedProps.ts +17 -0
- package/src/useDomObserver/useDomObserver.test.ts +22 -0
- package/src/useDomObserver/useDomObserver.ts +26 -0
- package/src/useDrawer/useDrawer.test.ts +17 -0
- package/src/useDrawer/useDrawer.ts +16 -0
- package/src/useElementSize/useElementSize.test.ts +108 -0
- package/src/useElementSize/useElementSize.ts +34 -0
- package/src/useEventListener/useEventListener.test.ts +128 -0
- package/src/useEventListener/useEventListener.ts +43 -0
- package/src/useFilters/filter.service.ts +86 -0
- package/src/useFilters/filter.ts +152 -0
- package/src/useFilters/useFilters.test.ts +48 -0
- package/src/useFilters/useFilters.ts +44 -0
- package/src/useGenericContext/useGenericContext.test.tsx +19 -0
- package/src/useGenericContext/useGenericContext.ts +18 -0
- package/src/useGeolocation/geolocation.service.ts +66 -0
- package/src/useGeolocation/useGeolocation.test.ts +29 -0
- package/src/useGeolocation/useGeolocation.ts +56 -0
- package/src/useHttp/useHttp.test.tsx +192 -0
- package/src/useHttp/useHttp.ts +52 -0
- package/src/useIdle/useIdle.test.ts +31 -0
- package/src/useIdle/useIdle.ts +79 -0
- package/src/useInfiniteLoader/useInfiniteLoader.test.ts +41 -0
- package/src/useInfiniteLoader/useInfiniteLoader.ts +46 -0
- package/src/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.ts +3 -0
- package/src/useKeyboard/codes.ts +94 -0
- package/src/useKeyboard/helper.ts +31 -0
- package/src/useKeyboard/useKeyboard.test.tsx +163 -0
- package/src/useKeyboard/useKeyboard.ts +91 -0
- package/src/useLabel/useLabel.test.tsx +68 -0
- package/src/useLabel/useLabel.tsx +43 -0
- package/src/useLayout/useLayout.test.tsx +32 -0
- package/src/useLayout/useLayout.tsx +19 -0
- package/src/useModal/useModal.test.ts +19 -0
- package/src/useModal/useModal.ts +16 -0
- package/src/useMounted/useMounted.test.tsx +34 -0
- package/src/useMounted/useMounted.ts +25 -0
- package/src/useNumber/useNumber.test.tsx +36 -0
- package/src/useNumber/useNumber.ts +15 -0
- package/src/usePage/usePage.test.tsx +31 -0
- package/src/usePage/usePage.tsx +19 -0
- package/src/usePagination/pagination.service.ts +23 -0
- package/src/usePagination/usePagination.test.ts +64 -0
- package/src/usePagination/usePagination.ts +47 -0
- package/src/usePortal/usePortal.test.ts +24 -0
- package/src/usePortal/usePortal.ts +34 -0
- package/src/usePrevious/usePrevious.test.ts +21 -0
- package/src/usePrevious/usePrevious.ts +9 -0
- package/src/useRealSize/useRealSize.ts +41 -0
- package/src/useResize/useResize.test.ts +31 -0
- package/src/useResize/useResize.ts +18 -0
- package/src/useSSR/useSSR.test.tsx +16 -0
- package/src/useSSR/useSSR.ts +23 -0
- package/src/useScroll/useScroll.test.tsx +25 -0
- package/src/useScroll/useScroll.ts +30 -0
- package/src/useScrollTo/useScrollTo.test.tsx +105 -0
- package/src/useScrollTo/useScrollTo.ts +41 -0
- package/src/useSearchParams/useSearchParams.test.ts +59 -0
- package/src/useSearchParams/useSearchParams.ts +123 -0
- package/src/useSearchParamsEncoded/useSearchParamsEncoded.test.ts +58 -0
- package/src/useSearchParamsEncoded/useSearchParamsEncoded.ts +202 -0
- package/src/useSorting/useSorting.test.ts +40 -0
- package/src/useSorting/useSorting.ts +99 -0
- package/src/useSwiperIndex/useSwiperIndex.ts +30 -0
- package/src/useSyncedRef/useSyncedRef.test.ts +16 -0
- package/src/useSyncedRef/useSyncedRef.ts +18 -0
- package/src/useUI/useUI.test.ts +16 -0
- package/src/useUI/useUI.ts +44 -0
- package/src/useUnit/useUnit.test.tsx +33 -0
- package/src/useUnit/useUnit.ts +5 -0
- package/src/useUnmountEffect/useUnmountEffect.test.ts +15 -0
- package/src/useUnmountEffect/useUnmountEffect.ts +16 -0
- package/src/useUser/useUser.test.ts +22 -0
- package/src/useUser/useUser.ts +20 -0
- package/src/useWindowSize/useWindowSize.test.ts +48 -0
- package/src/useWindowSize/useWindowSize.ts +31 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// readme https://testing-library.com/
|
|
2
|
+
import { renderHook } from '@testing-library/react';
|
|
3
|
+
import { useUnmountEffect } from './useUnmountEffect';
|
|
4
|
+
|
|
5
|
+
describe('useUnmountEffect', () => {
|
|
6
|
+
|
|
7
|
+
test('should run callback on unmount', () => {
|
|
8
|
+
const callback = jest.fn();
|
|
9
|
+
const { unmount } = renderHook(() => useUnmountEffect(callback));
|
|
10
|
+
expect(callback).toHaveBeenCalledTimes(0);
|
|
11
|
+
unmount();
|
|
12
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { useSyncedRef } from '../useSyncedRef/useSyncedRef';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Run effect only when component is unmounted.
|
|
6
|
+
*
|
|
7
|
+
* @param effect Effector to run on unmount
|
|
8
|
+
*/
|
|
9
|
+
export function useUnmountEffect(effect: CallableFunction): void {
|
|
10
|
+
const effectRef = useSyncedRef(effect);
|
|
11
|
+
|
|
12
|
+
useEffect(() => () => {
|
|
13
|
+
effectRef.current();
|
|
14
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
15
|
+
}, []);
|
|
16
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
// readme https://testing-library.com/
|
|
4
|
+
import { act, renderHook } from '@testing-library/react';
|
|
5
|
+
import { useUser } from './useUser';
|
|
6
|
+
|
|
7
|
+
describe('useUser', () => {
|
|
8
|
+
it('should work correctly', async () => {
|
|
9
|
+
const user = {
|
|
10
|
+
id: 1,
|
|
11
|
+
firstName: 'Jhon',
|
|
12
|
+
lastName: 'Appleseed',
|
|
13
|
+
email: 'jhon.appleseed@gmail.com',
|
|
14
|
+
};
|
|
15
|
+
const { result } = renderHook(() => useUser(state => state));
|
|
16
|
+
let state = result.current;
|
|
17
|
+
expect(state.user).toBe(undefined);
|
|
18
|
+
act(() => state.actions.setUser(user));
|
|
19
|
+
state = result.current;
|
|
20
|
+
expect(state.user).toBe(user);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { IUser } from '@websolutespa/bom-mixer-models';
|
|
2
|
+
import { create } from 'zustand';
|
|
3
|
+
|
|
4
|
+
type UserState = {
|
|
5
|
+
user?: IUser;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type UserActions = {
|
|
9
|
+
setUser: (user?: IUser) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type UserStore = UserState & {
|
|
13
|
+
actions: UserActions;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const useUser = create<UserStore>((set) => ({
|
|
17
|
+
actions: {
|
|
18
|
+
setUser: (user?: IUser) => set((state) => ({ user })),
|
|
19
|
+
},
|
|
20
|
+
}));
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// readme https://testing-library.com/
|
|
2
|
+
import { act, renderHook } from '@testing-library/react';
|
|
3
|
+
import { useWindowSize } from './useWindowSize';
|
|
4
|
+
|
|
5
|
+
const setupHook = () => renderHook(() => useWindowSize());
|
|
6
|
+
|
|
7
|
+
const windowResize = (dimension: 'width' | 'height', value: number): void => {
|
|
8
|
+
if (dimension === 'width') {
|
|
9
|
+
window.innerWidth = value;
|
|
10
|
+
}
|
|
11
|
+
if (dimension === 'height') {
|
|
12
|
+
window.innerHeight = value;
|
|
13
|
+
}
|
|
14
|
+
window.dispatchEvent(new Event('resize'));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
describe('useElementSize', () => {
|
|
18
|
+
it('should initialize', () => {
|
|
19
|
+
const { result } = setupHook();
|
|
20
|
+
const { height, width } = result.current;
|
|
21
|
+
expect(typeof height).toBe('number');
|
|
22
|
+
expect(typeof width).toBe('number');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return the corresponding height', () => {
|
|
26
|
+
const { result } = setupHook();
|
|
27
|
+
act(() => {
|
|
28
|
+
windowResize('height', 420);
|
|
29
|
+
});
|
|
30
|
+
expect(result.current.height).toBe(420);
|
|
31
|
+
act(() => {
|
|
32
|
+
windowResize('height', 2196);
|
|
33
|
+
});
|
|
34
|
+
expect(result.current.height).toBe(2196);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return the corresponding width', () => {
|
|
38
|
+
const { result } = setupHook();
|
|
39
|
+
act(() => {
|
|
40
|
+
windowResize('width', 420);
|
|
41
|
+
});
|
|
42
|
+
expect(result.current.width).toBe(420);
|
|
43
|
+
act(() => {
|
|
44
|
+
windowResize('width', 2196);
|
|
45
|
+
});
|
|
46
|
+
expect(result.current.width).toBe(2196);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { useEventListener } from '../useEventListener/useEventListener';
|
|
3
|
+
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect/useIsomorphicLayoutEffect';
|
|
4
|
+
|
|
5
|
+
type WindowSize = {
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function useWindowSize(): WindowSize {
|
|
11
|
+
|
|
12
|
+
const [windowSize, setWindowSize] = useState<WindowSize>({
|
|
13
|
+
width: 0, height: 0,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const handleSize = () => {
|
|
17
|
+
const width = window.innerWidth;
|
|
18
|
+
const height = window.innerHeight;
|
|
19
|
+
setWindowSize({ width, height });
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
useEventListener('resize', handleSize);
|
|
23
|
+
|
|
24
|
+
// Set size at the first client-side load
|
|
25
|
+
useIsomorphicLayoutEffect(() => {
|
|
26
|
+
handleSize();
|
|
27
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
return windowSize;
|
|
31
|
+
}
|