UNPKG

3.14 kBJavaScriptView Raw
1import * as React from 'react';
2import { ApplicationLauncherSeparator } from '../components/ApplicationLauncher/ApplicationLauncherSeparator';
3import { Divider } from '../components/Divider/Divider';
4/**
5 * This function is a helper for creating an array of renderable favorite items for the Application launcher or Select
6 *
7 * @param {object} items The items rendered in Select or Application aLauncher
8 * @param {boolean} isGrouped Flag indicating if items are grouped
9 * @param {any[]} favorites Array of ids of favorited items
10 * @param {boolean} isEnterTriggersArrowDown Flag indicating if we should add isEnterTriggersArrowDown to favorited item
11 */
12export const createRenderableFavorites = (items, isGrouped, favorites, isEnterTriggersArrowDown) => {
13 if (isGrouped) {
14 const favoriteItems = [];
15 items.forEach(group => {
16 if (favorites.length > 0) {
17 return (group.props.children &&
18 group.props.children
19 .filter(item => favorites.includes(item.props.id))
20 .map(item => {
21 if (isEnterTriggersArrowDown) {
22 return favoriteItems.push(React.cloneElement(item, {
23 isFavorite: true,
24 enterTriggersArrowDown: isEnterTriggersArrowDown,
25 id: `favorite-${item.props.id}`
26 }));
27 }
28 else {
29 return favoriteItems.push(React.cloneElement(item, { isFavorite: true, id: `favorite-${item.props.id}` }));
30 }
31 }));
32 }
33 });
34 return favoriteItems;
35 }
36 return items
37 .filter(item => favorites.includes(item.props.id))
38 .map(item => React.cloneElement(item, { isFavorite: true, enterTriggersArrowDown: isEnterTriggersArrowDown }));
39};
40/**
41 * This function is a helper for extending the array of renderable favorite with the select/application launcher items to render in the Application launcher or Select
42 *
43 * @param {object} items The items rendered in Select or Application aLauncher
44 * @param {boolean} isGrouped Flag indicating if items are grouped
45 * @param {any[]} favorites Array of ids of favorited items
46 */
47export const extendItemsWithFavorite = (items, isGrouped, favorites) => {
48 if (isGrouped) {
49 return items.map(group => React.cloneElement(group, {
50 children: React.Children.map(group.props.children, item => {
51 if (item.type === ApplicationLauncherSeparator || item.type === Divider) {
52 return item;
53 }
54 return React.cloneElement(item, {
55 isFavorite: favorites.some(favoriteId => favoriteId === item.props.id || `favorite-${favoriteId}` === item.props.id)
56 });
57 })
58 }));
59 }
60 return items.map(item => React.cloneElement(item, {
61 isFavorite: favorites.some(favoriteId => favoriteId === item.props.id)
62 }));
63};
64//# sourceMappingURL=favorites.js.map
\No newline at end of file