[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-ui / src / nfvo-components / input / validation / InputWrapper.jsx
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  */
16 import React from 'react';
17 import ReactDOM from 'react-dom';
18 import classNames from 'classnames';
19 import Checkbox from 'react-bootstrap/lib/Checkbox.js';
20 import Radio from 'react-bootstrap/lib/Radio.js';
21 import FormGroup from 'react-bootstrap/lib/FormGroup.js';
22 import FormControl from 'react-bootstrap/lib/FormControl.js';
23
24 class InputWrapper extends React.Component {
25
26         state = {
27                 value: this.props.value,
28                 checked: this.props.checked,
29                 selectedValues: []
30         }
31
32         render() {
33                 const {label, hasError, validations = {}, isReadOnlyMode, value, onBlur, onKeyDown, type, disabled, checked, name} = this.props;
34                 const {groupClassName, ...inputProps} = this.props;
35                 return(
36                         <FormGroup className={classNames('form-group', [groupClassName], {'required' : validations.required , 'has-error' : hasError})} >
37                                 {(label && (type !== 'checkbox' && type !== 'radio')) && <label className='control-label'>{label}</label>}
38                                 {(type === 'text' || type === 'number') &&
39                                         <FormControl
40                                                 bsClass={'form-control input-options-other'}
41                                                 onChange={(e) => this.onChange(e)}
42                                                 disabled={isReadOnlyMode || Boolean(disabled)}
43                                                 onBlur={onBlur}
44                                                 onKeyDown={onKeyDown}
45                                                 value={value || ''}
46                                                 ref={(input) => this.inputWrapper = input}
47                                                 type={type}
48                                                 data-test-id={this.props['data-test-id']}/>}
49
50                                 {type === 'textarea' &&
51                                         <FormControl
52                                                 className='form-control input-options-other'
53                                                 disabled={isReadOnlyMode || Boolean(disabled)}
54                                                 value={value || ''}
55                                                 onBlur={onBlur}
56                                                 onKeyDown={onKeyDown}
57                                                 componentClass={type}
58                                                 onChange={(e) => this.onChange(e)}
59                                                 data-test-id={this.props['data-test-id']}/>}
60
61                                 {type === 'checkbox' &&
62                                         <Checkbox
63                                                 className={classNames({'required' : validations.required , 'has-error' : hasError})}
64                                                 onChange={(e)=>this.onChangeCheckBox(e)}
65                                                 disabled={isReadOnlyMode || Boolean(disabled)}
66                                                 checked={value}
67                                                 data-test-id={this.props['data-test-id']}>{label}</Checkbox>}
68
69                                 {type === 'radio' &&
70                                         <Radio name={name}
71                                                 checked={checked}
72                                                 disabled={isReadOnlyMode || Boolean(disabled)}
73                                                 value={value}
74                                                 ref={(input) => this.inputWrapper = input}
75                                                 onChange={(e)=>this.onChangeRadio(e)}
76                                                 data-test-id={this.props['data-test-id']}>{label}</Radio>}
77                                 {type === 'select' &&
78                                         <FormControl onClick={ (e) => this.optionSelect(e) }
79                                                 componentClass={type}
80                                                 name={name} {...inputProps}
81                                                 data-test-id={this.props['data-test-id']}/>}
82
83                         </FormGroup>
84
85                 );
86         }
87
88         getValue() {
89                 return this.props.type !== 'select' ? this.state.value : this.state.selectedValues;
90         }
91
92         getChecked() {
93                 return this.state.checked;
94         }
95
96         optionSelect(e) {
97                 let selectedValues = [];
98                 if (e.target.value) {
99                         selectedValues.push(e.target.value);
100                 }
101                 this.setState({
102                         selectedValues
103                 });
104         }
105
106         onChange(e) {
107                 let {onChange} = this.props;
108                 this.setState({
109                         value: e.target.value
110                 });
111                 onChange(e.target.value);
112         }
113
114         onChangeCheckBox(e) {
115                 let {onChange} = this.props;
116                 this.setState({
117                         checked: e.target.checked
118                 });
119                 onChange(e.target.checked);
120         }
121
122         onChangeRadio(e) {
123                 let {onChange} = this.props;
124                 this.setState({
125                         checked: e.target.checked
126                 });
127                 onChange(this.state.value);
128         }
129
130         focus() {
131                 ReactDOM.findDOMNode(this.inputWrapper).focus();
132         }
133
134 }
135 export default  InputWrapper;