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 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import React from 'react'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 17 | import PropTypes from 'prop-types'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 18 | import ReactDOM from 'react-dom'; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 19 | import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 20 | import Input from 'nfvo-components/input/validation/InputWrapper.jsx'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 22 | const ExpandableInputClosed = ({iconType, onClick}) => ( |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 23 | <SVGIcon className='expandable-input-wrapper closed' data-test-id='expandable-input-closed' name={iconType} onClick={onClick} /> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 24 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 25 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 26 | class ExpandableInputOpened extends React.Component { |
| 27 | componentDidMount(){ |
| 28 | this.rawDomNode = ReactDOM.findDOMNode(this.searchInputNode.inputWrapper); |
| 29 | this.rawDomNode.focus(); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 30 | } |
| 31 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 32 | componentWillReceiveProps(newProps){ |
| 33 | if (!newProps.value){ |
| 34 | if (!(document.activeElement === this.rawDomNode)){ |
| 35 | this.props.handleBlur(); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | handleClose(){ |
| 41 | this.props.onChange(''); |
| 42 | this.rawDomNode.focus(); |
| 43 | } |
| 44 | |
| 45 | handleKeyDown(e){ |
| 46 | if (e.key === 'Escape'){ |
| 47 | e.preventDefault(); |
| 48 | if (this.props.value) { |
| 49 | this.handleClose(); |
| 50 | } else { |
| 51 | this.rawDomNode.blur(); |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | render() { |
| 57 | let {iconType, value, onChange, handleBlur} = this.props; |
| 58 | return ( |
| 59 | <div className='expandable-input-wrapper opened' key='expandable'> |
| 60 | <Input |
| 61 | type='text' |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 62 | data-test-id='expandable-input-opened' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | value={value} |
| 64 | ref={(input) => this.searchInputNode = input} |
| 65 | className='expandable-active' |
| 66 | groupClassName='expandable-input-control' |
| 67 | onChange={e => onChange(e)} |
| 68 | onKeyDown={e => this.handleKeyDown(e)} |
| 69 | onBlur={handleBlur}/> |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 70 | {value && <SVGIcon data-test-id='expandable-input-close-btn' onClick={() => this.handleClose()} name='close' />} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 71 | {!value && <SVGIcon name={iconType} onClick={handleBlur}/>} |
| 72 | </div> |
| 73 | ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | class ExpandableInput extends React.Component { |
| 78 | |
| 79 | static propTypes = { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 80 | iconType: PropTypes.string, |
| 81 | onChange: PropTypes.func, |
| 82 | value: PropTypes.string |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | state = {showInput: false}; |
| 86 | |
| 87 | closeInput(){ |
| 88 | if (!this.props.value) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 89 | this.setState({showInput: false}); |
| 90 | } |
| 91 | } |
| 92 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 93 | getValue(){ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 94 | return this.props.value; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | render(){ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 98 | let {iconType, value, onChange = false} = this.props; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 99 | return ( |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 100 | <div className='expandable-input-top'> |
| 101 | {this.state.showInput && |
| 102 | <ExpandableInputOpened |
| 103 | key='open' |
| 104 | iconType={iconType} |
| 105 | onChange={onChange} |
| 106 | value={value} |
| 107 | handleKeyDown={(e) => this.handleKeyDown(e)} |
| 108 | handleBlur={() => this.closeInput()}/> |
| 109 | } |
| 110 | {!this.state.showInput && <ExpandableInputClosed key='closed' iconType={iconType} onClick={() => this.setState({showInput: true})} />} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 111 | </div> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 112 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 116 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 117 | export default ExpandableInput; |