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']}
109 {type === 'textarea' && (
111 className="form-control input-options-other"
112 disabled={isReadOnlyMode || Boolean(disabled)}
115 onKeyDown={onKeyDown}
116 componentClass={type}
117 onChange={e => this.onChange(e)}
118 inputRef={input => (this.input = input)}
119 data-test-id={this.props['data-test-id']}
123 {type === 'checkbox' && (
125 className={classNames({
126 required: isRequired,
127 'has-error': !isValid
129 onChange={e => this.onChangeCheckBox(e)}
130 disabled={isReadOnlyMode || Boolean(disabled)}
132 data-test-id={this.props['data-test-id']}>
137 {type === 'radio' && (
141 disabled={isReadOnlyMode || Boolean(disabled)}
143 onChange={isChecked =>
144 this.onChangeRadio(isChecked)
146 inputRef={input => (this.input = input)}
148 data-test-id={this.props['data-test-id']}
151 {type === 'select' && (
153 onClick={e => this.optionSelect(e)}
154 className="custom-select"
155 componentClass={type}
156 inputRef={input => (this.input = input)}
159 data-test-id={this.props['data-test-id']}
162 {type === 'date' && (
166 startDate={startDate}
168 inputRef={input => (this.input = input)}
169 onChange={this.props.onChange}
170 disabled={isReadOnlyMode || Boolean(disabled)}
171 data-test-id={this.props['data-test-id']}
172 selectsStart={selectsStart}
173 selectsEnd={selectsEnd}
177 {this.renderErrorOverlay()}
183 return this.props.type !== 'select'
185 : this.state.selectedValues;
189 return this.state.checked;
193 let selectedValues = [];
194 if (e.target.value) {
195 selectedValues.push(e.target.value);
202 static getDerivedStateFromProps(props, state) {
203 if (state.value === props.value) {
206 return { value: props.value, ...state };
211 const { onChange, type } = this.props;
212 let value = e.target.value;
213 if (type === 'number') {
217 value = Number(value);
223 onChangeCheckBox(e) {
224 let { onChange } = this.props;
225 let checked = e.target.checked;
232 onChangeRadio(isChecked) {
233 let { onChange } = this.props;
237 onChange(this.state.value);
241 ReactDOM.findDOMNode(this.input).focus();
244 renderErrorOverlay() {
245 let position = 'right';
246 const { errorText = '', isValid = true, type, overlayPos } = this.props;
249 position = overlayPos;
255 type === 'password' ||
266 let target = ReactDOM.findDOMNode(this.input);
267 return target.offsetParent ? target : undefined;
271 id={`error-${errorText.replace(' ', '-')}`}
272 className="validation-error-message">
279 export default Input;