5586e2b4cbe6fcbbc124f2e74e51d816af6ae676
[aai/sparky-fe.git] / src / generic-components / toggleButtonGroup / ToggleButtonGroup.jsx
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 import React, {Component, PropTypes} from 'react';
24 import {connect} from 'react-redux';
25
26 import ButtonGroup from 'react-bootstrap/lib/ButtonGroup.js';
27 import Button from 'react-bootstrap/lib/Button.js';
28
29 import ToggleButtonGroupActions from 'generic-components/toggleButtonGroup/ToggleButtonGroupActions.js';
30
31 let mapActionToProps = (dispatch) => {
32                 return {
33                                 onButtonToggle: (buttonName) => {
34                                                 dispatch(ToggleButtonGroupActions.onToggle({button: buttonName}));
35                                 }
36                 };
37 };
38
39 let mapStateToProps = ({toggleButtonGroupData}) => {
40                 
41                 let {selectedButton} = toggleButtonGroupData;
42                 
43                 return {
44                                 selectedButton
45                 };
46 };
47
48 class ToggleButtonGroup extends Component {
49                 
50                 static propTypes = {
51                                 buttonDefinitions: PropTypes.object.isRequired
52                 };
53                 
54                 onButtonSelect(buttonName) {
55                                 this.props.onButtonToggle(buttonName);
56                 }
57                 
58                 render() {
59                                 let {selectedButton, buttonDefinitions} = this.props;
60                                 let buttonListElements = [];
61                                 Object.keys(buttonDefinitions).map(function (item) {
62                                                 buttonListElements.push(
63                                                                 <Button id={item} active={selectedButton === item ? true : false}
64                                                                         onClick={() => this.onButtonSelect(item)}>
65                                                                                 <i className={buttonDefinitions[item]} aria-hidden='true'></i>
66                                                                 </Button>
67                                                 );
68                                 }.bind(this));
69                                 
70                                 return (
71                                                 <ButtonGroup bsClass='btn-group displayOptionButtons'>
72                                                                 {buttonListElements}
73                                                 </ButtonGroup>
74                                 );
75                 }
76 }
77 export default connect(mapStateToProps, mapActionToProps)(ToggleButtonGroup);