blob: 3dcba86def2f527a705b4de2b09b38bb183f9326 [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';
Arielka51eac02019-07-07 12:56:11 +030020import { Radio } from 'onap-ui-react';
AviZi280f8012017-06-09 02:39:56 +030021import FormGroup from 'react-bootstrap/lib/FormGroup.js';
22import FormControl from 'react-bootstrap/lib/FormControl.js';
23
24class InputWrapper extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025 state = {
26 value: this.props.value,
27 checked: this.props.checked,
28 selectedValues: []
29 };
AviZi280f8012017-06-09 02:39:56 +030030
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031 render() {
32 const {
33 label,
34 hasError,
35 validations = {},
36 isReadOnlyMode,
37 value,
38 onBlur,
39 onKeyDown,
40 type,
41 disabled,
42 checked,
43 name
44 } = this.props;
45 const { groupClassName, ...inputProps } = this.props;
46 return (
47 <FormGroup
48 className={classNames('form-group', [groupClassName], {
49 required: validations.required,
50 'has-error': hasError
51 })}>
52 {label &&
53 (type !== 'checkbox' && type !== 'radio') && (
54 <label className="control-label">{label}</label>
55 )}
56 {(type === 'text' || type === 'number') && (
57 <FormControl
58 bsClass={'form-control input-options-other'}
59 onChange={e => this.onChange(e)}
60 disabled={isReadOnlyMode || Boolean(disabled)}
61 onBlur={onBlur}
62 onKeyDown={onKeyDown}
63 value={value || ''}
64 ref={input => (this.inputWrapper = input)}
65 type={type}
66 data-test-id={this.props['data-test-id']}
67 />
68 )}
AviZi280f8012017-06-09 02:39:56 +030069
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020070 {type === 'textarea' && (
71 <FormControl
72 className="form-control input-options-other"
73 disabled={isReadOnlyMode || Boolean(disabled)}
74 value={value || ''}
75 onBlur={onBlur}
76 onKeyDown={onKeyDown}
77 componentClass={type}
78 onChange={e => this.onChange(e)}
79 data-test-id={this.props['data-test-id']}
80 />
81 )}
AviZi280f8012017-06-09 02:39:56 +030082
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020083 {type === 'checkbox' && (
84 <Checkbox
85 className={classNames({
86 required: validations.required,
87 'has-error': hasError
88 })}
89 onChange={e => this.onChangeCheckBox(e)}
90 disabled={isReadOnlyMode || Boolean(disabled)}
91 checked={value}
92 data-test-id={this.props['data-test-id']}>
93 {label}
94 </Checkbox>
95 )}
AviZi280f8012017-06-09 02:39:56 +030096
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020097 {type === 'radio' && (
98 <Radio
99 name={name}
100 checked={checked}
101 disabled={isReadOnlyMode || Boolean(disabled)}
102 value={value}
103 ref={input => (this.inputWrapper = input)}
104 onChange={isChecked => this.onChangeRadio(isChecked)}
105 label={label}
106 data-test-id={this.props['data-test-id']}
107 />
108 )}
109 {type === 'select' && (
110 <FormControl
111 onClick={e => this.optionSelect(e)}
112 componentClass={type}
113 name={name}
114 {...inputProps}
115 data-test-id={this.props['data-test-id']}
116 />
117 )}
118 </FormGroup>
119 );
120 }
AviZi280f8012017-06-09 02:39:56 +0300121
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200122 getValue() {
123 return this.props.type !== 'select'
124 ? this.state.value
125 : this.state.selectedValues;
126 }
AviZi280f8012017-06-09 02:39:56 +0300127
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200128 getChecked() {
129 return this.state.checked;
130 }
AviZi280f8012017-06-09 02:39:56 +0300131
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200132 optionSelect(e) {
133 let selectedValues = [];
134 if (e.target.value) {
135 selectedValues.push(e.target.value);
136 }
137 this.setState({
138 selectedValues
139 });
140 }
AviZi280f8012017-06-09 02:39:56 +0300141
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200142 onChange(e) {
143 let { onChange } = this.props;
144 this.setState({
145 value: e.target.value
146 });
147 onChange(e.target.value);
148 }
AviZi280f8012017-06-09 02:39:56 +0300149
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200150 onChangeCheckBox(e) {
151 let { onChange } = this.props;
152 this.setState({
153 checked: e.target.checked
154 });
155 onChange(e.target.checked);
156 }
AviZi280f8012017-06-09 02:39:56 +0300157
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200158 onChangeRadio(isChecked) {
159 let { onChange } = this.props;
160 this.setState({
161 checked: isChecked
162 });
163 onChange(this.state.value);
164 }
AviZi280f8012017-06-09 02:39:56 +0300165
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200166 focus() {
167 ReactDOM.findDOMNode(this.inputWrapper).focus();
168 }
AviZi280f8012017-06-09 02:39:56 +0300169}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200170export default InputWrapper;