blob: eef8fee1ce6ecdad6eb840d565f3bbc2ea9205a0 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16import React from 'react';
17import ReactDOM from 'react-dom';
18import classNames from 'classnames';
19import Checkbox from 'react-bootstrap/lib/Checkbox.js';
20import Radio from 'react-bootstrap/lib/Radio.js';
21import FormGroup from 'react-bootstrap/lib/FormGroup.js';
22import FormControl from 'react-bootstrap/lib/FormControl.js';
23import Overlay from 'react-bootstrap/lib/Overlay.js';
24import Tooltip from 'react-bootstrap/lib/Tooltip.js';
Avi Zivb8e2faf2017-07-18 19:45:38 +030025import Datepicker from 'nfvo-components/datepicker/Datepicker.jsx';
AviZi280f8012017-06-09 02:39:56 +030026
27class Input extends React.Component {
28
29 state = {
30 value: this.props.value,
31 checked: this.props.checked,
32 selectedValues: []
Avi Zivb8e2faf2017-07-18 19:45:38 +030033 };
AviZi280f8012017-06-09 02:39:56 +030034
35 render() {
36 const {label, isReadOnlyMode, value, onBlur, onKeyDown, type, disabled, checked, name} = this.props;
37 // eslint-disable-next-line no-unused-vars
Avi Zivb8e2faf2017-07-18 19:45:38 +030038 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';
AviZi280f8012017-06-09 02:39:56 +030041 if (disabled) {
42 wrapperClassName += ' disabled';
43 }
44 return(
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>}
Avi Zivb8e2faf2017-07-18 19:45:38 +030048 {type === 'text' &&
AviZi280f8012017-06-09 02:39:56 +030049 <FormControl
50 bsClass={'form-control input-options-other'}
51 onChange={(e) => this.onChange(e)}
52 disabled={isReadOnlyMode || Boolean(disabled)}
53 onBlur={onBlur}
54 onKeyDown={onKeyDown}
55 value={value || ''}
56 inputRef={(input) => this.input = input}
57 type={type}
58 data-test-id={this.props['data-test-id']}/>}
Avi Zivb8e2faf2017-07-18 19:45:38 +030059 {type === 'number' &&
60 <FormControl
61 bsClass={'form-control input-options-other'}
62 onChange={(e) => this.onChange(e)}
63 disabled={isReadOnlyMode || Boolean(disabled)}
64 onBlur={onBlur}
65 onKeyDown={onKeyDown}
66 value={(value !== undefined) ? value : ''}
67 inputRef={(input) => this.input = input}
68 type={type}
69 data-test-id={this.props['data-test-id']}/>}
AviZi280f8012017-06-09 02:39:56 +030070
71 {type === 'textarea' &&
72 <FormControl
73 className='form-control input-options-other'
74 disabled={isReadOnlyMode || Boolean(disabled)}
75 value={value || ''}
76 onBlur={onBlur}
77 onKeyDown={onKeyDown}
78 componentClass={type}
79 onChange={(e) => this.onChange(e)}
80 inputRef={(input) => this.input = input}
81 data-test-id={this.props['data-test-id']}/>}
82
83 {type === 'checkbox' &&
84 <Checkbox
85 className={classNames({'required' : isRequired , 'has-error' : !isValid})}
86 onChange={(e)=>this.onChangeCheckBox(e)}
87 disabled={isReadOnlyMode || Boolean(disabled)}
88 checked={value}
89 data-test-id={this.props['data-test-id']}>{label}</Checkbox>}
90
91 {type === 'radio' &&
92 <Radio name={name}
93 checked={checked}
94 disabled={isReadOnlyMode || Boolean(disabled)}
95 value={value}
96 onChange={(e)=>this.onChangeRadio(e)}
Avi Zivb8e2faf2017-07-18 19:45:38 +030097 inputRef={(input) => this.input = input}
AviZi280f8012017-06-09 02:39:56 +030098 data-test-id={this.props['data-test-id']}>{label}</Radio>}
99 {type === 'select' &&
100 <FormControl onClick={ (e) => this.optionSelect(e) }
101 componentClass={type}
102 inputRef={(input) => this.input = input}
103 name={name} {...inputProps}
104 data-test-id={this.props['data-test-id']}/>}
Avi Zivb8e2faf2017-07-18 19:45:38 +0300105 {type === 'date' &&
106 <Datepicker
107 date={value}
108 format={dateFormat}
109 startDate={startDate}
110 endDate={endDate}
111 inputRef={(input) => this.input = input}
112 onChange={this.props.onChange}
113 disabled={isReadOnlyMode || Boolean(disabled)}
114 data-test-id={this.props['data-test-id']}
115 selectsStart={selectsStart}
116 selectsEnd={selectsEnd} />}
AviZi280f8012017-06-09 02:39:56 +0300117 </FormGroup>
118 { this.renderErrorOverlay() }
119 </div>
120 );
121 }
122
123 getValue() {
124 return this.props.type !== 'select' ? this.state.value : this.state.selectedValues;
125 }
126
127 getChecked() {
128 return this.state.checked;
129 }
130
131 optionSelect(e) {
132 let selectedValues = [];
133 if (e.target.value) {
134 selectedValues.push(e.target.value);
135 }
136 this.setState({
137 selectedValues
138 });
139 }
140
141 onChange(e) {
142 const {onChange, type} = this.props;
143 let value = e.target.value;
144 if (type === 'number') {
Avi Zivb8e2faf2017-07-18 19:45:38 +0300145 if (value === '') {
146 value = undefined;
147 } else {
148 value = Number(value);
149 }
AviZi280f8012017-06-09 02:39:56 +0300150 }
151 this.setState({
152 value
153 });
154 onChange(value);
155 }
156
157 onChangeCheckBox(e) {
158 let {onChange} = this.props;
159 this.setState({
160 checked: e.target.checked
161 });
162 onChange(e.target.checked);
163 }
164
165 onChangeRadio(e) {
166 let {onChange} = this.props;
167 this.setState({
168 checked: e.target.checked
169 });
170 onChange(this.state.value);
171 }
172
173 focus() {
174 ReactDOM.findDOMNode(this.input).focus();
175 }
176
177 renderErrorOverlay() {
178 let position = 'right';
179 const {errorText = '', isValid = true, type, overlayPos} = this.props;
180
181 if (overlayPos) {
182 position = overlayPos;
183 }
184 else if (type === 'text'
185 || type === 'email'
186 || type === 'number'
Avi Zivb8e2faf2017-07-18 19:45:38 +0300187 || type === 'radio'
188 || type === 'password'
189 || type === 'date') {
AviZi280f8012017-06-09 02:39:56 +0300190 position = 'bottom';
191 }
192
193 return (
194 <Overlay
195 show={!isValid}
196 placement={position}
197 target={() => {
198 let target = ReactDOM.findDOMNode(this.input);
199 return target.offsetParent ? target : undefined;
200 }}
201 container={this}>
202 <Tooltip
203 id={`error-${errorText.replace(' ', '-')}`}
204 className='validation-error-message'>
205 {errorText}
206 </Tooltip>
207 </Overlay>
208 );
209 }
210
211}
212export default Input;