AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
| 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 | */ |
| 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'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 20 | import Radio from 'sdc-ui/lib/react/Radio.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 21 | import FormGroup from 'react-bootstrap/lib/FormGroup.js'; |
| 22 | import FormControl from 'react-bootstrap/lib/FormControl.js'; |
| 23 | |
| 24 | class InputWrapper extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 25 | state = { |
| 26 | value: this.props.value, |
| 27 | checked: this.props.checked, |
| 28 | selectedValues: [] |
| 29 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 30 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 31 | 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 | )} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 69 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 70 | {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 | )} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 82 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 83 | {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 | )} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 96 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 97 | {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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 121 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 122 | getValue() { |
| 123 | return this.props.type !== 'select' |
| 124 | ? this.state.value |
| 125 | : this.state.selectedValues; |
| 126 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 127 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 128 | getChecked() { |
| 129 | return this.state.checked; |
| 130 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 131 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 132 | optionSelect(e) { |
| 133 | let selectedValues = []; |
| 134 | if (e.target.value) { |
| 135 | selectedValues.push(e.target.value); |
| 136 | } |
| 137 | this.setState({ |
| 138 | selectedValues |
| 139 | }); |
| 140 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 141 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 142 | onChange(e) { |
| 143 | let { onChange } = this.props; |
| 144 | this.setState({ |
| 145 | value: e.target.value |
| 146 | }); |
| 147 | onChange(e.target.value); |
| 148 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 149 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 150 | onChangeCheckBox(e) { |
| 151 | let { onChange } = this.props; |
| 152 | this.setState({ |
| 153 | checked: e.target.checked |
| 154 | }); |
| 155 | onChange(e.target.checked); |
| 156 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 157 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 158 | onChangeRadio(isChecked) { |
| 159 | let { onChange } = this.props; |
| 160 | this.setState({ |
| 161 | checked: isChecked |
| 162 | }); |
| 163 | onChange(this.state.value); |
| 164 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 165 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 166 | focus() { |
| 167 | ReactDOM.findDOMNode(this.inputWrapper).focus(); |
| 168 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 169 | } |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 170 | export default InputWrapper; |