@xqmsg/ui-core 0.15.2 → 0.15.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/hooks/useOnOutsideClick.d.ts +2 -0
- package/dist/ui-core.cjs.development.js +31 -11
- package/dist/ui-core.cjs.development.js.map +1 -1
- package/dist/ui-core.cjs.production.min.js +1 -1
- package/dist/ui-core.cjs.production.min.js.map +1 -1
- package/dist/ui-core.esm.js +32 -12
- package/dist/ui-core.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/input/StackedMultiSelect/index.tsx +3 -12
- package/src/components/input/StackedSelect/StackedSelect.tsx +2 -2
- package/src/components/input/components/dropdown/index.tsx +1 -1
- package/src/hooks/useOnOutsideClick.tsx +31 -0
package/package.json
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import React, { useEffect, useRef, useState } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
Box,
|
|
4
|
-
Flex,
|
|
5
|
-
Text,
|
|
6
|
-
Image,
|
|
7
|
-
useOutsideClick,
|
|
8
|
-
Input,
|
|
9
|
-
} from '@chakra-ui/react';
|
|
2
|
+
import { Box, Flex, Text, Image, Input } from '@chakra-ui/react';
|
|
10
3
|
import {
|
|
11
4
|
FieldOption,
|
|
12
5
|
FieldOptions,
|
|
@@ -24,6 +17,7 @@ import {
|
|
|
24
17
|
import SubtractIcon from '../StackedSelect/assets/svg/subtract.svg';
|
|
25
18
|
import { Dropdown } from '../components/dropdown';
|
|
26
19
|
import Token from '../components/token';
|
|
20
|
+
import { useOnClickOutside } from '../../../hooks/useOnOutsideClick';
|
|
27
21
|
|
|
28
22
|
export interface StackedMultiSelectProps extends ReactSelectFieldProps {
|
|
29
23
|
options: FieldOptions;
|
|
@@ -62,10 +56,7 @@ const StackedMultiSelect = React.forwardRef<
|
|
|
62
56
|
}
|
|
63
57
|
}, [boundingClientRect]);
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
ref: dropdownRef,
|
|
67
|
-
handler: () => setIsFocussed(false),
|
|
68
|
-
});
|
|
59
|
+
useOnClickOutside(dropdownRef, () => setIsFocussed(false));
|
|
69
60
|
|
|
70
61
|
// gets latest watched form value (common delimited) from RHF state and creates a list
|
|
71
62
|
useEffect(() => {
|
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
Input,
|
|
6
6
|
InputGroup,
|
|
7
7
|
InputRightElement,
|
|
8
|
-
useOutsideClick,
|
|
9
8
|
} from '@chakra-ui/react';
|
|
10
9
|
import { FieldOptions } from '../InputTypes';
|
|
11
10
|
import { StackedInputProps } from '../StackedInput/StackedInput';
|
|
@@ -14,6 +13,7 @@ import { UseFormSetValue, FieldValues, Control } from 'react-hook-form';
|
|
|
14
13
|
import SubtractIcon from './assets/svg/subtract.svg';
|
|
15
14
|
import { Dropdown } from '../components/dropdown';
|
|
16
15
|
import useDidMountEffect from '../../../hooks/useDidMountEffect';
|
|
16
|
+
import { useOnClickOutside } from '../../../hooks/useOnOutsideClick';
|
|
17
17
|
|
|
18
18
|
export interface StackedSelectProps extends StackedInputProps {
|
|
19
19
|
options: FieldOptions;
|
|
@@ -57,7 +57,7 @@ const StackedSelect = React.forwardRef<HTMLInputElement, StackedSelectProps>(
|
|
|
57
57
|
);
|
|
58
58
|
}, [value]);
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
useOnClickOutside(dropdownRef, () => setIsFocussed(false));
|
|
61
61
|
|
|
62
62
|
const handleOnSelectItem = (option: {
|
|
63
63
|
label: string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { RefObject, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useOnClickOutside(ref: RefObject<any>, handler: () => void) {
|
|
4
|
+
useEffect(
|
|
5
|
+
() => {
|
|
6
|
+
const listener: EventListener = event => {
|
|
7
|
+
// Do nothing if clicking ref's element or descendent elements
|
|
8
|
+
if (!ref.current || ref.current.contains(event.target)) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
handler();
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
document.addEventListener('mousedown', listener);
|
|
16
|
+
document.addEventListener('touchstart', listener);
|
|
17
|
+
|
|
18
|
+
return () => {
|
|
19
|
+
document.removeEventListener('mousedown', listener);
|
|
20
|
+
document.removeEventListener('touchstart', listener);
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
// Add ref and handler to effect dependencies
|
|
24
|
+
// It's worth noting that because passed in handler is a new ...
|
|
25
|
+
// ... function on every render that will cause this effect ...
|
|
26
|
+
// ... callback/cleanup to run every render. It's not a big deal ...
|
|
27
|
+
// ... but to optimize you can wrap handler in useCallback before ...
|
|
28
|
+
// ... passing it into this hook.
|
|
29
|
+
[ref, handler]
|
|
30
|
+
);
|
|
31
|
+
}
|