2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 import Overlay from 'react-bootstrap/lib/Overlay.js';
24 import Tooltip from 'react-bootstrap/lib/Tooltip.js';
25 import Datepicker from 'nfvo-components/datepicker/Datepicker.jsx';
27 class Input extends React.Component {
30 value: this.props.value,
31 checked: this.props.checked,
36 const {label, isReadOnlyMode, value, onBlur, onKeyDown, type, disabled, checked, name} = this.props;
37 // eslint-disable-next-line no-unused-vars
38 const {groupClassName, isValid = true, errorText, isRequired, overlayPos, ...inputProps} = this.props;
39 const {dateFormat, startDate, endDate, selectsStart, selectsEnd} = this.props; // Date Props
40 let wrapperClassName = (type !== 'radio') ? 'validation-input-wrapper' : 'validation-radio-wrapper';
42 wrapperClassName += ' disabled';
45 <div className={wrapperClassName}>
46 <FormGroup className={classNames('form-group', [groupClassName], {'required' : isRequired , 'has-error' : !isValid})} >
47 {(label && (type !== 'checkbox' && type !== 'radio')) && <label className='control-label'>{label}</label>}
50 bsClass={'form-control input-options-other'}
51 onChange={(e) => this.onChange(e)}
52 disabled={isReadOnlyMode || Boolean(disabled)}
56 inputRef={(input) => this.input = input}
58 data-test-id={this.props['data-test-id']}/>}
61 bsClass={'form-control input-options-other'}
62 onChange={(e) => this.onChange(e)}
63 disabled={isReadOnlyMode || Boolean(disabled)}
66 value={(value !== undefined) ? value : ''}
67 inputRef={(input) => this.input = input}
69 data-test-id={this.props['data-test-id']}/>}
71 {type === 'textarea' &&
73 className='form-control input-options-other'
74 disabled={isReadOnlyMode || Boolean(disabled)}
79 onChange={(e) => this.onChange(e)}
80 inputRef={(input) => this.input = input}
81 data-test-id={this.props['data-test-id']}/>}
83 {type === 'checkbox' &&
85 className={classNames({'required' : isRequired , 'has-error' : !isValid})}
86 onChange={(e)=>this.onChangeCheckBox(e)}
87 disabled={isReadOnlyMode || Boolean(disabled)}
89 data-test-id={this.props['data-test-id']}>{label}</Checkbox>}
94 disabled={isReadOnlyMode || Boolean(disabled)}
96 onChange={(e)=>this.onChangeRadio(e)}
97 inputRef={(input) => this.input = input}
98 data-test-id={this.props['data-test-id']}>{label}</Radio>}
100 <FormControl onClick={ (e) => this.optionSelect(e) }
101 componentClass={type}
102 inputRef={(input) => this.input = input}
103 name={name} {...inputProps}
104 data-test-id={this.props['data-test-id']}/>}
109 startDate={startDate}
111 inputRef={(input) => this.input = input}
112 onChange={this.props.onChange}
113 disabled={isReadOnlyMode || Boolean(disabled)}
114 data-test-id={this.props['data-test-id']}
115 selectsStart={selectsStart}
116 selectsEnd={selectsEnd} />}
118 { this.renderErrorOverlay() }
124 return this.props.type !== 'select' ? this.state.value : this.state.selectedValues;
128 return this.state.checked;
132 let selectedValues = [];
133 if (e.target.value) {
134 selectedValues.push(e.target.value);
142 const {onChange, type} = this.props;
143 let value = e.target.value;
144 if (type === 'number') {
148 value = Number(value);
157 onChangeCheckBox(e) {
158 let {onChange} = this.props;
160 checked: e.target.checked
162 onChange(e.target.checked);
166 let {onChange} = this.props;
168 checked: e.target.checked
170 onChange(this.state.value);
174 ReactDOM.findDOMNode(this.input).focus();
177 renderErrorOverlay() {
178 let position = 'right';
179 const {errorText = '', isValid = true, type, overlayPos} = this.props;
182 position = overlayPos;
184 else if (type === 'text'
188 || type === 'password'
189 || type === 'date') {
198 let target = ReactDOM.findDOMNode(this.input);
199 return target.offsetParent ? target : undefined;
203 id={`error-${errorText.replace(' ', '-')}`}
204 className='validation-error-message'>
212 export default Input;