-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMapSearchOperationalPoint.tsx
172 lines (160 loc) · 5.51 KB
/
MapSearchOperationalPoint.tsx
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
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
import { useState } from 'react';
import cx from 'classnames';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import type { SearchResultItemOperationalPoint } from 'common/api/osrdEditoastApi';
import CheckboxRadioSNCF from 'common/BootstrapSNCF/CheckboxRadioSNCF';
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
import { onResultSearchClick } from 'common/Map/utils';
import type { Viewport } from 'reducers/map';
import { getMap } from 'reducers/map/selectors';
import { useAppDispatch } from 'store';
import useSearchOperationalPoint, { MAIN_OP_CH_CODES } from './useSearchOperationalPoint';
type MapSearchOperationalPointProps = {
updateExtViewport: (viewport: Partial<Viewport>) => void;
closeMapSearchPopUp: () => void;
};
const MapSearchOperationalPoint = ({
updateExtViewport,
closeMapSearchPopUp,
}: MapSearchOperationalPointProps) => {
const map = useSelector(getMap);
const {
searchTerm,
chCodeFilter,
searchResults,
searchResultsFilteredByCh,
mainOperationalPointsOnly,
setSearchTerm,
setChCodeFilter,
setSearchResults,
setMainOperationalPointsOnly,
} = useSearchOperationalPoint();
const dispatch = useAppDispatch();
const { t } = useTranslation(['map-search']);
const onResultClick = (result: SearchResultItemOperationalPoint) => {
onResultSearchClick({
result,
map,
updateExtViewport,
dispatch,
title: result.name,
});
closeMapSearchPopUp();
};
const [selectedResultIndex, setSelectedResultIndex] = useState(-1);
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
switch (event.key) {
case 'ArrowUp':
setSelectedResultIndex((prevIndex) => {
const newIndex = prevIndex > 0 ? prevIndex - 1 : searchResults.length - 1;
const element = document.getElementById(`result-${newIndex}`);
if (element) {
element.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}
return newIndex;
});
break;
case 'ArrowDown':
setSelectedResultIndex((prevIndex) => {
const newIndex = prevIndex < searchResults.length - 1 ? prevIndex + 1 : 0;
const element = document.getElementById(`result-${newIndex}`);
if (element) {
element.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}
return newIndex;
});
break;
default:
break;
}
};
return (
<div className="mt-2">
<div className="d-flex mb-2 flex-column flex-md-row">
<span className="flex-fill col-md-6 col-xl-7 pl-0 mb-2">
<InputSNCF
id="map-search-operational-points"
name="map-search-operational-points"
placeholder={t('placeholdername')}
title={t('placeholdername')}
inputProps={{ onKeyDown: handleKeyDown }}
type="text"
value={searchTerm}
onChange={(e) => {
setSelectedResultIndex(-1);
setSearchTerm(e.target.value);
}}
onClear={() => {
setSearchTerm('');
setSearchResults([]);
}}
clearButton
noMargin
sm
focus
/>
</span>
<span className="col-md-3 pl-0 mb-2">
<InputSNCF
id="map-search-operational-points-ch-code"
type="text"
placeholder={t('placeholderchcode')}
onChange={(e) => {
setChCodeFilter(e.target.value || undefined);
}}
onClear={() => setChCodeFilter(undefined)}
value={chCodeFilter}
disabled={mainOperationalPointsOnly}
clearButton
noMargin
sm
/>
</span>
<span className="col-md-3 col-xl-2 pr-2 pl-0 mt-md-1">
<CheckboxRadioSNCF
id="map-search-operational-points-main-only"
type="checkbox"
label={t('mainOperationalPointsOnly')}
checked={mainOperationalPointsOnly}
onChange={() => setMainOperationalPointsOnly(!mainOperationalPointsOnly)}
/>
</span>
</div>
<h2 className="text-center mt-3">
{searchResults.length > 100
? t('resultsCountTooMuch')
: t('resultsCount', {
count: searchResultsFilteredByCh.length,
})}
</h2>
<div className="search-results">
{searchResults.length > 0 &&
searchResults.length <= 100 &&
searchResultsFilteredByCh.map((searchResult, index) => (
<button
id={`result-${index}`}
type="button"
className={cx('search-result-item', {
main: MAIN_OP_CH_CODES.includes(searchResult.ch),
selected: index === selectedResultIndex,
})}
key={`mapSearchOperationalPoint-${searchResult.obj_id}`}
onClick={() => onResultClick(searchResult)}
tabIndex={-1}
>
<span className="trigram">{searchResult.trigram}</span>
<span className="name">
{searchResult.name}
{!MAIN_OP_CH_CODES.includes(searchResult.ch) && (
<span className="ch">{searchResult.ch ?? ''}</span>
)}
</span>
<span className="uic">{searchResult.ci}</span>
</button>
))}
</div>
</div>
);
};
export default MapSearchOperationalPoint;