Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / framework / src / components / material-ui / listItemLink.tsx
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import * as React from 'react';
19 import { NavLink, Link, Route } from 'react-router-dom';
20
21 import ListItem from '@mui/material/ListItem';
22 import ListItemIcon from '@mui/material/ListItemIcon';
23 import ListItemText from '@mui/material/ListItemText';
24
25 import { Theme } from '@mui/material/styles';
26 import { WithStyles } from '@mui/styles';
27 import withStyles from '@mui/styles/withStyles';
28 import createStyles from '@mui/styles/createStyles';
29 import { toAriaLabel } from '../../utilities/yangHelper';
30 import { IconType } from '../../models/iconDefinition';
31 import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
32
33 const styles = (theme: Theme) => createStyles({
34   active: {
35     backgroundColor: theme.palette.action.selected
36   }
37 });
38
39 export interface IListItemLinkProps extends WithStyles<typeof styles> {
40   icon: IconType | null;
41   primary: string | React.ComponentType;
42   secondary?: React.ComponentType;
43   to: string;
44   exact?: boolean;
45   external?: boolean;
46 }
47
48 export const ListItemLink = withStyles(styles)((props: IListItemLinkProps) => {
49   const { icon, primary: Primary, secondary: Secondary, classes, to, exact = false, external=false } = props;
50   const renderLink = (itemProps: any): JSX.Element => (
51     props.external ? <a target="_blank" href={to} { ...itemProps }></a> :
52   <NavLink exact={ exact } to={ to } activeClassName={ classes.active } { ...itemProps } />);
53   
54   const customIconHeight = 22;
55   const ariaLabel = typeof Primary === 'string' ? toAriaLabel("link-to-"+Primary) : toAriaLabel("link-to-"+Primary.displayName);
56   
57   //create menu icon, either using an faIcon or a link to a custom svg icon
58   //moved to one place for easier usage
59   const listItemIcon = icon && ( typeof icon === 'string' ? <img height={customIconHeight} src={icon} /> : <FontAwesomeIcon icon={icon} /> );
60     
61   return (
62        <>
63         <ListItem button component={ renderLink } aria-label={ariaLabel}>
64           { icon
65             ? <ListItemIcon>{ listItemIcon }</ListItemIcon>
66             : null
67           }
68         { typeof Primary === 'string'
69           ? <ListItemText primary={ Primary } style={{ padding: 0 }} /> 
70           : <Primary />
71           }
72         </ListItem>
73         { Secondary 
74           ? <Route exact={ exact } path={ to } component={ Secondary } />
75           : null
76         }
77       </>
78     );
79   }
80 );
81
82 export default ListItemLink;
83