DocSpace-client/web/ASC.Web.Components/src/components/search-input/index.js

131 lines
3.5 KiB
JavaScript
Raw Normal View History

import React from "react";
import PropTypes from "prop-types";
import styled from 'styled-components';
import InputBlock from '../input-block';
2019-08-23 07:16:18 +00:00
import isEqual from 'lodash/isEqual';
const StyledSearchInput = styled.div`
font-family: Open Sans;
font-style: normal;
`;
2019-08-23 07:16:18 +00:00
class SearchInput extends React.Component {
constructor(props) {
super(props);
this.input = React.createRef();
this.timerId = null;
this.state = {
2019-08-22 08:58:18 +00:00
inputValue: props.value,
};
this.clearSearch = this.clearSearch.bind(this);
this.onInputChange = this.onInputChange.bind(this);
this.setSearchTimer = this.setSearchTimer.bind(this);
2019-08-23 07:16:18 +00:00
}
clearSearch(){
2019-08-23 07:16:18 +00:00
if (this.input.current) this.input.current.clearInput();
this.setState({
inputValue: ''
});
if(typeof this.props.onClearSearch === 'function') this.props.onClearSearch();
};
2019-08-23 07:16:18 +00:00
onInputChange(e) {
this.setState({
inputValue: e.target.value
});
if (this.props.autoRefresh)
this.setSearchTimer(e.target.value);
2019-07-15 14:24:17 +00:00
}
setSearchTimer(value) {
this.timerId && clearTimeout(this.timerId);
this.timerId = null;
this.timerId = setTimeout(() => {
this.props.onChange(value);
clearTimeout(this.timerId);
this.timerId = null;
}, this.props.refreshTimeout);
}
2019-08-23 07:16:18 +00:00
shouldComponentUpdate(nextProps, nextState) {
if (this.props.value != nextProps.value) {
this.setState({ inputValue: nextProps.value });
2019-08-23 07:16:18 +00:00
return true;
}
return (!isEqual(this.state, nextState) || !isEqual(this.props, nextProps));
}
2019-07-15 14:24:17 +00:00
render() {
//console.log("Search input render");
let clearButtonSize = 15;
2019-07-15 14:24:17 +00:00
switch (this.props.size) {
case 'base':
clearButtonSize = !!this.state.inputValue || this.props.showClearButton > 0 ? 12 : 15;
2019-07-15 14:24:17 +00:00
break;
case 'middle':
clearButtonSize = !!this.state.inputValue || this.props.showClearButton > 0 ? 16 : 18;
break;
2019-07-15 14:24:17 +00:00
case 'big':
clearButtonSize = !!this.state.inputValue || this.props.showClearButton > 0 ? 19 : 21;
break;
2019-07-15 14:24:17 +00:00
case 'huge':
clearButtonSize = !!this.state.inputValue || this.props.showClearButton > 0 ? 22 : 24;
2019-07-15 14:24:17 +00:00
break;
default:
break;
}
return (
<StyledSearchInput className={this.props.className}>
2019-08-23 07:16:18 +00:00
<InputBlock
ref={this.input}
id={this.props.id}
name={this.props.name}
isDisabled={this.props.isDisabled}
iconName={!!this.state.inputValue || this.props.showClearButton > 0 ? "CrossIcon" : "SearchIcon"}
isIconFill={true}
iconSize={clearButtonSize}
iconColor={"#A3A9AE"}
onIconClick={!!this.state.inputValue || this.props.showClearButton ? this.clearSearch : undefined}
size={this.props.size}
scale={true}
value={this.state.inputValue}
placeholder={this.props.placeholder}
onChange={this.onInputChange}
>
{this.props.children}
2019-08-23 07:16:18 +00:00
</InputBlock>
</StyledSearchInput>
);
}
};
SearchInput.propTypes = {
2019-08-23 07:16:18 +00:00
id: PropTypes.string,
size: PropTypes.oneOf(['base', 'middle', 'big', 'huge']),
value: PropTypes.string,
scale: PropTypes.bool,
placeholder: PropTypes.string,
onChange: PropTypes.func,
isDisabled: PropTypes.bool,
showClearButton: PropTypes.bool,
refreshTimeout: PropTypes.number,
autoRefresh: PropTypes.bool
};
SearchInput.defaultProps = {
autoRefresh: true,
2019-08-23 07:16:18 +00:00
size: 'base',
value: '',
scale: false,
isDisabled: false,
refreshTimeout: 1000,
showClearButton: false
};
export default SearchInput;