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 'onap-ui-react';
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']}
92 placeholder={this.props.placeholder || ''}
95 {type === 'number' && (
97 bsClass={'form-control input-options-other'}
98 onChange={e => this.onChange(e)}
99 disabled={isReadOnlyMode || Boolean(disabled)}
101 onKeyDown={onKeyDown}
102 value={value !== undefined ? value : ''}
103 inputRef={input => (this.input = input)}
105 data-test-id={this.props['data-test-id']}
108 {type === 'file' && (
110 bsClass={'form-control input-options-other'}
111 onChange={e => this.onChangeFile(e)}
112 disabled={isReadOnlyMode || Boolean(disabled)}
114 data-test-id={this.props['data-test-id']}
115 inputRef={input => (this.input = input)}
118 {type === 'textarea' && (
120 className="form-control input-options-other"
121 disabled={isReadOnlyMode || Boolean(disabled)}
124 onKeyDown={onKeyDown}
125 componentClass={type}
126 onChange={e => this.onChange(e)}
127 inputRef={input => (this.input = input)}
128 data-test-id={this.props['data-test-id']}
132 {type === 'checkbox' && (
134 className={classNames({
135 required: isRequired,
136 'has-error': !isValid
138 onChange={e => this.onChangeCheckBox(e)}
139 disabled={isReadOnlyMode || Boolean(disabled)}
141 data-test-id={this.props['data-test-id']}>
146 {type === 'radio' && (
150 disabled={isReadOnlyMode || Boolean(disabled)}
152 onChange={isChecked =>
153 this.onChangeRadio(isChecked)
155 inputRef={input => (this.input = input)}
157 data-test-id={this.props['data-test-id']}
160 {type === 'select' && (
162 onClick={e => this.optionSelect(e)}
163 className="custom-select"
164 componentClass={type}
165 inputRef={input => (this.input = input)}
168 data-test-id={this.props['data-test-id']}
171 {type === 'date' && (
175 startDate={startDate}
177 inputRef={input => (this.input = input)}
178 onChange={this.props.onChange}
179 disabled={isReadOnlyMode || Boolean(disabled)}
180 data-test-id={this.props['data-test-id']}
181 selectsStart={selectsStart}
182 selectsEnd={selectsEnd}
186 {this.renderErrorOverlay()}
192 return this.props.type !== 'select'
194 : this.state.selectedValues;
198 return this.state.checked;
202 let selectedValues = [];
203 if (e.target.value) {
204 selectedValues.push(e.target.value);
211 static getDerivedStateFromProps(props, state) {
212 if (state.value === props.value) {
215 return { value: props.value, ...state };
220 const { onChange, type } = this.props;
221 let value = e.target.value;
222 if (type === 'number') {
226 value = Number(value);
233 let { onChange } = this.props;
234 onChange(e.target.files[0]);
237 onChangeCheckBox(e) {
238 let { onChange } = this.props;
239 let checked = e.target.checked;
246 onChangeRadio(isChecked) {
247 let { onChange } = this.props;
251 onChange(this.state.value);
255 ReactDOM.findDOMNode(this.input).focus();
258 renderErrorOverlay() {
259 let position = 'right';
260 const { errorText = '', isValid = true, type, overlayPos } = this.props;
263 position = overlayPos;
269 type === 'password' ||
280 let target = ReactDOM.findDOMNode(this.input);
281 return target.offsetParent ? target : undefined;
285 id={`error-${errorText.replace(' ', '-')}`}
286 className="validation-error-message">
293 export default Input;