@digitransit-component/digitransit-component-favourite-bar 6.0.5 → 7.1.0

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/lib/index.js CHANGED
@@ -1,14 +1,12 @@
1
1
  import PropTypes from 'prop-types';
2
- import React from 'react';
3
- import { withTranslation, useTranslation, I18nextProvider } from 'react-i18next';
2
+ import React, { useState, useRef, useEffect } from 'react';
3
+ import { useTranslation } from 'react-i18next';
4
4
  import cx from 'classnames';
5
- import differenceWith from 'lodash/differenceWith';
6
- import isEqual from 'lodash/isEqual';
7
- import isEmpty from 'lodash/isEmpty';
8
5
  import Shimmer from '@hsl-fi/shimmer';
9
6
  import SuggestionItem from '@digitransit-component/digitransit-component-suggestion-item';
10
7
  import Icon, { defaultColors } from '@digitransit-component/digitransit-component-icon';
11
8
  import 'lodash/uniqWith';
9
+ import 'lodash/isEqual';
12
10
  import memoize from 'lodash/memoize';
13
11
  import escapeRegExp from 'lodash/escapeRegExp';
14
12
  import cloneDeep from 'lodash/cloneDeep';
@@ -158,7 +156,7 @@ function styleInject(css, ref) {
158
156
  }
159
157
  }
160
158
 
161
- var css_248z = ".styles_button-shared__RSwxE, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 {\n background-color: white;\n display: flex;\n flex-direction: row;\n justify-content: center;\n align-items: center;\n border-radius: 5px;\n border: 1px solid #ddd;\n cursor: pointer;\n}\n.styles_button-shared__RSwxE:hover, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf:hover, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1:hover {\n box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);\n}\n\n.styles_shimmer-shared__MGBAa, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 > div {\n display: flex;\n align-items: center;\n overflow: hidden;\n}\n.styles_shimmer-shared__MGBAa .styles_shimmer__bIP9J, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div .styles_shimmer__bIP9J, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 > div .styles_shimmer__bIP9J {\n border-radius: 5px;\n}\n\n.styles_favourite-container__WTJm3 {\n display: flex;\n position: relative;\n z-index: 1;\n width: 100%;\n height: 50px;\n flex-shrink: 1;\n flex-direction: row;\n justify-content: space-between;\n align-items: center;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 {\n width: auto;\n min-width: 0;\n flex: 1;\n flex-grow: 1;\n height: 100%;\n position: relative;\n padding: 0;\n margin-right: 8px;\n overflow: hidden;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD {\n display: flex;\n width: 50px;\n height: 100%;\n justify-content: center;\n align-items: center;\n flex-shrink: 1;\n}\n@media screen and (min-width: 375px) {\n .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD {\n flex-shrink: 0;\n }\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD svg {\n width: 20px;\n height: 20px;\n position: relative;\n left: 2px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_place__4mHYj svg {\n width: 24px;\n height: 24px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_home__msJXd svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_school__Hta8J svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_sport__YFDd0 svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_shopping__jMKed svg {\n width: 18px;\n height: 18px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_work__uZX4R svg {\n width: 20px;\n height: 20px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV {\n white-space: nowrap;\n overflow: hidden;\n text-align: start;\n width: 100%;\n flex-direction: row;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_name__Fvsvr {\n font-family: inherit;\n font-size: 15px;\n margin-right: 6px;\n font-weight: var(--font-weight-medium);\n font-stretch: normal;\n font-style: normal;\n letter-spacing: -0.6px;\n color: #333;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_name__Fvsvr:empty {\n margin-top: 0;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_address__iBEhu {\n font-family: inherit;\n font-size: 12px;\n margin-right: 6px;\n font-weight: normal;\n font-stretch: normal;\n font-style: normal;\n line-height: 1.67;\n letter-spacing: -0.48px;\n color: #666;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf {\n height: 100%;\n width: 42px;\n flex-shrink: 0;\n overflow: hidden;\n padding: 0;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div {\n justify-content: center;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_plus__ryt4n svg {\n width: 16px;\n height: 16px;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_arrow__EbmBZ svg {\n width: 19px;\n height: 19px;\n transform: rotate(90deg);\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_arrow__EbmBZ.styles_rotate__Nw2-A svg {\n transform: rotate(-90deg);\n}\n\n.styles_favourite-suggestion-container__MVZBj {\n position: relative;\n display: flex;\n justify-content: center;\n padding-top: 9px;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_divider__podkI {\n display: block;\n height: 6px;\n background-color: #f2f5f7;\n border-top: 1px solid rgba(0, 0, 0, 0.1);\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-list__boIJY {\n max-height: 30vh;\n list-style-type: none;\n padding: 0;\n position: absolute;\n z-index: 20;\n width: 100%;\n margin: 0 auto;\n border-radius: 5px;\n box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.16);\n border: solid 1px rgba(0, 0, 0, 0.1);\n background-color: #fff;\n overflow-x: hidden;\n overflow-y: scroll;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-item__ptExh.styles_custom__87c-n {\n font-size: 15px;\n font-weight: var(--font-weight-medium);\n letter-spacing: -0.5;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-item__ptExh:hover {\n background-color: #f2f5f7;\n}";
159
+ var css_248z = ".styles_button-shared__RSwxE, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 {\n background-color: white;\n display: flex;\n flex-direction: row;\n justify-content: center;\n align-items: center;\n border-radius: 5px;\n border: 1px solid #ddd;\n cursor: pointer;\n}\n.styles_button-shared__RSwxE:hover, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf:hover, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1:hover {\n box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);\n}\n\n.styles_shimmer-shared__MGBAa, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 > div {\n display: flex;\n align-items: center;\n overflow: hidden;\n}\n.styles_shimmer-shared__MGBAa .styles_shimmer__bIP9J, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div .styles_shimmer__bIP9J, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 > div .styles_shimmer__bIP9J {\n border-radius: 5px;\n}\n\n.styles_favourite-container__WTJm3 {\n display: flex;\n position: relative;\n z-index: 1;\n width: 100%;\n height: 50px;\n flex-shrink: 1;\n flex-direction: row;\n justify-content: space-between;\n align-items: center;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 {\n width: auto;\n min-width: 0;\n flex: 1;\n flex-grow: 1;\n height: 100%;\n position: relative;\n padding: 0;\n margin-right: 8px;\n overflow: hidden;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD {\n display: flex;\n width: 50px;\n height: 100%;\n justify-content: center;\n align-items: center;\n flex-shrink: 1;\n}\n@media screen and (min-width: 375px) {\n .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD {\n flex-shrink: 0;\n }\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD svg {\n width: 20px;\n height: 20px;\n position: relative;\n left: 2px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_place__4mHYj svg {\n width: 24px;\n height: 24px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_home__msJXd svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_school__Hta8J svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_sport__YFDd0 svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_shopping__jMKed svg {\n width: 18px;\n height: 18px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_work__uZX4R svg {\n width: 20px;\n height: 20px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV {\n white-space: nowrap;\n overflow: hidden;\n text-align: start;\n width: 100%;\n flex-direction: row;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_name__Fvsvr {\n font-family: inherit;\n font-size: 15px;\n margin-right: 6px;\n font-weight: var(--font-weight-medium, 500);\n font-stretch: normal;\n font-style: normal;\n letter-spacing: -0.6px;\n color: #333;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_name__Fvsvr:empty {\n margin-top: 0;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_address__iBEhu {\n font-family: inherit;\n font-size: 12px;\n margin-right: 6px;\n font-weight: normal;\n font-stretch: normal;\n font-style: normal;\n line-height: 1.67;\n letter-spacing: -0.48px;\n color: #666;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf {\n height: 100%;\n width: 42px;\n flex-shrink: 0;\n overflow: hidden;\n padding: 0;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div {\n justify-content: center;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_plus__ryt4n svg {\n width: 16px;\n height: 16px;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_arrow__EbmBZ svg {\n width: 19px;\n height: 19px;\n transform: rotate(90deg);\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_arrow__EbmBZ.styles_rotate__Nw2-A svg {\n transform: rotate(-90deg);\n}\n\n.styles_favourite-suggestion-container__MVZBj {\n position: relative;\n display: flex;\n justify-content: center;\n padding-top: 9px;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_divider__podkI {\n display: block;\n height: 6px;\n background-color: #f2f5f7;\n border-top: 1px solid rgba(0, 0, 0, 0.1);\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-list__boIJY {\n max-height: 30vh;\n list-style-type: none;\n padding: 0;\n position: absolute;\n z-index: 20;\n width: 100%;\n margin: 0 auto;\n border-radius: 5px;\n box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.16);\n border: solid 1px rgba(0, 0, 0, 0.1);\n background-color: #fff;\n overflow-x: hidden;\n overflow-y: scroll;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-item__ptExh.styles_custom__87c-n {\n font-size: 15px;\n font-weight: var(--font-weight-medium, 500);\n letter-spacing: -0.5;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-item__ptExh:hover {\n background-color: #f2f5f7;\n}";
162
160
  var styles = {"button-shared":"styles_button-shared__RSwxE","favourite-container":"styles_favourite-container__WTJm3","expandButton":"styles_expandButton__mkemf","favourite-content":"styles_favourite-content__6gqt1","shimmer-shared":"styles_shimmer-shared__MGBAa","shimmer":"styles_shimmer__bIP9J","icon":"styles_icon__mAwUD","place":"styles_place__4mHYj","home":"styles_home__msJXd","school":"styles_school__Hta8J","sport":"styles_sport__YFDd0","shopping":"styles_shopping__jMKed","work":"styles_work__uZX4R","favourite-location":"styles_favourite-location__RiyEV","name":"styles_name__Fvsvr","address":"styles_address__iBEhu","plus":"styles_plus__ryt4n","arrow":"styles_arrow__EbmBZ","rotate":"styles_rotate__Nw2-A","favourite-suggestion-container":"styles_favourite-suggestion-container__MVZBj","divider":"styles_divider__podkI","favourite-suggestion-list":"styles_favourite-suggestion-list__boIJY","favourite-suggestion-item":"styles_favourite-suggestion-item__ptExh","custom":"styles_custom__87c-n"};
163
161
  styleInject(css_248z);
164
162
 
@@ -247,6 +245,20 @@ i18n.init();
247
245
 
248
246
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
249
247
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
248
+ const TOGGLE_DEBOUNCE_MS = 200;
249
+ const LIST_BOTTOM_MARGIN = 16;
250
+ const MIN_LIST_HEIGHT = 120;
251
+ const FAVOURITE_ICON_ID_TO_NAME = {
252
+ 'icon-icon_place': 'place',
253
+ 'icon-icon_home': 'home',
254
+ 'icon-icon_work': 'work',
255
+ 'icon-icon_sport': 'sport',
256
+ 'icon-icon_school': 'school',
257
+ 'icon-icon_shopping': 'shopping',
258
+ // Map two buggy ids temporarily. This code can be removed at some point.
259
+ icon_home: 'home',
260
+ icon_work: 'work'
261
+ };
250
262
  const isKeyboardSelectionEvent = event => {
251
263
  const space = [13, ' ', 'Spacebar'];
252
264
  const enter = [32, 'Enter'];
@@ -273,7 +285,9 @@ const FavouriteLocation = ({
273
285
  color,
274
286
  lang
275
287
  }) => {
276
- const [t] = useTranslation();
288
+ const [t] = useTranslation('translation', {
289
+ i18n
290
+ });
277
291
  const ariaLabel = label === '' ? text : `${text} ${label} ${t('add-destination', {
278
292
  lng: lang
279
293
  })}`;
@@ -309,8 +323,6 @@ process.env.NODE_ENV !== "production" ? FavouriteLocation.propTypes = {
309
323
  color: PropTypes.string.isRequired,
310
324
  lang: PropTypes.string.isRequired
311
325
  } : void 0;
312
- const LIST_BOTTOM_MARGIN = 16;
313
- const MIN_LIST_HEIGHT = 120;
314
326
 
315
327
  /**
316
328
  * FavouriteBar renders favourites. FavouriteBar displays the first two favourites, the rest are shown in a list.
@@ -318,263 +330,209 @@ const MIN_LIST_HEIGHT = 120;
318
330
  * <FavouriteBar
319
331
  * favourites={favourites}
320
332
  * onClickFavourite={onClickFavourite}
321
- * onAddPlace={this.addPlace}
322
- * onAddHome={this.addHome}
323
- * onAddWork={this.addWork}
324
- * lang={this.props.lang}
325
- * isLoading={this.props.isLoading}
333
+ * onAddPlace={onAddPlace}
334
+ * onEdit={onEdit}
335
+ * onAddHome={onAddHome}
336
+ * onAddWork={onAddWork}
337
+ * lang={lang}
338
+ * isLoading={isLoading}
326
339
  * />
327
340
  */
328
- class FavouriteBar extends React.Component {
329
- constructor(props) {
330
- super(props);
331
- // Let the list grow down to the bottom edge of the viewport
332
- _defineProperty(this, "updateListMaxHeight", () => {
333
- const list = this.suggestionListRef.current;
341
+ function FavouriteBar({
342
+ favourites,
343
+ onClickFavourite,
344
+ onAddPlace,
345
+ onEdit,
346
+ onAddHome,
347
+ onAddWork,
348
+ lang = 'fi',
349
+ isLoading = false,
350
+ colors = defaultColors
351
+ }) {
352
+ const [t] = useTranslation('translation', {
353
+ i18n
354
+ });
355
+ const [listOpen, setListOpen] = useState(false);
356
+ const [listMaxHeight, setListMaxHeight] = useState(null);
357
+ const expandListRef = useRef(null);
358
+ const suggestionListRef = useRef(null);
359
+ const firstItemRef = useRef(null);
360
+ const lastToggleRef = useRef(0);
361
+ const focusAfterToggleRef = useRef(false);
362
+ const firstFavourite = favourites[0] || null;
363
+ const secondFavourite = favourites[1] || null;
364
+ const listFavourites = favourites.slice(2);
365
+ const translate = id => t(id, {
366
+ lng: lang
367
+ });
368
+ useEffect(() => {
369
+ const handleClickOutside = event => {
370
+ if (suggestionListRef.current && expandListRef.current && !suggestionListRef.current.contains(event.target) && !expandListRef.current.contains(event.target)) {
371
+ setListOpen(false);
372
+ }
373
+ };
374
+ document.addEventListener('mousedown', handleClickOutside);
375
+ return () => document.removeEventListener('mousedown', handleClickOutside);
376
+ }, []);
377
+
378
+ // Let the list grow down to the bottom edge of the viewport
379
+ useEffect(() => {
380
+ if (!listOpen) {
381
+ return undefined;
382
+ }
383
+ const updateListMaxHeight = () => {
384
+ const list = suggestionListRef.current;
334
385
  if (list) {
335
386
  const {
336
387
  top
337
388
  } = list.getBoundingClientRect();
338
- const listMaxHeight = Math.max(window.innerHeight - top - LIST_BOTTOM_MARGIN, MIN_LIST_HEIGHT);
339
- this.setState({
340
- listMaxHeight
341
- });
342
- }
343
- });
344
- _defineProperty(this, "toggleList", () => {
345
- const eventDiff = new Date().getTime() - this.state.timestamp;
346
- if (eventDiff > 200) {
347
- this.setState(prevState => ({
348
- listOpen: !prevState.listOpen,
349
- timestamp: new Date().getTime()
350
- }), () => {
351
- if (this.state.listOpen) {
352
- this.updateListMaxHeight();
353
- this.firstItemRef.current?.focus();
354
- } else {
355
- this.expandListRef.current?.focus();
356
- }
357
- });
358
- }
359
- });
360
- _defineProperty(this, "translate", id => {
361
- return this.props.t(id, {
362
- lng: this.props.lang
363
- });
364
- });
365
- _defineProperty(this, "handleClickOutside", event => {
366
- if (this.suggestionListRef.current && this.expandListRef.current && !this.suggestionListRef.current.contains(event.target) && !this.expandListRef.current.contains(event.target)) {
367
- this.setState({
368
- listOpen: false
369
- });
389
+ setListMaxHeight(Math.max(window.innerHeight - top - LIST_BOTTOM_MARGIN, MIN_LIST_HEIGHT));
370
390
  }
371
- });
372
- _defineProperty(this, "suggestionSelected", index => {
373
- const {
374
- favourites
375
- } = this.state;
376
- if (index < favourites.length) {
377
- this.props.onClickFavourite(favourites[index]);
378
- } else if (index === favourites.length) {
379
- this.props.onAddPlace();
380
- } else if (index === favourites.length + 1) {
381
- this.props.onEdit();
391
+ };
392
+ updateListMaxHeight();
393
+ window.addEventListener('resize', updateListMaxHeight);
394
+ return () => window.removeEventListener('resize', updateListMaxHeight);
395
+ }, [listOpen]);
396
+
397
+ // Move focus only after a toggle, not when the list is closed by other means
398
+ useEffect(() => {
399
+ if (focusAfterToggleRef.current) {
400
+ focusAfterToggleRef.current = false;
401
+ if (listOpen) {
402
+ firstItemRef.current?.focus();
403
+ } else {
404
+ expandListRef.current?.focus();
382
405
  }
383
- this.toggleList();
384
- });
385
- _defineProperty(this, "handleKeyDown", (event, index) => {
386
- const {
387
- listOpen,
388
- favourites
389
- } = this.state;
390
- const key = event && (event.key || event.which || event.keyCode) || '';
391
- if (isKeyboardSelectionEvent(event)) {
392
- if (!listOpen) {
393
- this.toggleList();
394
- } else {
395
- this.suggestionSelected(index);
396
- }
397
- } else if (key === 'Escape' || key === 27) {
398
- if (listOpen) {
399
- this.toggleList();
400
- }
401
- } else if (key === 'Tab' && !event.shiftKey && index === favourites.length + this.getCustomSuggestions().length - 1) {
402
- this.setState({
403
- listOpen: false
404
- });
406
+ }
407
+ }, [listOpen]);
408
+ const toggleList = () => {
409
+ const now = Date.now();
410
+ if (now - lastToggleRef.current > TOGGLE_DEBOUNCE_MS) {
411
+ lastToggleRef.current = now;
412
+ focusAfterToggleRef.current = true;
413
+ setListOpen(open => !open);
414
+ }
415
+ };
416
+ const getCustomSuggestions = () => {
417
+ const customSuggestions = [{
418
+ name: translate('add-place'),
419
+ selectedIconId: 'favourite'
420
+ }];
421
+ if (favourites.length === 0) {
422
+ return customSuggestions;
423
+ }
424
+ return [...customSuggestions, {
425
+ name: translate('edit'),
426
+ selectedIconId: 'edit'
427
+ }];
428
+ };
429
+ const suggestionSelected = index => {
430
+ if (index < listFavourites.length) {
431
+ onClickFavourite(listFavourites[index]);
432
+ } else if (index === listFavourites.length) {
433
+ onAddPlace();
434
+ } else if (index === listFavourites.length + 1) {
435
+ onEdit();
436
+ }
437
+ toggleList();
438
+ };
439
+ const handleKeyDown = (event, index) => {
440
+ const key = event && (event.key || event.which || event.keyCode) || '';
441
+ if (isKeyboardSelectionEvent(event)) {
442
+ if (!listOpen) {
443
+ toggleList();
444
+ } else {
445
+ suggestionSelected(index);
405
446
  }
406
- });
407
- _defineProperty(this, "renderSuggestion", (item, index, ariaLabelSuffix = '') => {
408
- const id = `favourite-suggestion-list--item-${index}`;
409
- return /*#__PURE__*/React.createElement("li", {
410
- key: `favourite-suggestion-item-${index}`
411
- }, /*#__PURE__*/React.createElement("div", {
412
- role: "button",
413
- type: "button",
414
- tabIndex: "0",
415
- id: id,
416
- className: cx(styles['favourite-suggestion-item']),
417
- onClick: () => this.suggestionSelected(index),
418
- onKeyDown: e => this.handleKeyDown(e, index),
419
- ref: index === 0 ? this.firstItemRef : ''
420
- // eslint-disable-next-line jsx-a11y/no-autofocus
421
- ,
422
- autoFocus: index === 0,
423
- "aria-label": `${item?.name || ''} ${item?.address || ''} ${ariaLabelSuffix}`
424
- }, /*#__PURE__*/React.createElement(SuggestionItem, {
425
- item: item,
426
- colors: this.props.colors,
427
- fontWeights: this.props.fontWeights
428
- })));
429
- });
430
- _defineProperty(this, "getCustomSuggestions", () => {
431
- const customSuggestions = [{
432
- name: this.translate('add-place'),
433
- selectedIconId: 'favourite'
434
- }];
435
- if (this.props.favourites.length === 0) {
436
- return customSuggestions;
447
+ } else if (key === 'Escape' || key === 27) {
448
+ if (listOpen) {
449
+ toggleList();
437
450
  }
438
- return [...customSuggestions, {
439
- name: this.translate('edit'),
440
- selectedIconId: 'edit'
441
- }];
442
- });
443
- this.state = {
444
- listOpen: false,
445
- firstFavourite: props.favourites[0] || null,
446
- secondFavourite: props.favourites[1] || null,
447
- favourites: props.favourites.slice(2, props.favourites.length),
448
- timestamp: 0,
449
- listMaxHeight: null
450
- };
451
- this.expandListRef = /*#__PURE__*/React.createRef();
452
- this.suggestionListRef = /*#__PURE__*/React.createRef();
453
- this.firstItemRef = /*#__PURE__*/React.createRef();
454
- }
455
- componentDidMount() {
456
- document.addEventListener('mousedown', this.handleClickOutside);
457
- window.addEventListener('resize', this.updateListMaxHeight);
458
- }
459
- static getDerivedStateFromProps(nextProps, prevState) {
460
- const {
461
- favourites
462
- } = prevState;
463
- const nextFavourites = nextProps.favourites;
464
- if (!isEmpty(differenceWith(nextFavourites, favourites, isEqual)) || !isEmpty(differenceWith(favourites, nextFavourites, isEqual)) || !isEqual(nextFavourites, favourites) || isEmpty(nextFavourites)) {
465
- return {
466
- firstFavourite: nextFavourites[0] || null,
467
- secondFavourite: nextFavourites[1] || null,
468
- favourites: nextFavourites.slice(2, nextFavourites.length)
469
- };
451
+ } else if (key === 'Tab' && !event.shiftKey && index === listFavourites.length + getCustomSuggestions().length - 1) {
452
+ setListOpen(false);
470
453
  }
471
- return null;
472
- }
473
- componentWillUnmount() {
474
- document.removeEventListener('mousedown', this.handleClickOutside);
475
- window.removeEventListener('resize', this.updateListMaxHeight);
476
- }
477
- render() {
478
- const {
479
- onClickFavourite,
480
- isLoading,
481
- fontWeights
482
- } = this.props;
483
- const {
484
- primary
485
- } = this.props.colors;
486
- const {
487
- listOpen,
488
- favourites,
489
- firstFavourite,
490
- secondFavourite,
491
- listMaxHeight
492
- } = this.state;
493
- const expandIcon = this.props.favourites.length === 0 ? 'plus' : 'arrow';
494
- const [name1, address1] = formatFavourite(firstFavourite);
495
- const [name2, address2] = formatFavourite(secondFavourite);
496
- return /*#__PURE__*/React.createElement("div", {
497
- style: {
498
- '--font-weight-medium': fontWeights.medium
499
- }
454
+ };
455
+ const renderSuggestion = (item, index, ariaLabelSuffix = '') => {
456
+ const id = `favourite-suggestion-list--item-${index}`;
457
+ return /*#__PURE__*/React.createElement("li", {
458
+ key: `favourite-suggestion-item-${index}`
500
459
  }, /*#__PURE__*/React.createElement("div", {
501
- className: styles['favourite-container']
502
- }, /*#__PURE__*/React.createElement(FavouriteLocation, {
503
- text: name1 || this.translate('add-home'),
504
- label: address1 || '',
505
- clickItem: () => firstFavourite ? onClickFavourite(firstFavourite) : this.props.onAddHome(),
506
- iconId: firstFavourite && firstFavourite.selectedIconId ? FavouriteBar.FavouriteIconIdToNameMap[firstFavourite.selectedIconId] : 'home',
507
- isLoading: isLoading,
508
- color: primary,
509
- lang: this.props.lang
510
- }), /*#__PURE__*/React.createElement(FavouriteLocation, {
511
- text: name2 || this.translate('add-work'),
512
- label: address2 || '',
513
- clickItem: () => secondFavourite ? onClickFavourite(secondFavourite) : this.props.onAddWork(),
514
- iconId: secondFavourite && secondFavourite.selectedIconId ? FavouriteBar.FavouriteIconIdToNameMap[secondFavourite.selectedIconId] : 'work',
515
- isLoading: isLoading,
516
- color: primary,
517
- lang: this.props.lang
518
- }), /*#__PURE__*/React.createElement("button", {
460
+ role: "button",
519
461
  type: "button",
520
- className: cx(styles.expandButton, styles[expandIcon], listOpen && styles.rotate),
521
- ref: this.expandListRef,
522
- id: "favourite-expand-button",
523
- onKeyDown: e => this.handleKeyDown(e),
524
- onClick: () => this.toggleList(),
525
- "aria-expanded": this.state.listOpen,
526
- "aria-label": this.translate('open-favourites')
527
- }, /*#__PURE__*/React.createElement(Shimmer, {
528
- active: isLoading
529
- }, /*#__PURE__*/React.createElement(Icon, {
530
- img: expandIcon,
531
- color: primary
532
- })))), /*#__PURE__*/React.createElement("div", {
533
- className: styles['favourite-suggestion-container']
534
- }, listOpen && /*#__PURE__*/React.createElement("ul", {
535
- className: styles['favourite-suggestion-list'],
536
- id: "favourite-suggestion-list",
537
- style: listMaxHeight ? {
538
- maxHeight: listMaxHeight
539
- } : undefined,
540
- ref: this.suggestionListRef,
541
- "aria-label": this.translate('favourites-list')
542
- }, favourites.map((item, index) => {
543
- const favouriteLabel = formatFavouritePlaceLabel(item.name, item.address);
544
- const address = favouriteLabel[1];
545
- return this.renderSuggestion(_objectSpread(_objectSpread({}, item), {}, {
546
- address
547
- }), index, `, ${this.translate('add-destination')}`);
548
- }), favourites.length > 0 && /*#__PURE__*/React.createElement("div", {
549
- "aria-hidden": true,
550
- className: styles.divider
551
- }), this.getCustomSuggestions().map((item, index) => this.renderSuggestion(item, favourites.length + index, undefined)))));
552
- }
462
+ tabIndex: "0",
463
+ id: id,
464
+ className: cx(styles['favourite-suggestion-item']),
465
+ onClick: () => suggestionSelected(index),
466
+ onKeyDown: e => handleKeyDown(e, index),
467
+ ref: index === 0 ? firstItemRef : undefined
468
+ // eslint-disable-next-line jsx-a11y/no-autofocus
469
+ ,
470
+ autoFocus: index === 0,
471
+ "aria-label": `${item?.name || ''} ${item?.address || ''} ${ariaLabelSuffix}`
472
+ }, /*#__PURE__*/React.createElement(SuggestionItem, {
473
+ item: item,
474
+ colors: colors
475
+ })));
476
+ };
477
+ const {
478
+ primary
479
+ } = colors;
480
+ const expandIcon = favourites.length === 0 ? 'plus' : 'arrow';
481
+ const [name1, address1] = formatFavourite(firstFavourite);
482
+ const [name2, address2] = formatFavourite(secondFavourite);
483
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
484
+ className: styles['favourite-container']
485
+ }, /*#__PURE__*/React.createElement(FavouriteLocation, {
486
+ text: name1 || translate('add-home'),
487
+ label: address1 || '',
488
+ clickItem: () => firstFavourite ? onClickFavourite(firstFavourite) : onAddHome(),
489
+ iconId: firstFavourite && firstFavourite.selectedIconId ? FAVOURITE_ICON_ID_TO_NAME[firstFavourite.selectedIconId] : 'home',
490
+ isLoading: isLoading,
491
+ color: primary,
492
+ lang: lang
493
+ }), /*#__PURE__*/React.createElement(FavouriteLocation, {
494
+ text: name2 || translate('add-work'),
495
+ label: address2 || '',
496
+ clickItem: () => secondFavourite ? onClickFavourite(secondFavourite) : onAddWork(),
497
+ iconId: secondFavourite && secondFavourite.selectedIconId ? FAVOURITE_ICON_ID_TO_NAME[secondFavourite.selectedIconId] : 'work',
498
+ isLoading: isLoading,
499
+ color: primary,
500
+ lang: lang
501
+ }), /*#__PURE__*/React.createElement("button", {
502
+ type: "button",
503
+ className: cx(styles.expandButton, styles[expandIcon], listOpen && styles.rotate),
504
+ ref: expandListRef,
505
+ id: "favourite-expand-button",
506
+ onKeyDown: e => handleKeyDown(e),
507
+ onClick: toggleList,
508
+ "aria-expanded": listOpen,
509
+ "aria-label": translate('open-favourites')
510
+ }, /*#__PURE__*/React.createElement(Shimmer, {
511
+ active: isLoading
512
+ }, /*#__PURE__*/React.createElement(Icon, {
513
+ img: expandIcon,
514
+ color: primary
515
+ })))), /*#__PURE__*/React.createElement("div", {
516
+ className: styles['favourite-suggestion-container']
517
+ }, listOpen && /*#__PURE__*/React.createElement("ul", {
518
+ className: styles['favourite-suggestion-list'],
519
+ id: "favourite-suggestion-list",
520
+ style: listMaxHeight ? {
521
+ maxHeight: listMaxHeight
522
+ } : undefined,
523
+ ref: suggestionListRef,
524
+ "aria-label": translate('favourites-list')
525
+ }, listFavourites.map((item, index) => {
526
+ const favouriteLabel = formatFavouritePlaceLabel(item.name, item.address);
527
+ const address = favouriteLabel[1];
528
+ return renderSuggestion(_objectSpread(_objectSpread({}, item), {}, {
529
+ address
530
+ }), index, `, ${translate('add-destination')}`);
531
+ }), listFavourites.length > 0 && /*#__PURE__*/React.createElement("div", {
532
+ "aria-hidden": true,
533
+ className: styles.divider
534
+ }), getCustomSuggestions().map((item, index) => renderSuggestion(item, listFavourites.length + index, undefined)))));
553
535
  }
554
- _defineProperty(FavouriteBar, "defaultProps", {
555
- onClickFavourite: () => ({}),
556
- onAddPlace: () => ({}),
557
- onEdit: () => ({}),
558
- onAddHome: () => ({}),
559
- onAddWork: () => ({}),
560
- lang: 'fi',
561
- isLoading: false,
562
- colors: defaultColors,
563
- fontWeights: {
564
- medium: 500
565
- }
566
- });
567
- _defineProperty(FavouriteBar, "FavouriteIconIdToNameMap", {
568
- 'icon-icon_place': 'place',
569
- 'icon-icon_home': 'home',
570
- 'icon-icon_work': 'work',
571
- 'icon-icon_sport': 'sport',
572
- 'icon-icon_school': 'school',
573
- 'icon-icon_shopping': 'shopping',
574
- // Map two buggy ids temporarily. This code can be removed at some point.
575
- icon_home: 'home',
576
- icon_work: 'work'
577
- });
578
536
  process.env.NODE_ENV !== "production" ? FavouriteBar.propTypes = {
579
537
  /** Required. Array of favourites, favourite object contains following properties.
580
538
  * @type {Array<object>}
@@ -597,34 +555,23 @@ process.env.NODE_ENV !== "production" ? FavouriteBar.propTypes = {
597
555
  selectedIconId: PropTypes.string,
598
556
  favouriteId: PropTypes.string
599
557
  })).isRequired,
600
- /** Optional. Function for clicking favourites. */
601
- onClickFavourite: PropTypes.func,
602
- /** Optional. Function for selecting "Add place" from suggestions. */
603
- onAddPlace: PropTypes.func,
604
- /** Optional. Function for selected "Edit" from suggestions. */
605
- onEdit: PropTypes.func,
606
- /** Optional. Function for "Add home" button. */
607
- onAddHome: PropTypes.func,
608
- /** Optional. Function for "Add work" button. */
609
- onAddWork: PropTypes.func,
558
+ /** Required. Function for clicking favourites. */
559
+ onClickFavourite: PropTypes.func.isRequired,
560
+ /** Required. Function for selecting "Add place" from suggestions. */
561
+ onAddPlace: PropTypes.func.isRequired,
562
+ /** Required. Function for selected "Edit" from suggestions. */
563
+ onEdit: PropTypes.func.isRequired,
564
+ /** Required. Function for "Add home" button. */
565
+ onAddHome: PropTypes.func.isRequired,
566
+ /** Required. Function for "Add work" button. */
567
+ onAddWork: PropTypes.func.isRequired,
610
568
  /** Optional. Language, fi, en or sv. */
611
569
  lang: PropTypes.string,
612
570
  /** Optional. Whether to show loading animation, true or false. */
613
571
  isLoading: PropTypes.bool,
614
572
  /** Optional color palette */
615
- colors: PropTypes.objectOf(PropTypes.string),
616
- /** Optional. */
617
- fontWeights: PropTypes.shape({
618
- /** Default value is 500. */
619
- medium: PropTypes.number
620
- }),
621
- /** Translation function */
622
- t: PropTypes.func.isRequired
573
+ colors: PropTypes.objectOf(PropTypes.string)
623
574
  } : void 0;
624
- const FavouriteBarWithTranslation = withTranslation()(FavouriteBar);
625
- var index = props => /*#__PURE__*/React.createElement(I18nextProvider, {
626
- i18n: i18n
627
- }, /*#__PURE__*/React.createElement(FavouriteBarWithTranslation, props));
628
575
 
629
- export { index as default };
576
+ export { FavouriteBar as default };
630
577
  //# sourceMappingURL=index.js.map