-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathListItemLocation.jsx
More file actions
100 lines (81 loc) · 3.75 KB
/
ListItemLocation.jsx
File metadata and controls
100 lines (81 loc) · 3.75 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
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
import { useEffect, useRef } from 'react';
import { useRecoilValue } from 'recoil';
import mapsIndoorsInstanceState from '../../../atoms/mapsIndoorsInstanceState';
import { useTranslation } from 'react-i18next';
import './ListItemLocation.scss';
import showExternalIDsState from '../../../atoms/showExternalIDsState';
import PropTypes from 'prop-types';
import { debounce } from 'lodash';
ListItemLocation.propTypes = {
location: PropTypes.object,
locationClicked: PropTypes.func,
icon: PropTypes.string,
isHovered: PropTypes.bool,
disableHover: PropTypes.bool
};
/**
* React wrapper around the custom element <mi-list-item-location>.
*
* @param {object} props
* @param {object} location - MapsIndoors Location
* @param {function} locationClicked - Function that is called when Location is clicked.
* @param {string} icon - The icon to be shown in the list item location component.
* @param {boolean} isHovered - Check if the location is hovered.
* @param {boolean} disableHover - Disable hover functionality to prevent dual pins during routing.
*/
function ListItemLocation({ location, locationClicked, icon, isHovered, disableHover }) {
const { t } = useTranslation();
const elementRef = useRef();
const mapsIndoorsInstance = useRecoilValue(mapsIndoorsInstanceState);
const showExternalIDs = useRecoilValue(showExternalIDsState);
useEffect(() => {
const clickHandler = customEvent => {
mapsIndoorsInstance.unhoverLocation();
locationClicked(customEvent.detail);
};
const hoverHandler = debounce(() => {
// Skip hover functionality if disabled (e.g., during routing to prevent dual pins)
if (disableHover) return;
// Check if the location is non-selectable before hovering it
if (location.properties.locationSettings?.selectable !== false) {
mapsIndoorsInstance.hoverLocation(location);
}
});
const unhoverHandler = debounce(() => {
// Skip unhover functionality if disabled
if (disableHover) return;
// Check if the location is non-selectable before unhovering it
if (location.properties.locationSettings?.selectable !== false) {
mapsIndoorsInstance.unhoverLocation(location);
}
});
// Add a "non-selectable" class to the non-selectable locations.
if (location.properties.locationSettings?.selectable === false) {
elementRef.current.classList.add('non-selectable');
}
const { current } = elementRef;
current.location = location;
current.icon = icon ? icon : mapsIndoorsInstance.getDisplayRule(location).icon;
current.addEventListener('locationClicked', clickHandler);
// Only add hover listeners if hover is not disabled
if (!disableHover) {
current.addEventListener('mouseover', hoverHandler);
current.addEventListener('mouseout', unhoverHandler);
}
if (isHovered) {
elementRef.current.classList.add('hovered')
} else {
elementRef.current.classList.remove('hovered')
}
return () => {
current.removeEventListener('locationClicked', clickHandler);
// Only remove hover listeners if they were added
if (!disableHover) {
current.removeEventListener('mouseover', hoverHandler);
current.removeEventListener('mouseout', unhoverHandler);
}
}
}, [location, locationClicked, isHovered, disableHover]);
return <mi-list-item-location level={t('Level')} ref={elementRef} show-external-id={showExternalIDs} />
}
export default ListItemLocation;