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 '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 {
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={(isChecked)=>this.onChangeRadio(isChecked)}
97 inputRef={(input) => this.input = input}
99 data-test-id={this.props['data-test-id']} />}
100 {type === 'select' &&
101 <FormControl onClick={ (e) => this.optionSelect(e) }
102 componentClass={type}
103 inputRef={(input) => this.input = input}
104 name={name} {...inputProps}
105 data-test-id={this.props['data-test-id']}/>}
110 startDate={startDate}
112 inputRef={(input) => this.input = input}
113 onChange={this.props.onChange}
114 disabled={isReadOnlyMode || Boolean(disabled)}
115 data-test-id={this.props['data-test-id']}
116 selectsStart={selectsStart}
117 selectsEnd={selectsEnd} />}
119 { this.renderErrorOverlay() }
125 return this.props.type !== 'select' ? this.state.value : this.state.selectedValues;
129 return this.state.checked;
133 let selectedValues = [];
134 if (e.target.value) {
135 selectedValues.push(e.target.value);
143 const {onChange, type} = this.props;
144 let value = e.target.value;
145 if (type === 'number') {
149 value = Number(value);
158 onChangeCheckBox(e) {
159 let {onChange} = this.props;
160 let checked = e.target.checked;
167 onChangeRadio(isChecked) {
168 let {onChange} = this.props;
172 onChange(this.state.value);
176 ReactDOM.findDOMNode(this.input).focus();
179 renderErrorOverlay() {
180 let position = 'right';
181 const {errorText = '', isValid = true, type, overlayPos} = this.props;
184 position = overlayPos;
186 else if (type === 'text'
190 || type === 'password'
191 || type === 'date') {
200 let target = ReactDOM.findDOMNode(this.input);
201 return target.offsetParent ? target : undefined;
205 id={`error-${errorText.replace(' ', '-')}`}
206 className='validation-error-message'>
214 export default Input;