AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
| 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | /** |
| 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 | */ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | import React, { Component } from 'react'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 30 | import Select from 'react-select'; |
| 31 | |
| 32 | class SelectInput extends Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 33 | inputValue = []; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 34 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 35 | render() { |
| 36 | let { label, value, ...other } = this.props; |
| 37 | const dataTestId = this.props['data-test-id'] |
| 38 | ? { 'data-test-id': this.props['data-test-id'] } |
| 39 | : {}; |
| 40 | return ( |
| 41 | <div |
| 42 | {...dataTestId} |
| 43 | className="validation-input-wrapper dropdown-multi-select"> |
| 44 | <div className="form-group"> |
| 45 | {label && <label className="control-label">{label}</label>} |
| 46 | <Select |
| 47 | ref="_myInput" |
| 48 | onChange={value => this.onSelectChanged(value)} |
| 49 | {...other} |
| 50 | value={value} |
| 51 | /> |
| 52 | </div> |
| 53 | </div> |
| 54 | ); |
| 55 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 56 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 57 | getValue() { |
| 58 | return this.inputValue && this.inputValue.length ? this.inputValue : ''; |
| 59 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 60 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 61 | onSelectChanged(value) { |
| 62 | this.props.onMultiSelectChanged(value); |
| 63 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 64 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 65 | componentDidMount() { |
| 66 | let { value } = this.props; |
| 67 | this.inputValue = value ? value : []; |
| 68 | } |
| 69 | componentDidUpdate() { |
| 70 | if (this.inputValue !== this.props.value) { |
| 71 | this.inputValue = this.props.value; |
| 72 | } |
| 73 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | export default SelectInput; |