Skip to content

Commit 3738410

Browse files
feat(Navigation): provide dropdown props to custom components (#721)
* feat(Navigation): provide dropdown props to custom components * feat(Navigation): add layout data to custom component * fix: clearer code & storybook * fix: cleanup * fix: better naming in stories * fix: simplify custom navItem story * fix: clearer storybook
1 parent 4487d50 commit 3738410

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.custom-nav-item {
2+
white-space: nowrap;
3+
4+
&:hover {
5+
color: var(--g-color-text-brand);
6+
}
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
3+
import {cn} from '../../../utils';
4+
import {NavigationItemProps} from '../../models';
5+
6+
import './CustomComponent.scss';
7+
8+
const b = cn('custom-nav-item');
9+
10+
type DCDropdownNavigationItemProps = Pick<
11+
NavigationItemProps,
12+
'onClick' | 'isActive' | 'menuLayout'
13+
>;
14+
15+
export const CustomComponent: React.FC<DCDropdownNavigationItemProps> = (props) => {
16+
const {onClick, isActive, menuLayout} = props;
17+
return (
18+
<div className={b({active: isActive})} onClick={onClick}>
19+
{`Custom Item (${menuLayout}${isActive ? ' - active' : ''})`}
20+
</div>
21+
);
22+
};

src/navigation/__stories__/Navigation.stories.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {Meta, StoryFn} from '@storybook/react';
55
import {PageConstructor} from '../../containers/PageConstructor';
66
import {CustomConfig, NavigationData} from '../../models';
77

8+
import {CustomComponent} from './CustomComponent/CustomComponent';
9+
810
import data from './data.json';
911

1012
export default {
@@ -37,14 +39,19 @@ NavigationWithBorder.args = {
3739
NavigationWithCustomItems.args = {
3840
custom: {
3941
navigation: {
40-
search: () => <div>Search</div>,
42+
'custom-item': CustomComponent,
4143
},
4244
},
4345
navigation: {
4446
...data.navigation,
4547
header: {
4648
...data.navigation.header,
47-
rightItems: [...data.navigation.header.rightItems, {type: 'search'}],
49+
rightItems: [
50+
...data.navigation.header.rightItems,
51+
{
52+
type: 'custom-item',
53+
},
54+
],
4855
},
4956
} as NavigationData,
5057
};

src/navigation/components/NavigationItem/NavigationItem.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, {useMemo} from 'react';
33
import omit from 'lodash/omit';
44

55
import {BlockIdContext} from '../../../context/blockIdContext';
6-
import {CustomItem, NavigationItemType} from '../../../models';
6+
import {CustomItem, NavigationItemType, NavigationItemTypes} from '../../../models';
77
import {block} from '../../../utils';
88
import {NavigationItemProps} from '../../models';
99

@@ -15,6 +15,10 @@ const b = block('navigation-item');
1515

1616
const ANALYTICS_ID = 'navigation';
1717

18+
const nonComplexNavigationItemTypes = NavigationItemTypes.filter(
19+
(type) => type !== NavigationItemType.Dropdown,
20+
);
21+
1822
const NavigationItem: React.FC<NavigationItemProps> = ({
1923
data,
2024
className,
@@ -30,12 +34,14 @@ const NavigationItem: React.FC<NavigationItemProps> = ({
3034
...props,
3135
};
3236

33-
if (type !== NavigationItemType.Dropdown) {
37+
if (nonComplexNavigationItemTypes.includes(type)) {
3438
return omit(componentProperties, 'hidePopup', 'isActive');
3539
}
3640

37-
return componentProperties;
38-
}, [data, props, type]);
41+
return NavigationItemTypes.includes(type)
42+
? componentProperties
43+
: {...componentProperties, menuLayout};
44+
}, [data, props, type, menuLayout]);
3945

4046
return (
4147
<BlockIdContext.Provider value={ANALYTICS_ID}>

0 commit comments

Comments
 (0)