blob: 6c8115deee3a3999fa619f3faf40db5fec41e559 [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}
Avi Zivb8e2faf2017-07-18 19:45:38 +030074 ref={(input) => this.inputWrapper = input}
AviZi280f8012017-06-09 02:39:56 +030075 onChange={(e)=>this.onChangeRadio(e)}
76 data-test-id={this.props['data-test-id']}>{label}</Radio>}
77 {type === 'select' &&
78 <FormControl onClick={ (e) => this.optionSelect(e) }
79 componentClass={type}
80 name={name} {...inputProps}
81 data-test-id={this.props['data-test-id']}/>}
82
83 </FormGroup>
84
85 );
86 }
87
88 getValue() {
89 return this.props.type !== 'select' ? this.state.value : this.state.selectedValues;
90 }
91
92 getChecked() {
93 return this.state.checked;
94 }
95
96 optionSelect(e) {
97 let selectedValues = [];
98 if (e.target.value) {
99 selectedValues.push(e.target.value);
100 }
101 this.setState({
102 selectedValues
103 });
104 }
105
106 onChange(e) {
107 let {onChange} = this.props;
108 this.setState({
109 value: e.target.value
110 });
111 onChange(e.target.value);
112 }
113
114 onChangeCheckBox(e) {
115 let {onChange} = this.props;
116 this.setState({
117 checked: e.target.checked
118 });
119 onChange(e.target.checked);
120 }
121
122 onChangeRadio(e) {
123 let {onChange} = this.props;
124 this.setState({
125 checked: e.target.checked
126 });
127 onChange(this.state.value);
128 }
129
130 focus() {
131 ReactDOM.findDOMNode(this.inputWrapper).focus();
132 }
133
134}
135export default InputWrapper;