2 * Copyright © 2016-2018 European Support Limited
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 or implied.
13 * See the License for the specific language governing permissions and
14 * 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 'sdc-ui/lib/react/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 {
29 value: this.props.value,
30 checked: this.props.checked,
35 /* eslint-disable no-unused-vars */
61 } = this.props; // Date Props
62 /* eslint-enable no-unused-vars */
63 let wrapperClassName =
65 ? 'validation-input-wrapper'
66 : 'validation-radio-wrapper';
68 wrapperClassName += ' disabled';
71 <div className={wrapperClassName}>
73 className={classNames('form-group', [groupClassName], {
78 (type !== 'checkbox' && type !== 'radio') && (
79 <label className="control-label">{label}</label>
83 bsClass={'form-control input-options-other'}
84 onChange={e => this.onChange(e)}
85 disabled={isReadOnlyMode || Boolean(disabled)}
89 inputRef={input => (this.input = input)}
91 data-test-id={this.props['data-test-id']}
94 {type === 'number' && (
96 bsClass={'form-control input-options-other'}
97 onChange={e => this.onChange(e)}
98 disabled={isReadOnlyMode || Boolean(disabled)}
100 onKeyDown={onKeyDown}
101 value={value !== undefined ? value : ''}
102 inputRef={input => (this.input = input)}
104 data-test-id={this.props['data-test-id']}
108 {type === 'textarea' && (
110 className="form-control input-options-other"
111 disabled={isReadOnlyMode || Boolean(disabled)}
114 onKeyDown={onKeyDown}
115 componentClass={type}
116 onChange={e => this.onChange(e)}
117 inputRef={input => (this.input = input)}
118 data-test-id={this.props['data-test-id']}
122 {type === 'checkbox' && (
124 className={classNames({
125 required: isRequired,
126 'has-error': !isValid
128 onChange={e => this.onChangeCheckBox(e)}
129 disabled={isReadOnlyMode || Boolean(disabled)}
131 data-test-id={this.props['data-test-id']}>
136 {type === 'radio' && (
140 disabled={isReadOnlyMode || Boolean(disabled)}
142 onChange={isChecked =>
143 this.onChangeRadio(isChecked)
145 inputRef={input => (this.input = input)}
147 data-test-id={this.props['data-test-id']}
150 {type === 'select' && (
152 onClick={e => this.optionSelect(e)}
153 className="custom-select"
154 componentClass={type}
155 inputRef={input => (this.input = input)}
158 data-test-id={this.props['data-test-id']}
161 {type === 'date' && (
165 startDate={startDate}
167 inputRef={input => (this.input = input)}
168 onChange={this.props.onChange}
169 disabled={isReadOnlyMode || Boolean(disabled)}
170 data-test-id={this.props['data-test-id']}
171 selectsStart={selectsStart}
172 selectsEnd={selectsEnd}
176 {this.renderErrorOverlay()}
182 return this.props.type !== 'select'
184 : this.state.selectedValues;
188 return this.state.checked;
192 let selectedValues = [];
193 if (e.target.value) {
194 selectedValues.push(e.target.value);
201 static getDerivedStateFromProps(props, state) {
202 if (state.value === props.value) {
205 return { value: props.value, ...state };
210 const { onChange, type } = this.props;
211 let value = e.target.value;
212 if (type === 'number') {
216 value = Number(value);
222 onChangeCheckBox(e) {
223 let { onChange } = this.props;
224 let checked = e.target.checked;
231 onChangeRadio(isChecked) {
232 let { onChange } = this.props;
236 onChange(this.state.value);
240 ReactDOM.findDOMNode(this.input).focus();
243 renderErrorOverlay() {
244 let position = 'right';
245 const { errorText = '', isValid = true, type, overlayPos } = this.props;
248 position = overlayPos;
254 type === 'password' ||
265 let target = ReactDOM.findDOMNode(this.input);
266 return target.offsetParent ? target : undefined;
270 id={`error-${errorText.replace(' ', '-')}`}
271 className="validation-error-message">
278 export default Input;