blob: 03c727379e03a3083969773c6a68181aef5bd93d [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
Michael Landoefa037d2017-02-19 12:57:33 +020016/**
17 * The HTML structure here is aligned with bootstrap HTML structure for form elements.
18 * In this way we have proper styling and it is aligned with other form elements on screen.
19 *
20 * Select and MultiSelect options:
21 *
22 * label - the label to be shown which paired with the input
23 *
24 * all other "react-select" props - as documented on
25 * http://jedwatson.github.io/react-select/
26 * or
27 * https://github.com/JedWatson/react-select
28 */
29import React, {Component} from 'react';
30import Select from 'react-select';
31
32class SelectInput extends Component {
33
34 inputValue = [];
35
36 render() {
37 let {label, value, ...other} = this.props;
AviZi280f8012017-06-09 02:39:56 +030038 const dataTestId = this.props['data-test-id'] ? {'data-test-id': this.props['data-test-id']} : {};
Michael Landoefa037d2017-02-19 12:57:33 +020039 return (
AviZi280f8012017-06-09 02:39:56 +030040 <div {...dataTestId} className='validation-input-wrapper dropdown-multi-select'>
Michael Landoefa037d2017-02-19 12:57:33 +020041 <div className='form-group'>
42 {label && <label className='control-label'>{label}</label>}
43 <Select ref='_myInput' onChange={value => this.onSelectChanged(value)} {...other} value={value} />
44 </div>
45 </div>
46 );
47 }
48
49 getValue() {
50 return this.inputValue && this.inputValue.length ? this.inputValue : '';
51 }
52
53 onSelectChanged(value) {
54 this.props.onMultiSelectChanged(value);
55 }
56
57 componentDidMount() {
58 let {value} = this.props;
59 this.inputValue = value ? value : [];
60 }
61 componentDidUpdate() {
62 if (this.inputValue !== this.props.value) {
63 this.inputValue = this.props.value;
64 }
65 }
66}
67
68export default SelectInput;