@@ -5,78 +5,88 @@ import Form from 'react-bootstrap/Form'
55import FormControl from 'react-bootstrap/FormControl'
66import NavDropdown from 'react-bootstrap/NavDropdown'
77import { Button } from '../Button'
8- import { NavLink , Brand , NavLinkElement , Search } from './interfaces'
8+ import { NavLink , NavBrand , NavLinkList , NavSearch } from './interfaces'
99
1010interface Props extends React . Props < any > {
1111 /** Determines the navbar background color */
1212 bg ?: string
1313 /** Determines the letters color. It should be combined with the background color (bg) */
1414 variant ?: 'light' | 'dark'
1515 /** Determines the links names, theirs onClick methods and paths. It has children array which contain links to be used on a dropdown. */
16- navLinks : NavLink [ ]
17- /** Determines the hospital/clinic name to be shown at the navbar */
18- brand : Brand
19- /** Defines the properties of the search element */
20- search : Search
16+ navItems : ( NavBrand | NavLink | NavLinkList | NavSearch ) [ ]
2117}
2218
2319/**
2420 * Used to redirect users to the main topics.
2521 */
2622const Navbar = ( props : Props ) => {
27- const { bg, variant, navLinks , brand , search } = props
23+ const { bg, variant, navItems } = props
2824
29- const getNavItems = ( subLink : NavLinkElement , index : number ) => (
30- < NavDropdown . Item href = { subLink . href ? subLink . href : '' } key = { index } onClick = { subLink . onClick } >
31- { subLink . label }
25+ const getNavListLinks = ( link : NavLink , index : number ) => (
26+ < NavDropdown . Item href = { link . href ? link . href : '' } key = { index } onClick = { link . onClick } >
27+ { link . label }
3228 </ NavDropdown . Item >
3329 )
34-
35- const getNavLinks = ( link : NavLink , index : number ) => {
36- if ( link . children . length === 0 ) {
37- return (
38- < Nav . Link onClick = { link . onClick } key = { index } >
39- { link . label }
40- </ Nav . Link >
41- )
42- }
43-
44- return (
45- < NavDropdown title = { link . label } id = "collasible-nav-dropdown" key = { index } >
46- { link . children . map ( ( subLink , i ) => getNavItems ( subLink , i ) ) }
47- </ NavDropdown >
48- )
49- }
30+ const getNavSearch = ( search : NavSearch ) => (
31+ < Nav >
32+ < Form inline >
33+ < FormControl
34+ type = "text"
35+ placeholder = { search . placeholderText || 'Search' }
36+ className = "mr-sm-2"
37+ onChange = { search . onChangeInput }
38+ />
39+ < Button color = { search . buttonColor || 'primary' } onClick = { search . onClickButton } >
40+ { search . buttonText || 'Search' }
41+ </ Button >
42+ </ Form >
43+ </ Nav >
44+ )
45+ const getNavList = ( list : NavLinkList , index : number ) => (
46+ < NavDropdown title = { list . label } id = "collasible-nav-dropdown" key = { index } >
47+ { list . children . map ( ( subLink , i ) => getNavListLinks ( subLink , i ) ) }
48+ </ NavDropdown >
49+ )
50+ const getNavBrand = ( brand : NavBrand ) => (
51+ < NavbarRB . Brand onClick = { brand . onClick } style = { { cursor : 'pointer' } } >
52+ { brand . src ? (
53+ < img
54+ alt = { brand . label }
55+ src = { brand . src }
56+ width = "28"
57+ height = "28"
58+ className = "d-inline-block align-top mr-3"
59+ />
60+ ) : (
61+ ''
62+ ) }
63+ < span style = { { color : brand . color } } > { `${ brand . label } ` } </ span >
64+ </ NavbarRB . Brand >
65+ )
66+ const getNavLink = ( link : NavLink , index : number ) => (
67+ < Nav . Link onClick = { link . onClick } key = { index } >
68+ { link . label }
69+ </ Nav . Link >
70+ )
5071 return (
5172 < NavbarRB bg = { bg } variant = { variant } >
52- < NavbarRB . Brand onClick = { brand . onClick } style = { { cursor : 'pointer' } } >
53- { brand . src ? (
54- < img
55- alt = { brand . label }
56- src = { brand . src }
57- width = "28"
58- height = "28"
59- className = "d-inline-block align-top mr-3"
60- />
61- ) : (
62- ''
63- ) }
64- < span style = { { color : brand . color } } > { `${ brand . label } ` } </ span >
65- </ NavbarRB . Brand >
6673 < NavbarRB . Collapse id = "responsive-navbar-nav" >
67- < Nav className = "mr-auto" > { navLinks . map ( ( link , index ) => getNavLinks ( link , index ) ) } </ Nav >
68- < Nav >
69- < Form inline >
70- < FormControl
71- type = "text"
72- placeholder = { search . placeholderText || 'Search' }
73- className = "mr-sm-2"
74- onChange = { search . onChangeInput }
75- />
76- < Button color = { search . buttonColor || 'primary' } onClick = { search . onClickButton } >
77- { search . buttonText || 'Search' }
78- </ Button >
79- </ Form >
74+ < Nav className = "mr-auto" >
75+ { navItems . map ( ( item , index ) => {
76+ if ( ( item as NavBrand ) . type === 'brand' ) {
77+ return getNavBrand ( item as NavBrand )
78+ }
79+ if ( ( item as NavLink ) . type === 'link' ) {
80+ return getNavLink ( item as NavLink , index )
81+ }
82+ if ( ( item as NavSearch ) . type === 'search' ) {
83+ return getNavSearch ( item as NavSearch )
84+ }
85+ if ( ( item as NavLinkList ) . type === 'link-list' ) {
86+ return getNavList ( item as NavLinkList , index )
87+ }
88+ return null
89+ } ) }
8090 </ Nav >
8191 </ NavbarRB . Collapse >
8292 </ NavbarRB >
0 commit comments