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

130 lines
3.4 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,
};
2019-08-23 07:16:18 +00:00
}
clearSearch = () => {
this.setState({
inputValue: ''
});
typeof this.props.onClearSearch === 'function' && this.props.onClearSearch();
}
onInputChange = (e) => {
this.setState({
inputValue: e.target.value
});
if (this.props.autoRefresh)
2019-09-12 11:10:15 +00:00
this.setSearchTimer(e.target.value);
2019-07-15 14:24:17 +00:00
}
setSearchTimer = (value) => {
clearTimeout(this.timerId);
this.timerId = setTimeout(() =>
{
this.props.onChange(value);
clearTimeout(this.timerId);
this.timerId = null;
},
this.props.refreshTimeout
);
2019-09-12 11:10:15 +00:00
}
componentDidUpdate(prevProps) {
if (this.props.value != prevProps.value) {
this.setState({ inputValue: this.props.value });
2019-08-23 07:16:18 +00:00
return true;
}
}
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 ? 12 : 15;
2019-07-15 14:24:17 +00:00
break;
case 'middle':
clearButtonSize = !!this.state.inputValue || this.props.showClearButton ? 16 : 18;
break;
2019-07-15 14:24:17 +00:00
case 'big':
clearButtonSize = !!this.state.inputValue || this.props.showClearButton ? 19 : 21;
break;
2019-07-15 14:24:17 +00:00
case 'huge':
clearButtonSize = !!this.state.inputValue || this.props.showClearButton ? 22 : 24;
2019-07-15 14:24:17 +00:00
break;
}
return (
<StyledSearchInput className={this.props.className} style={this.props.style}>
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 ? "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,
2019-09-12 11:10:15 +00:00
name: PropTypes.string,
className: PropTypes.string,
2019-08-23 07:16:18 +00:00
size: PropTypes.oneOf(['base', 'middle', 'big', 'huge']),
value: PropTypes.string,
scale: PropTypes.bool,
placeholder: PropTypes.string,
onChange: PropTypes.func,
2019-09-12 11:10:15 +00:00
onClearSearch: PropTypes.func,
2019-08-23 07:16:18 +00:00
isDisabled: PropTypes.bool,
showClearButton: PropTypes.bool,
refreshTimeout: PropTypes.number,
autoRefresh: PropTypes.bool,
children: PropTypes.any,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
};
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;