blob: e2ee40fcd2793b5ce214bf8e6b9f24ec1553eebe [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';
AviZi280f8012017-06-09 02:39:56 +030017import ReactDOM from 'react-dom';
18import SVGIcon from 'nfvo-components/icon/SVGIcon.jsx';
19import Input from 'nfvo-components/input/validation/InputWrapper.jsx';
Michael Landoefa037d2017-02-19 12:57:33 +020020
AviZi280f8012017-06-09 02:39:56 +030021const ExpandableInputClosed = ({iconType, onClick}) => (
22 <SVGIcon className='expandable-input-wrapper closed' name={iconType} onClick={onClick} />
23);
Michael Landoefa037d2017-02-19 12:57:33 +020024
AviZi280f8012017-06-09 02:39:56 +030025class ExpandableInputOpened extends React.Component {
26 componentDidMount(){
27 this.rawDomNode = ReactDOM.findDOMNode(this.searchInputNode.inputWrapper);
28 this.rawDomNode.focus();
Michael Landoefa037d2017-02-19 12:57:33 +020029 }
30
AviZi280f8012017-06-09 02:39:56 +030031 componentWillReceiveProps(newProps){
32 if (!newProps.value){
33 if (!(document.activeElement === this.rawDomNode)){
34 this.props.handleBlur();
35 }
36 }
37 }
38
39 handleClose(){
40 this.props.onChange('');
41 this.rawDomNode.focus();
42 }
43
44 handleKeyDown(e){
45 if (e.key === 'Escape'){
46 e.preventDefault();
47 if (this.props.value) {
48 this.handleClose();
49 } else {
50 this.rawDomNode.blur();
51 }
52 };
53 }
54
55 render() {
56 let {iconType, value, onChange, handleBlur} = this.props;
57 return (
58 <div className='expandable-input-wrapper opened' key='expandable'>
59 <Input
60 type='text'
61 value={value}
62 ref={(input) => this.searchInputNode = input}
63 className='expandable-active'
64 groupClassName='expandable-input-control'
65 onChange={e => onChange(e)}
66 onKeyDown={e => this.handleKeyDown(e)}
67 onBlur={handleBlur}/>
68 {value && <SVGIcon onClick={() => this.handleClose()} name='close' />}
69 {!value && <SVGIcon name={iconType} onClick={handleBlur}/>}
70 </div>
71 );
72 }
73}
74
75class ExpandableInput extends React.Component {
76
77 static propTypes = {
78 iconType: React.PropTypes.string,
79 onChange: React.PropTypes.func,
80 value: React.PropTypes.string
81 };
82
83 state = {showInput: false};
84
85 closeInput(){
86 if (!this.props.value) {
Michael Landoefa037d2017-02-19 12:57:33 +020087 this.setState({showInput: false});
88 }
89 }
90
Michael Landoefa037d2017-02-19 12:57:33 +020091 getValue(){
AviZi280f8012017-06-09 02:39:56 +030092 return this.props.value;
Michael Landoefa037d2017-02-19 12:57:33 +020093 }
94
95 render(){
AviZi280f8012017-06-09 02:39:56 +030096 let {iconType, value, onChange = false} = this.props;
Michael Landoefa037d2017-02-19 12:57:33 +020097 return (
AviZi280f8012017-06-09 02:39:56 +030098 <div className='expandable-input-top'>
99 {this.state.showInput &&
100 <ExpandableInputOpened
101 key='open'
102 iconType={iconType}
103 onChange={onChange}
104 value={value}
105 handleKeyDown={(e) => this.handleKeyDown(e)}
106 handleBlur={() => this.closeInput()}/>
107 }
108 {!this.state.showInput && <ExpandableInputClosed key='closed' iconType={iconType} onClick={() => this.setState({showInput: true})} />}
Michael Landoefa037d2017-02-19 12:57:33 +0200109 </div>
AviZi280f8012017-06-09 02:39:56 +0300110 );
Michael Landoefa037d2017-02-19 12:57:33 +0200111 }
112}
113
AviZi280f8012017-06-09 02:39:56 +0300114
Michael Landoefa037d2017-02-19 12:57:33 +0200115export default ExpandableInput;