react-artasys-ui 0.1.14 → 0.1.16
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 +1 -1
- package/dist/index.js.map +1 -1
- package/lib/Dropdown/Dropdown.d.ts +3 -1
- package/lib/Dropdown/Items.d.ts +6 -0
- package/package.json +1 -1
- package/src/Dropdown/Dropdown.tsx +18 -15
- package/src/Dropdown/Items.tsx +37 -0
- package/src/Dropdown/style.module.css +17 -0
|
@@ -8,6 +8,8 @@ export interface IDropdown extends AllHTMLAttributes<HTMLDivElement> {
|
|
|
8
8
|
hover?: boolean;
|
|
9
9
|
items?: FunctionComponentElement<IItem> | FunctionComponentElement<IItem>[];
|
|
10
10
|
disabled?: boolean;
|
|
11
|
+
onShow?: () => void;
|
|
12
|
+
onHide?: () => void;
|
|
11
13
|
}
|
|
12
|
-
declare const Dropdown: ({ children, className, items, direction, position, split, disabled, hover, ...props }: IDropdown) => JSX.Element;
|
|
14
|
+
declare const Dropdown: ({ children, className, items, direction, position, split, disabled, hover, onShow, onHide, ...props }: IDropdown) => JSX.Element;
|
|
13
15
|
export default Dropdown;
|
package/package.json
CHANGED
|
@@ -4,7 +4,9 @@ import {
|
|
|
4
4
|
useState,
|
|
5
5
|
FunctionComponentElement,
|
|
6
6
|
useEffect,
|
|
7
|
-
createContext
|
|
7
|
+
createContext,
|
|
8
|
+
Children,
|
|
9
|
+
type FocusEvent
|
|
8
10
|
} from "react";
|
|
9
11
|
import styles from "./style.module.css";
|
|
10
12
|
import Arrow from "../Components/Arrow";
|
|
@@ -12,6 +14,7 @@ import type {
|
|
|
12
14
|
IItem,
|
|
13
15
|
TChildrenAction
|
|
14
16
|
} from "./Item";
|
|
17
|
+
import Items from "./Items";
|
|
15
18
|
|
|
16
19
|
export const Context = createContext<TChildrenAction>({
|
|
17
20
|
close: () => {}
|
|
@@ -24,9 +27,11 @@ export interface IDropdown extends AllHTMLAttributes<HTMLDivElement> {
|
|
|
24
27
|
hover?: boolean;
|
|
25
28
|
items?: FunctionComponentElement<IItem> | FunctionComponentElement<IItem>[];
|
|
26
29
|
disabled?: boolean;
|
|
30
|
+
onShow?: () => void;
|
|
31
|
+
onHide?: () => void;
|
|
27
32
|
};
|
|
28
33
|
|
|
29
|
-
const Dropdown = ({children, className, items, direction = 'down', position = 'right', split = false, disabled, hover = false, ...props}: IDropdown) => {
|
|
34
|
+
const Dropdown = ({children, className, items, direction = 'down', position = 'right', split = false, disabled, hover = false, onShow, onHide, ...props}: IDropdown) => {
|
|
30
35
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
31
36
|
const hoverTimeout = useRef<ReturnType<typeof setTimeout>>();
|
|
32
37
|
const [isOpen, setOpen] = useState(false);
|
|
@@ -47,15 +52,11 @@ const Dropdown = ({children, className, items, direction = 'down', position = 'r
|
|
|
47
52
|
};
|
|
48
53
|
|
|
49
54
|
const handleClick = () => {
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
if (hover) {
|
|
54
|
-
close();
|
|
55
|
-
}
|
|
55
|
+
if (hoverTimeout.current) return;
|
|
56
|
+
if (!split || hover) toggle();
|
|
56
57
|
};
|
|
57
58
|
|
|
58
|
-
const handleBlur = (e:
|
|
59
|
+
const handleBlur = (e: FocusEvent) => {
|
|
59
60
|
if (e.currentTarget.contains(e.relatedTarget)) return;
|
|
60
61
|
close();
|
|
61
62
|
};
|
|
@@ -77,8 +78,14 @@ const Dropdown = ({children, className, items, direction = 'down', position = 'r
|
|
|
77
78
|
if (isOpen) {
|
|
78
79
|
classList?.add(styles['opened']);
|
|
79
80
|
element?.focus();
|
|
81
|
+
if (typeof onShow === 'function') {
|
|
82
|
+
onShow();
|
|
83
|
+
}
|
|
80
84
|
}else{
|
|
81
85
|
classList?.remove(styles['opened']);
|
|
86
|
+
if (typeof onHide === 'function') {
|
|
87
|
+
onHide();
|
|
88
|
+
}
|
|
82
89
|
}
|
|
83
90
|
}, [isOpen]);
|
|
84
91
|
|
|
@@ -93,15 +100,11 @@ const Dropdown = ({children, className, items, direction = 'down', position = 'r
|
|
|
93
100
|
}}>
|
|
94
101
|
<div {...props} className={classes.join(' ')} ref={containerRef} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseOut} tabIndex={1} onBlur={handleBlur}>
|
|
95
102
|
{(position === 'left' && !disabled) && <Arrow className={styles['arrow']} onClick={handleClickArrow}/>}
|
|
96
|
-
<div onClick={handleClick}>
|
|
103
|
+
<div onClick={handleClick} className={'ui-dropdown-block' + (isOpen ? ' ' + styles['hide'] : '')}>
|
|
97
104
|
{children}
|
|
98
105
|
</div>
|
|
99
106
|
{(position === 'right' && !disabled) && <Arrow className={styles['arrow']} onClick={handleClickArrow}/>}
|
|
100
|
-
{
|
|
101
|
-
(items && !disabled) && <ul className={styles['dropdown-list']}>
|
|
102
|
-
{items}
|
|
103
|
-
</ul>
|
|
104
|
-
}
|
|
107
|
+
<Items isOpen={isOpen} disabled={disabled} items={items}/>
|
|
105
108
|
</div>
|
|
106
109
|
</Context.Provider>);
|
|
107
110
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import styles from "./style.module.css";
|
|
2
|
+
import type { IDropdown } from "./Dropdown";
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
|
+
|
|
5
|
+
interface IItems extends Pick<IDropdown, 'items' | 'disabled'> {
|
|
6
|
+
isOpen: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const Items = ({items, isOpen, disabled}: IItems) => {
|
|
10
|
+
const listRef = useRef<HTMLUListElement>(null);
|
|
11
|
+
const [hide, setHide] = useState(false);
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!listRef.current) return;
|
|
15
|
+
const screenWidth = window.innerWidth;
|
|
16
|
+
const element = listRef.current.getBoundingClientRect();
|
|
17
|
+
if (isOpen) {
|
|
18
|
+
listRef.current.ontransitionend = () => {
|
|
19
|
+
setHide(true);
|
|
20
|
+
};
|
|
21
|
+
}else{
|
|
22
|
+
setHide(false);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// console.log(element.left)
|
|
26
|
+
if (element.left <= 0) {
|
|
27
|
+
|
|
28
|
+
// listRef.current.style.transform = 'translateX(' + Math.abs(element.left) + 'px)';
|
|
29
|
+
}
|
|
30
|
+
},[isOpen]);
|
|
31
|
+
|
|
32
|
+
if (disabled || hide) return null;
|
|
33
|
+
|
|
34
|
+
return(<ul className={styles['dropdown-list']} children={items} ref={listRef}/>);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default Items;
|
|
@@ -9,6 +9,23 @@
|
|
|
9
9
|
cursor: pointer;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
.container > :global(.ui-dropdown-block) {
|
|
13
|
+
position: relative;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.container > :global(.ui-dropdown-block)::after {
|
|
17
|
+
content: "";
|
|
18
|
+
position: absolute;
|
|
19
|
+
left: 0;
|
|
20
|
+
top: 0;
|
|
21
|
+
width: 100%;
|
|
22
|
+
height: 100%;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.container > :global(.ui-dropdown-block).hide::after {
|
|
26
|
+
display: none;
|
|
27
|
+
}
|
|
28
|
+
|
|
12
29
|
.arrow {
|
|
13
30
|
padding: 5px;
|
|
14
31
|
}
|