-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathInteractiveElementWrapper.tsx
More file actions
64 lines (60 loc) · 1.58 KB
/
InteractiveElementWrapper.tsx
File metadata and controls
64 lines (60 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import { useInteractiveElement, type InteractiveElementProps } from '../../hooks/useInteractiveElement';
interface InteractiveElementWrapperProps extends InteractiveElementProps {
children: React.ReactNode;
className?: string;
style?: React.CSSProperties;
role?: string;
tabIndex?: number;
onKeyDown?: (e: React.KeyboardEvent) => void;
onClick?: (e: React.MouseEvent) => void;
}
/**
* A wrapper component that automatically handles:
* 1. Generates the correct DOM ID for the builder.
* 2. Checks the selection state.
* 3. Applies the 'builder-highlight' class if selected.
* 4. Handles click events to update the selection (stopping propagation).
*/
export const InteractiveElementWrapper: React.FC<InteractiveElementWrapperProps> = ({
children,
kind,
label,
pageIndex,
sectionIndex,
questionIndex,
className = '',
style,
role,
tabIndex,
onKeyDown,
onClick: externalOnClick, // Allow passing an extra click handler if absolutely necessary
}) => {
const { id, highlightClass, handleClick } = useInteractiveElement({
kind,
label,
pageIndex,
sectionIndex,
questionIndex,
});
const combinedClickHandler = (e: React.MouseEvent) => {
handleClick(e);
if (externalOnClick) {
externalOnClick(e);
}
};
return (
<div
id={id}
className={`${className} ${highlightClass}`.trim()}
onClick={combinedClickHandler}
style={style}
role={role}
tabIndex={tabIndex}
onKeyDown={onKeyDown}
data-testid={`${kind}-wrapper`}
>
{children}
</div>
);
};