Update ODLUX
[ccsdk/features.git] / sdnr / wt / 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
31 const styles = (theme: Theme) => createStyles({
32   active: {
33     backgroundColor: theme.palette.action.selected
34   }
35 });
36
37 export interface IListItemLinkProps extends WithStyles<typeof styles> {
38   icon: JSX.Element | null;
39   primary: string | React.ComponentType;
40   secondary?: React.ComponentType;
41   to: string;
42   exact?: boolean;
43   external?: boolean;
44 }
45
46 export const ListItemLink = withStyles(styles)((props: IListItemLinkProps) => {
47   const { icon, primary: Primary, secondary: Secondary, classes, to, exact = false, external=false } = props;
48   const renderLink = (itemProps: any): JSX.Element => (
49     props.external ? <a target="_blank" href={to} { ...itemProps }></a> :
50   <NavLink exact={ exact } to={ to } activeClassName={ classes.active } { ...itemProps } />);
51
52   const ariaLabel = typeof Primary === 'string' ? toAriaLabel("link-to-"+Primary) : toAriaLabel("link-to-"+Primary.displayName);
53   return (
54        <>
55         <ListItem button component={ renderLink } aria-label={ariaLabel}>
56           { icon
57             ? <ListItemIcon>{ icon }</ListItemIcon>
58             : null
59           }
60         { typeof Primary === 'string'
61           ? <ListItemText primary={ Primary } style={{ padding: 0 }} /> 
62           : <Primary />
63           }
64         </ListItem>
65         { Secondary 
66           ? <Route exact={ exact } path={ to } component={ Secondary } />
67           : null
68         }
69       </>
70     );
71   }
72 );
73
74 export default ListItemLink;
75