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';
26 class Input extends React.Component {
29 value: this.props.value,
30 checked: this.props.checked,
35 const {label, isReadOnlyMode, value, onBlur, onKeyDown, type, disabled, checked, name} = this.props;
36 // eslint-disable-next-line no-unused-vars
37 const {groupClassName, isValid = true, errorText, isRequired, ...inputProps} = this.props;
38 let wrapperClassName = (type !== 'radio') ? 'validation-input-wrapper' : 'form-group';
40 wrapperClassName += ' disabled';
43 <div className={wrapperClassName}>
44 <FormGroup className={classNames('form-group', [groupClassName], {'required' : isRequired , 'has-error' : !isValid})} >
45 {(label && (type !== 'checkbox' && type !== 'radio')) && <label className='control-label'>{label}</label>}
46 {(type === 'text' || type === 'number') &&
48 bsClass={'form-control input-options-other'}
49 onChange={(e) => this.onChange(e)}
50 disabled={isReadOnlyMode || Boolean(disabled)}
54 inputRef={(input) => this.input = input}
56 data-test-id={this.props['data-test-id']}/>}
58 {type === 'textarea' &&
60 className='form-control input-options-other'
61 disabled={isReadOnlyMode || Boolean(disabled)}
66 onChange={(e) => this.onChange(e)}
67 inputRef={(input) => this.input = input}
68 data-test-id={this.props['data-test-id']}/>}
70 {type === 'checkbox' &&
72 className={classNames({'required' : isRequired , 'has-error' : !isValid})}
73 onChange={(e)=>this.onChangeCheckBox(e)}
74 disabled={isReadOnlyMode || Boolean(disabled)}
76 data-test-id={this.props['data-test-id']}>{label}</Checkbox>}
81 disabled={isReadOnlyMode || Boolean(disabled)}
83 onChange={(e)=>this.onChangeRadio(e)}
84 data-test-id={this.props['data-test-id']}>{label}</Radio>}
86 <FormControl onClick={ (e) => this.optionSelect(e) }
88 inputRef={(input) => this.input = input}
89 name={name} {...inputProps}
90 data-test-id={this.props['data-test-id']}/>}
92 { this.renderErrorOverlay() }
98 return this.props.type !== 'select' ? this.state.value : this.state.selectedValues;
102 return this.state.checked;
106 let selectedValues = [];
107 if (e.target.value) {
108 selectedValues.push(e.target.value);
116 const {onChange, type} = this.props;
117 let value = e.target.value;
118 if (type === 'number') {
119 value = Number(value);
127 onChangeCheckBox(e) {
128 let {onChange} = this.props;
130 checked: e.target.checked
132 onChange(e.target.checked);
136 let {onChange} = this.props;
138 checked: e.target.checked
140 onChange(this.state.value);
144 ReactDOM.findDOMNode(this.input).focus();
147 renderErrorOverlay() {
148 let position = 'right';
149 const {errorText = '', isValid = true, type, overlayPos} = this.props;
152 position = overlayPos;
154 else if (type === 'text'
157 || type === 'password') {
166 let target = ReactDOM.findDOMNode(this.input);
167 return target.offsetParent ? target : undefined;
171 id={`error-${errorText.replace(' ', '-')}`}
172 className='validation-error-message'>
180 export default Input;