kelt-ui-kit-react 2.0.5 → 2.0.6
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 +1513 -1513
- package/package.json +1 -1
- package/src/expands/expand/expand.tsx +191 -186
package/package.json
CHANGED
|
@@ -1,186 +1,191 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { Icon } from "../../icon/Icon";
|
|
3
|
-
import { ExpandInterface } from "./expand.interface";
|
|
4
|
-
|
|
5
|
-
export type ExpandProps = {
|
|
6
|
-
currentExpand?: ExpandInterface;
|
|
7
|
-
item: ExpandInterface;
|
|
8
|
-
className?: string;
|
|
9
|
-
showArrow?: boolean;
|
|
10
|
-
catchfocusCapture?: boolean
|
|
11
|
-
catchFocusCaptureId?: number;
|
|
12
|
-
onChange: (item: ExpandInterface) => void;
|
|
13
|
-
};
|
|
14
|
-
export const Expand = ({
|
|
15
|
-
currentExpand,
|
|
16
|
-
item,
|
|
17
|
-
className,
|
|
18
|
-
showArrow = true,
|
|
19
|
-
catchfocusCapture = false,
|
|
20
|
-
catchFocusCaptureId,
|
|
21
|
-
onChange,
|
|
22
|
-
}: ExpandProps): JSX.Element => {
|
|
23
|
-
const [active, setActive] = useState<boolean>(false);
|
|
24
|
-
const expandRef = useRef<HTMLDivElement>(null);
|
|
25
|
-
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
if (item && currentExpand) {
|
|
28
|
-
if (currentExpand.id === item.id) {
|
|
29
|
-
if (!active) {
|
|
30
|
-
setActive(true);
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
setActive(false);
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
setActive(false);
|
|
37
|
-
}
|
|
38
|
-
}, [currentExpand, setActive, item, active]);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
scrollParent.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { Icon } from "../../icon/Icon";
|
|
3
|
+
import { ExpandInterface } from "./expand.interface";
|
|
4
|
+
|
|
5
|
+
export type ExpandProps = {
|
|
6
|
+
currentExpand?: ExpandInterface;
|
|
7
|
+
item: ExpandInterface;
|
|
8
|
+
className?: string;
|
|
9
|
+
showArrow?: boolean;
|
|
10
|
+
catchfocusCapture?: boolean
|
|
11
|
+
catchFocusCaptureId?: number;
|
|
12
|
+
onChange: (item: ExpandInterface) => void;
|
|
13
|
+
};
|
|
14
|
+
export const Expand = ({
|
|
15
|
+
currentExpand,
|
|
16
|
+
item,
|
|
17
|
+
className,
|
|
18
|
+
showArrow = true,
|
|
19
|
+
catchfocusCapture = false,
|
|
20
|
+
catchFocusCaptureId,
|
|
21
|
+
onChange,
|
|
22
|
+
}: ExpandProps): JSX.Element => {
|
|
23
|
+
const [active, setActive] = useState<boolean>(false);
|
|
24
|
+
const expandRef = useRef<HTMLDivElement>(null);
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (item && currentExpand) {
|
|
28
|
+
if (currentExpand.id === item.id) {
|
|
29
|
+
if (!active) {
|
|
30
|
+
setActive(true);
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
setActive(false);
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
setActive(false);
|
|
37
|
+
}
|
|
38
|
+
}, [currentExpand, setActive, item, active]);
|
|
39
|
+
|
|
40
|
+
// Si catchFocusCaptureId est fourni, seul l'expand portant cet id gère le focus / scroll
|
|
41
|
+
const canCatchFocus =
|
|
42
|
+
catchfocusCapture &&
|
|
43
|
+
(catchFocusCaptureId === undefined || catchFocusCaptureId === item.id);
|
|
44
|
+
|
|
45
|
+
const scrollToHeader = useCallback(() => {
|
|
46
|
+
const expandElement = expandRef.current;
|
|
47
|
+
if (!expandElement) return;
|
|
48
|
+
|
|
49
|
+
let scrollParent: HTMLElement | null = expandElement.parentElement;
|
|
50
|
+
while (scrollParent) {
|
|
51
|
+
const style = window.getComputedStyle(scrollParent);
|
|
52
|
+
const isScrollable =
|
|
53
|
+
/(auto|scroll)/.test(style.overflow + style.overflowY) &&
|
|
54
|
+
scrollParent.scrollHeight > scrollParent.clientHeight;
|
|
55
|
+
|
|
56
|
+
if (isScrollable) {
|
|
57
|
+
const parentRect = scrollParent.getBoundingClientRect();
|
|
58
|
+
const targetRect = expandElement.getBoundingClientRect();
|
|
59
|
+
const offsetTop =
|
|
60
|
+
targetRect.top - parentRect.top + scrollParent.scrollTop;
|
|
61
|
+
|
|
62
|
+
scrollParent.scrollTo({
|
|
63
|
+
top: offsetTop,
|
|
64
|
+
behavior: "smooth",
|
|
65
|
+
});
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
scrollParent = scrollParent.parentElement;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const header =
|
|
72
|
+
expandElement.querySelector<HTMLElement>(".expand-header") ||
|
|
73
|
+
expandElement;
|
|
74
|
+
header.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
75
|
+
}, []);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (!canCatchFocus) return;
|
|
79
|
+
if (active) {
|
|
80
|
+
const timer1 = setTimeout(() => {
|
|
81
|
+
scrollToHeader();
|
|
82
|
+
}, 100);
|
|
83
|
+
const timer2 = setTimeout(() => {
|
|
84
|
+
scrollToHeader();
|
|
85
|
+
}, 350);
|
|
86
|
+
return () => {
|
|
87
|
+
clearTimeout(timer1);
|
|
88
|
+
clearTimeout(timer2);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}, [active, scrollToHeader, canCatchFocus]);
|
|
92
|
+
|
|
93
|
+
const handleFocusCapture = useCallback(
|
|
94
|
+
(e: React.FocusEvent<HTMLDivElement>) => {
|
|
95
|
+
if (!canCatchFocus) return;
|
|
96
|
+
const target = e.target as HTMLElement;
|
|
97
|
+
if (
|
|
98
|
+
active &&
|
|
99
|
+
(target.tagName === "INPUT" ||
|
|
100
|
+
target.tagName === "TEXTAREA" ||
|
|
101
|
+
target.tagName === "SELECT")
|
|
102
|
+
) {
|
|
103
|
+
const expandElement = expandRef.current;
|
|
104
|
+
if (!expandElement) return;
|
|
105
|
+
|
|
106
|
+
const performScroll = () => {
|
|
107
|
+
let scrollParent: HTMLElement | null = expandElement.parentElement;
|
|
108
|
+
while (scrollParent) {
|
|
109
|
+
const style = window.getComputedStyle(scrollParent);
|
|
110
|
+
const isScrollable =
|
|
111
|
+
/(auto|scroll)/.test(style.overflow + style.overflowY) &&
|
|
112
|
+
scrollParent.scrollHeight > scrollParent.clientHeight;
|
|
113
|
+
|
|
114
|
+
if (isScrollable) {
|
|
115
|
+
const parentRect = scrollParent.getBoundingClientRect();
|
|
116
|
+
const headerRect = expandElement.getBoundingClientRect();
|
|
117
|
+
const headerOffsetTop =
|
|
118
|
+
headerRect.top - parentRect.top + scrollParent.scrollTop;
|
|
119
|
+
|
|
120
|
+
// Si on a déjà scrollé vers ou au-delà du header, on ne remonte JAMAIS au header
|
|
121
|
+
if (scrollParent.scrollTop >= headerOffsetTop - 10) {
|
|
122
|
+
const targetRect = target.getBoundingClientRect();
|
|
123
|
+
if (targetRect.bottom > parentRect.bottom) {
|
|
124
|
+
target.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Si on n'a pas encore scrollé jusqu'au header, on y va
|
|
130
|
+
scrollParent.scrollTo({
|
|
131
|
+
top: headerOffsetTop,
|
|
132
|
+
behavior: "smooth",
|
|
133
|
+
});
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
scrollParent = scrollParent.parentElement;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
performScroll();
|
|
141
|
+
// Répéter après l'ouverture du clavier virtuel (spécifique tablettes / mobiles Android)
|
|
142
|
+
setTimeout(performScroll, 300);
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
[active, canCatchFocus],
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const handleChange = useCallback(
|
|
149
|
+
(e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
|
|
150
|
+
e.stopPropagation();
|
|
151
|
+
if (onChange) {
|
|
152
|
+
onChange(item);
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
[onChange, item],
|
|
156
|
+
);
|
|
157
|
+
return (
|
|
158
|
+
<div
|
|
159
|
+
ref={expandRef}
|
|
160
|
+
onClick={handleChange}
|
|
161
|
+
onFocusCapture={handleFocusCapture}
|
|
162
|
+
className={`expand ${active ? "expand--active" : ""} ${className ?? ""}`}
|
|
163
|
+
>
|
|
164
|
+
<div className={`expand-header`}>
|
|
165
|
+
<div className="expand-header-content">
|
|
166
|
+
<div className="expand-header-content-default">
|
|
167
|
+
{active && showArrow && (
|
|
168
|
+
<Icon classIcon="bi bi-chevron-down mr-2"></Icon>
|
|
169
|
+
)}
|
|
170
|
+
{!active && showArrow && (
|
|
171
|
+
<Icon classIcon="bi bi-chevron-right mr-2"></Icon>
|
|
172
|
+
)}
|
|
173
|
+
<span className="flex-1 expand-header-title-left">
|
|
174
|
+
{item.title}
|
|
175
|
+
</span>
|
|
176
|
+
{item.description && (
|
|
177
|
+
<span className="expand-header-title-right">
|
|
178
|
+
{item.description}
|
|
179
|
+
</span>
|
|
180
|
+
)}
|
|
181
|
+
</div>
|
|
182
|
+
</div>
|
|
183
|
+
</div>
|
|
184
|
+
<div className="expand-body">
|
|
185
|
+
<div>{item.headerContent}</div>
|
|
186
|
+
{item.content}
|
|
187
|
+
</div>
|
|
188
|
+
{item.footer && <div className="expand-footer">{item.footer}</div>}
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
};
|