Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 4784520

Browse files
committed
feat(navbar): add example with icons and support className prop
fix #228
1 parent 91ce73e commit 4784520

3 files changed

Lines changed: 107 additions & 16 deletions

File tree

src/components/Navbar/Navbar.tsx

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ interface Props extends React.Props<any> {
2222
const Navbar = (props: Props) => {
2323
const { bg, variant, navItems } = props
2424

25-
const getNavListLinks = (link: NavLink, index: number) => (
26-
<NavDropdown.Item href={link.href ? link.href : ''} key={index} onClick={link.onClick}>
25+
const getNavListLink = (link: NavLink, index: number) => (
26+
<NavDropdown.Item
27+
className={link.className ? link.className.concat(' ', '') : ''}
28+
href={link.href ? link.href : ''}
29+
key={index}
30+
onClick={link.onClick}
31+
>
2732
{link.label}
2833
</NavDropdown.Item>
2934
)
3035
const getNavSearch = (search: NavSearch, index: number) => (
31-
<Nav key={index}>
36+
<Nav className={search.className ? search.className.concat(' ', '') : ''} key={index}>
3237
<Form inline>
3338
<FormControl
3439
type="text"
@@ -43,29 +48,46 @@ const Navbar = (props: Props) => {
4348
</Nav>
4449
)
4550
const getNavLinkList = (list: NavLinkList, index: number) => (
46-
<NavDropdown title={list.label} id="collasible-nav-dropdown" key={index}>
47-
{list.children.map((subLink, i) => getNavListLinks(subLink, i))}
51+
<NavDropdown
52+
className={list.className ? list.className.concat(' ', '') : ''}
53+
title={list.label}
54+
id="collasible-nav-dropdown"
55+
key={index}
56+
>
57+
{list.children.map((listLink, i) => getNavListLink(listLink, i))}
4858
</NavDropdown>
4959
)
5060
const getNavHeader = (header: NavHeader, index: number) => (
51-
<NavbarRB.Brand onClick={header.onClick} style={{ cursor: 'pointer' }} key={index}>
61+
<NavbarRB.Brand
62+
className={header.className ? header.className.concat(' ', '') : ''}
63+
onClick={header.onClick}
64+
style={{ cursor: 'pointer' }}
65+
key={index}
66+
>
5267
<span style={{ color: header.color }}>{`${header.label}`}</span>
5368
</NavbarRB.Brand>
5469
)
5570
const getNavIcon = (icon: NavIcon, index: number) => (
56-
<NavbarRB.Brand onClick={icon.onClick} style={{ cursor: 'pointer' }} key={index}>
57-
<img
58-
alt={icon.alt}
59-
src={icon.src}
60-
width="28"
61-
height="28"
62-
className="d-inline-block align-top mr-3"
63-
/>
71+
<NavbarRB.Brand
72+
className={
73+
icon.className
74+
? icon.className.concat(' ', 'd-inline-block align-top')
75+
: 'd-inline-block align-top'
76+
}
77+
onClick={icon.onClick}
78+
style={{ cursor: 'pointer' }}
79+
key={index}
80+
>
81+
<img alt={icon.alt} src={icon.src} width="28" height="28" />
6482
</NavbarRB.Brand>
6583
)
6684

6785
const getNavLink = (link: NavLink, index: number) => (
68-
<Nav.Link onClick={link.onClick} key={index}>
86+
<Nav.Link
87+
className={link.className ? link.className.concat(' ', '') : ''}
88+
onClick={link.onClick}
89+
key={index}
90+
>
6991
{link.label}
7092
</Nav.Link>
7193
)

src/components/Navbar/interfaces.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export interface NavItem {
22
type: string
3+
/**
4+
* Defines the class of the list.
5+
*/
6+
className?: string
37
}
48

59
export interface NavIcon extends NavItem {

stories/navbar.stories.tsx

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ storiesOf('Navbar', module)
6161
]}
6262
/>
6363
))
64-
.add('Dark navbar w/ linked list', () => (
64+
.add('Dark navbar w/ link list', () => (
6565
<Navbar
6666
bg="dark"
6767
variant="dark"
@@ -126,6 +126,71 @@ storiesOf('Navbar', module)
126126
]}
127127
/>
128128
))
129+
.add('Navbar w/ icons', () => (
130+
<Navbar
131+
navItems={[
132+
{
133+
type: 'icon',
134+
src:
135+
'https://raw.githubusercontent.com/HospitalRun/hospitalrun.github.io/master/favicon.png',
136+
onClick: () => {
137+
Toast('success', 'Icon clicked!!', 'Success')
138+
},
139+
},
140+
{
141+
type: 'header',
142+
label: 'HospitalRun',
143+
onClick: () => {
144+
Toast('success', 'Header clicked!!', 'Success')
145+
},
146+
},
147+
{
148+
type: 'link',
149+
label: 'Link 1',
150+
onClick: () => {
151+
Toast('success', 'Link 1 clicked!!', 'Success')
152+
},
153+
},
154+
{
155+
type: 'link',
156+
label: 'Link 2',
157+
onClick: () => {
158+
Toast('success', 'Link 2 clicked!!', 'Success')
159+
},
160+
className: 'mr-3',
161+
},
162+
{
163+
type: 'search',
164+
placeholderText: 'Custom',
165+
buttonText: 'Text',
166+
buttonColor: 'secondary',
167+
onClickButton: () => {
168+
Toast('success', 'Search button clicked!!', 'Success')
169+
},
170+
onChangeInput: () => {
171+
Toast('success', 'Search box changed!!', 'Success')
172+
},
173+
},
174+
{
175+
type: 'icon',
176+
src: 'https://image.flaticon.com/icons/svg/126/126472.svg',
177+
onClick: () => {
178+
Toast('success', 'Settings icon clicked!!', 'Success')
179+
},
180+
className: 'ml-4',
181+
},
182+
{
183+
type: 'icon',
184+
src:
185+
'https://s3.us-east-2.amazonaws.com/upload-icon/uploads/icons/png/7104608081548233620-128.png',
186+
onClick: () => {
187+
Toast('success', 'Profile icon clicked!!', 'Success')
188+
},
189+
className: 'ml-2',
190+
},
191+
]}
192+
/>
193+
))
129194
.add('Custom navbar', () => (
130195
<Navbar
131196
bg="light"

0 commit comments

Comments
 (0)