Initial coomit for AAI-UI(sparky-fe)
[aai/sparky-fe.git] / src / generic-components / input / SelectInput.jsx
1 /*
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 /**
27         * The HTML structure here is aligned with bootstrap HTML structure for form
28         * elements. In this way we have proper styling and it is aligned with other
29         * form elements on screen.
30         *
31         * Select and MultiSelect options:
32         *
33         * label - the label to be shown which paired with the input
34         *
35         * all other "react-select" props - as documented on
36         * http://jedwatson.github.io/react-select/
37         * or
38         * https://github.com/JedWatson/react-select
39         */
40 import React, {Component} from 'react';
41 import Select from 'react-select';
42
43 class SelectInput extends Component {
44                 
45                 inputValue = [];
46                 
47                 render() {
48                                 let {label, value, ...other} = this.props;
49                                 return (
50                                                 <div className='validation-input-wrapper dropdown-multi-select'>
51                                                                 <div className='form-group'>
52                                                                                 {label && <label className='control-label'>{label}</label>}
53                                                                                 <Select ref='_myInput'
54                                                                                         onChange={value => this.onSelectChanged(value)} {...other}
55                                                                                         value={value}/>
56                                                                 </div>
57                                                 </div>
58                                 );
59                 }
60                 
61                 getValue() {
62                                 return this.inputValue && this.inputValue.length ? this.inputValue : '';
63                 }
64                 
65                 onSelectChanged(value) {
66                                 this.props.onMultiSelectChanged(value);
67                 }
68                 
69                 componentDidMount() {
70                                 let {value} = this.props;
71                                 this.inputValue = value ? value : [];
72                 }
73                 
74                 componentDidUpdate() {
75                                 if (this.inputValue !== this.props.value) {
76                                                 this.inputValue = this.props.value;
77                                 }
78                 }
79 }
80
81 export default SelectInput;