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 { |
| 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 74 | ref={(input) => this.inputWrapper = input} |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 75 | onChange={(isChecked)=>this.onChangeRadio(isChecked)} label={label} |
| 76 | data-test-id={this.props['data-test-id']} />} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 122 | onChangeRadio(isChecked) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 123 | let {onChange} = this.props; |
| 124 | this.setState({ |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 125 | checked: isChecked |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 126 | }); |
| 127 | onChange(this.state.value); |
| 128 | } |
| 129 | |
| 130 | focus() { |
| 131 | ReactDOM.findDOMNode(this.inputWrapper).focus(); |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | export default InputWrapper; |