blob: 82fbe1deed7ca7dfcc4d57be414eb94ad84f2080 [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 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import React from 'react';
talig8e9c0652017-12-20 14:30:43 +020017import PropTypes from 'prop-types';
AviZi280f8012017-06-09 02:39:56 +030018import ReactDOM from 'react-dom';
Avi Zivb8e2faf2017-07-18 19:45:38 +030019import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';
AviZi280f8012017-06-09 02:39:56 +030020import Input from 'nfvo-components/input/validation/InputWrapper.jsx';
Michael Landoefa037d2017-02-19 12:57:33 +020021
AviZi280f8012017-06-09 02:39:56 +030022const ExpandableInputClosed = ({iconType, onClick}) => (
Avi Zivb8e2faf2017-07-18 19:45:38 +030023 <SVGIcon className='expandable-input-wrapper closed' data-test-id='expandable-input-closed' name={iconType} onClick={onClick} />
AviZi280f8012017-06-09 02:39:56 +030024);
Michael Landoefa037d2017-02-19 12:57:33 +020025
AviZi280f8012017-06-09 02:39:56 +030026class ExpandableInputOpened extends React.Component {
27 componentDidMount(){
28 this.rawDomNode = ReactDOM.findDOMNode(this.searchInputNode.inputWrapper);
29 this.rawDomNode.focus();
Michael Landoefa037d2017-02-19 12:57:33 +020030 }
31
AviZi280f8012017-06-09 02:39:56 +030032 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 Zivb8e2faf2017-07-18 19:45:38 +030062 data-test-id='expandable-input-opened'
AviZi280f8012017-06-09 02:39:56 +030063 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 Zivb8e2faf2017-07-18 19:45:38 +030070 {value && <SVGIcon data-test-id='expandable-input-close-btn' onClick={() => this.handleClose()} name='close' />}
AviZi280f8012017-06-09 02:39:56 +030071 {!value && <SVGIcon name={iconType} onClick={handleBlur}/>}
72 </div>
73 );
74 }
75}
76
77class ExpandableInput extends React.Component {
78
79 static propTypes = {
talig8e9c0652017-12-20 14:30:43 +020080 iconType: PropTypes.string,
81 onChange: PropTypes.func,
82 value: PropTypes.string
AviZi280f8012017-06-09 02:39:56 +030083 };
84
85 state = {showInput: false};
86
87 closeInput(){
88 if (!this.props.value) {
Michael Landoefa037d2017-02-19 12:57:33 +020089 this.setState({showInput: false});
90 }
91 }
92
Michael Landoefa037d2017-02-19 12:57:33 +020093 getValue(){
AviZi280f8012017-06-09 02:39:56 +030094 return this.props.value;
Michael Landoefa037d2017-02-19 12:57:33 +020095 }
96
97 render(){
AviZi280f8012017-06-09 02:39:56 +030098 let {iconType, value, onChange = false} = this.props;
Michael Landoefa037d2017-02-19 12:57:33 +020099 return (
AviZi280f8012017-06-09 02:39:56 +0300100 <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 Landoefa037d2017-02-19 12:57:33 +0200111 </div>
AviZi280f8012017-06-09 02:39:56 +0300112 );
Michael Landoefa037d2017-02-19 12:57:33 +0200113 }
114}
115
AviZi280f8012017-06-09 02:39:56 +0300116
Michael Landoefa037d2017-02-19 12:57:33 +0200117export default ExpandableInput;