blob: 5ca716cc20799ab21d66b468c0213385fc1b099e [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';
23
24class InputWrapper extends React.Component {
25
26 state = {
27 value: this.props.value,
28 checked: this.props.checked,
29 selectedValues: []
30 }
31
32 render() {
33 const {label, hasError, validations = {}, isReadOnlyMode, value, onBlur, onKeyDown, type, disabled, checked, name} = this.props;
34 const {groupClassName, ...inputProps} = this.props;
35 return(
36 <FormGroup className={classNames('form-group', [groupClassName], {'required' : validations.required , 'has-error' : hasError})} >
37 {(label && (type !== 'checkbox' && type !== 'radio')) && <label className='control-label'>{label}</label>}
38 {(type === 'text' || type === 'number') &&
39 <FormControl
40 bsClass={'form-control input-options-other'}
41 onChange={(e) => this.onChange(e)}
42 disabled={isReadOnlyMode || Boolean(disabled)}
43 onBlur={onBlur}
44 onKeyDown={onKeyDown}
45 value={value || ''}
46 ref={(input) => this.inputWrapper = input}
47 type={type}
48 data-test-id={this.props['data-test-id']}/>}
49
50 {type === 'textarea' &&
51 <FormControl
52 className='form-control input-options-other'
53 disabled={isReadOnlyMode || Boolean(disabled)}
54 value={value || ''}
55 onBlur={onBlur}
56 onKeyDown={onKeyDown}
57 componentClass={type}
58 onChange={(e) => this.onChange(e)}
59 data-test-id={this.props['data-test-id']}/>}
60
61 {type === 'checkbox' &&
62 <Checkbox
63 className={classNames({'required' : validations.required , 'has-error' : hasError})}
64 onChange={(e)=>this.onChangeCheckBox(e)}
65 disabled={isReadOnlyMode || Boolean(disabled)}
66 checked={value}
67 data-test-id={this.props['data-test-id']}>{label}</Checkbox>}
68
69 {type === 'radio' &&
70 <Radio name={name}
71 checked={checked}
72 disabled={isReadOnlyMode || Boolean(disabled)}
73 value={value}
74 onChange={(e)=>this.onChangeRadio(e)}
75 data-test-id={this.props['data-test-id']}>{label}</Radio>}
76 {type === 'select' &&
77 <FormControl onClick={ (e) => this.optionSelect(e) }
78 componentClass={type}
79 name={name} {...inputProps}
80 data-test-id={this.props['data-test-id']}/>}
81
82 </FormGroup>
83
84 );
85 }
86
87 getValue() {
88 return this.props.type !== 'select' ? this.state.value : this.state.selectedValues;
89 }
90
91 getChecked() {
92 return this.state.checked;
93 }
94
95 optionSelect(e) {
96 let selectedValues = [];
97 if (e.target.value) {
98 selectedValues.push(e.target.value);
99 }
100 this.setState({
101 selectedValues
102 });
103 }
104
105 onChange(e) {
106 let {onChange} = this.props;
107 this.setState({
108 value: e.target.value
109 });
110 onChange(e.target.value);
111 }
112
113 onChangeCheckBox(e) {
114 let {onChange} = this.props;
115 this.setState({
116 checked: e.target.checked
117 });
118 onChange(e.target.checked);
119 }
120
121 onChangeRadio(e) {
122 let {onChange} = this.props;
123 this.setState({
124 checked: e.target.checked
125 });
126 onChange(this.state.value);
127 }
128
129 focus() {
130 ReactDOM.findDOMNode(this.inputWrapper).focus();
131 }
132
133}
134export default InputWrapper;