2019-07-15 12:53:12 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import InputBlock from '../input-block';
|
2019-08-15 13:27:13 +00:00
|
|
|
import ComboBox from '../combobox';
|
2019-07-18 09:35:03 +00:00
|
|
|
|
|
|
|
import FilterButton from './filter-button';
|
|
|
|
import HideFilter from './hide-filter';
|
|
|
|
import CloseButton from './close-button';
|
2019-07-15 12:53:12 +00:00
|
|
|
|
2019-07-17 12:36:13 +00:00
|
|
|
const StyledSearchInput = styled.div`
|
|
|
|
min-width: 200px;
|
2019-07-19 09:48:19 +00:00
|
|
|
font-family: Open Sans;
|
|
|
|
font-style: normal;
|
2019-07-17 12:36:13 +00:00
|
|
|
`;
|
2019-07-19 09:48:19 +00:00
|
|
|
const StyledFilterBlock = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2019-08-08 09:45:00 +00:00
|
|
|
`;
|
2019-07-15 12:53:12 +00:00
|
|
|
const StyledFilterItem = styled.div`
|
2019-07-17 12:36:13 +00:00
|
|
|
display: ${props => props.block ? 'block' : 'inline-block'};
|
|
|
|
margin-bottom: ${props => props.block ? '3px' : '0'};
|
|
|
|
position relative;
|
2019-07-19 09:48:19 +00:00
|
|
|
|
2019-07-15 14:24:17 +00:00
|
|
|
height: 100%;
|
2019-07-19 09:48:19 +00:00
|
|
|
padding: 3px 44px 3px 7px;
|
2019-07-15 12:53:12 +00:00
|
|
|
margin-right: 2px;
|
2019-07-17 12:36:13 +00:00
|
|
|
border: 1px solid #ECEEF1;
|
2019-07-15 12:53:12 +00:00
|
|
|
border-radius: 3px;
|
2019-07-17 12:36:13 +00:00
|
|
|
background-color: #F8F9F9;
|
2019-07-19 09:48:19 +00:00
|
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
font-size: 13px;
|
2019-08-20 11:52:49 +00:00
|
|
|
line-height: 15px;
|
2019-07-19 09:48:19 +00:00
|
|
|
|
2019-07-17 12:36:13 +00:00
|
|
|
&:last-child{
|
|
|
|
margin-bottom: 0;
|
|
|
|
}
|
|
|
|
`;
|
2019-07-19 09:48:19 +00:00
|
|
|
const StyledCloseButtonBlock = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
position: absolute;
|
|
|
|
height: 100%;
|
|
|
|
width: 25px;
|
|
|
|
border-left: 1px solid #ECEEF1;
|
2019-07-17 12:36:13 +00:00
|
|
|
right: 0;
|
2019-07-19 09:48:19 +00:00
|
|
|
top: 0;
|
2019-07-15 12:53:12 +00:00
|
|
|
`;
|
2019-08-15 13:27:13 +00:00
|
|
|
const StyledComboBox = styled(ComboBox)`
|
|
|
|
display: inline-block;
|
|
|
|
background: transparent;
|
|
|
|
cursor: pointer;
|
2019-08-20 11:30:29 +00:00
|
|
|
vertical-align: middle;
|
2019-08-15 13:27:13 +00:00
|
|
|
margin-left: -10px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
class FilterItem extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state={
|
|
|
|
id: this.props.id
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onSelect = this.onSelect.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelect(option){
|
|
|
|
this.props.onSelectFilterItem(null, {
|
|
|
|
key: option.group + "_" + option.key,
|
|
|
|
label: option.label,
|
2019-08-19 13:35:39 +00:00
|
|
|
group: option.group,
|
|
|
|
inSubgroup: !!option.inSubgroup
|
2019-08-15 13:27:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
|
|
|
return(
|
|
|
|
<StyledFilterItem key={this.state.id} id={this.state.id} block={this.props.block} >
|
|
|
|
{this.props.groupLabel}:
|
|
|
|
<StyledComboBox
|
|
|
|
options={this.props.groupItems}
|
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
onSelect={this.onSelect}
|
|
|
|
selectedOption={{
|
2019-08-19 13:35:39 +00:00
|
|
|
key: this.state.id,
|
2019-08-20 11:30:29 +00:00
|
|
|
label: this.props.label
|
2019-08-15 13:27:13 +00:00
|
|
|
}}
|
|
|
|
size='content'
|
|
|
|
scaled={false}
|
|
|
|
noBorder={true}
|
2019-08-20 11:52:49 +00:00
|
|
|
opened={this.props.opened}
|
2019-08-15 13:27:13 +00:00
|
|
|
></StyledComboBox>
|
|
|
|
|
2019-07-19 09:48:19 +00:00
|
|
|
<StyledCloseButtonBlock>
|
2019-07-18 09:35:03 +00:00
|
|
|
<CloseButton
|
2019-08-15 13:27:13 +00:00
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
onClick={!this.props.isDisabled ? ((e) => this.props.onClose(e, this.props.id)) : undefined}
|
2019-07-15 12:53:12 +00:00
|
|
|
/>
|
2019-07-19 09:48:19 +00:00
|
|
|
</StyledCloseButtonBlock>
|
2019-08-15 13:27:13 +00:00
|
|
|
</StyledFilterItem>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 12:53:12 +00:00
|
|
|
|
2019-08-08 09:45:00 +00:00
|
|
|
const cloneProperty = function(props){
|
|
|
|
var newProps = [];
|
|
|
|
props.forEach(item=> {
|
|
|
|
let cloneItem = {};
|
|
|
|
for (var key in item) {
|
|
|
|
cloneItem[key] = item[key];
|
|
|
|
}
|
|
|
|
newProps.push(cloneItem);
|
|
|
|
});
|
|
|
|
return newProps;
|
|
|
|
}
|
|
|
|
|
2019-07-15 12:53:12 +00:00
|
|
|
class SearchInput extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2019-08-13 08:07:35 +00:00
|
|
|
this.input = React.createRef();
|
|
|
|
|
2019-08-08 09:45:00 +00:00
|
|
|
function getDefaultFilterData(){
|
|
|
|
let filterData = props.getFilterData();
|
|
|
|
let filterItems = [];
|
|
|
|
let selectedFilterData = cloneProperty(props.selectedFilterData);
|
|
|
|
selectedFilterData.forEach(defaultfilterValue => {
|
|
|
|
let filterValue = filterData.find(x => ((x.key === defaultfilterValue.key.replace(defaultfilterValue.group + "_",'')) && x.group === defaultfilterValue.group));
|
2019-08-19 13:35:39 +00:00
|
|
|
let groupLabel = '';
|
|
|
|
|
|
|
|
let groupFilterItem = filterData.find(x => (x.key === defaultfilterValue.group));
|
|
|
|
if(groupFilterItem != undefined){
|
|
|
|
groupLabel = groupFilterItem.label;
|
|
|
|
}else{
|
|
|
|
let subgroupFilterItem = filterData.find(x => (x.subgroup === defaultfilterValue.group))
|
|
|
|
if(subgroupFilterItem != undefined){
|
|
|
|
groupLabel = subgroupFilterItem.label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 09:45:00 +00:00
|
|
|
if(filterValue != undefined){
|
|
|
|
defaultfilterValue.key = defaultfilterValue.group + "_" + defaultfilterValue.key;
|
|
|
|
defaultfilterValue.label = filterValue.label;
|
2019-08-19 13:35:39 +00:00
|
|
|
defaultfilterValue.groupLabel = groupLabel;
|
2019-08-08 09:45:00 +00:00
|
|
|
filterItems.push(defaultfilterValue);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return filterItems;
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:36:13 +00:00
|
|
|
this.minWidth = 190;
|
|
|
|
this.isUpdateFilter = true;
|
2019-08-08 09:45:00 +00:00
|
|
|
this.isResizeUpdate = false;
|
|
|
|
this.isNewProps = false;
|
|
|
|
this.isUpdatedHiddenItems = false;
|
|
|
|
|
|
|
|
this.isNew = true;
|
|
|
|
|
2019-07-15 12:53:12 +00:00
|
|
|
this.state = {
|
2019-08-08 09:45:00 +00:00
|
|
|
filterItems: props.selectedFilterData ? getDefaultFilterData() : [],
|
2019-07-17 12:36:13 +00:00
|
|
|
openFilterItems: [],
|
|
|
|
hideFilterItems: []
|
2019-07-15 12:53:12 +00:00
|
|
|
};
|
2019-07-17 12:36:13 +00:00
|
|
|
this.searchWrapper = React.createRef();
|
|
|
|
this.filterWrapper = React.createRef();
|
|
|
|
|
2019-07-15 12:53:12 +00:00
|
|
|
this.onClickDropDownItem = this.onClickDropDownItem.bind(this);
|
|
|
|
this.getData = this.getData.bind(this);
|
2019-08-13 08:07:35 +00:00
|
|
|
this.clearFilter = this.clearFilter.bind(this);
|
2019-07-15 12:53:12 +00:00
|
|
|
this.onDeleteFilterItem = this.onDeleteFilterItem.bind(this);
|
2019-07-15 14:24:17 +00:00
|
|
|
this.getFilterItems = this.getFilterItems.bind(this);
|
2019-07-17 12:36:13 +00:00
|
|
|
this.updateFilter = this.updateFilter.bind(this);
|
2019-08-13 08:07:35 +00:00
|
|
|
this.onInputChange = this.onInputChange.bind(this);
|
|
|
|
|
2019-07-17 12:36:13 +00:00
|
|
|
this.resize = this.resize.bind(this);
|
2019-08-08 09:45:00 +00:00
|
|
|
}
|
2019-07-15 12:53:12 +00:00
|
|
|
|
|
|
|
onClickDropDownItem(event, filterItem){
|
2019-08-08 09:45:00 +00:00
|
|
|
let curentFilterItems = cloneProperty(this.state.filterItems);
|
|
|
|
|
2019-08-19 13:35:39 +00:00
|
|
|
if(filterItem.subgroup != undefined){
|
|
|
|
let indexFilterItem = curentFilterItems.findIndex(x => x.group === filterItem.subgroup);
|
|
|
|
if(indexFilterItem != -1){
|
|
|
|
curentFilterItems.splice(indexFilterItem, 1);
|
|
|
|
}
|
|
|
|
let selectFilterItem = {
|
|
|
|
key: filterItem.subgroup + "_-1",
|
|
|
|
group: filterItem.subgroup,
|
2019-08-20 11:30:29 +00:00
|
|
|
label: filterItem.defaultSelectLabel,
|
2019-08-19 13:35:39 +00:00
|
|
|
groupLabel: filterItem.label,
|
|
|
|
inSubgroup: true
|
|
|
|
};
|
|
|
|
curentFilterItems.push(selectFilterItem);
|
|
|
|
this.setState({ filterItems: curentFilterItems});
|
|
|
|
this.isUpdateFilter = false;
|
2019-08-08 09:45:00 +00:00
|
|
|
|
2019-08-19 13:35:39 +00:00
|
|
|
}else{
|
|
|
|
let filterItems = this.getData();
|
2019-07-15 12:53:12 +00:00
|
|
|
|
2019-08-19 13:35:39 +00:00
|
|
|
let indexFilterItem = curentFilterItems.findIndex(x => x.group === filterItem.group);
|
|
|
|
if(indexFilterItem != -1){
|
|
|
|
curentFilterItems.splice(indexFilterItem, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectFilterItem = {
|
|
|
|
key: filterItem.key,
|
|
|
|
group: filterItem.group,
|
|
|
|
label: filterItem.label,
|
|
|
|
groupLabel: filterItem.inSubgroup ? filterItems.find(x => x.subgroup === filterItem.group).label : filterItems.find(x => x.key === filterItem.group).label
|
|
|
|
};
|
|
|
|
curentFilterItems.push(selectFilterItem);
|
|
|
|
this.setState({ filterItems: curentFilterItems});
|
|
|
|
|
|
|
|
let clone = cloneProperty(curentFilterItems.filter(function(item) {
|
|
|
|
return item.key != '-1';
|
|
|
|
}));
|
|
|
|
clone.map(function(item){
|
|
|
|
item.key = item.key.replace(item.group + "_" ,'');
|
|
|
|
return item;
|
|
|
|
})
|
|
|
|
if(typeof this.props.onChangeFilter === "function")
|
|
|
|
this.props.onChangeFilter({
|
|
|
|
inputValue: this.state.inputValue,
|
|
|
|
filterValue: this.props.isNeedFilter ?
|
|
|
|
clone.filter(function(item) {
|
|
|
|
return item.key != '-1';
|
|
|
|
}) :
|
|
|
|
null
|
|
|
|
});
|
|
|
|
this.isUpdateFilter = false;
|
|
|
|
}
|
|
|
|
|
2019-07-15 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getData(){
|
|
|
|
let _this = this;
|
2019-07-17 12:36:13 +00:00
|
|
|
let d = this.props.getFilterData();
|
2019-08-19 13:35:39 +00:00
|
|
|
let result = [];
|
|
|
|
d.forEach(element => {
|
|
|
|
if(!element.inSubgroup){
|
|
|
|
element.onClick = !element.isSeparator && !element.isHeader && !element.disabled ? ((e) => _this.onClickDropDownItem(e, element)) : undefined;
|
|
|
|
element.key = element.group != element.key ? element.group +"_"+ element.key : element.key;
|
|
|
|
result.push(element);
|
|
|
|
}
|
2019-07-15 12:53:12 +00:00
|
|
|
});
|
2019-08-19 13:35:39 +00:00
|
|
|
return result;
|
2019-07-15 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 08:07:35 +00:00
|
|
|
clearFilter(){
|
|
|
|
if(this.input.current) this.input.current.clearInput();
|
|
|
|
this.setState({
|
|
|
|
inputValue: '',
|
|
|
|
filterItems: [],
|
|
|
|
openFilterItems: [],
|
|
|
|
hideFilterItems: []
|
|
|
|
});
|
|
|
|
this.props.onChangeFilter({
|
|
|
|
inputValue: '',
|
|
|
|
filterValue: []
|
|
|
|
});
|
2019-07-15 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onDeleteFilterItem(e , key){
|
|
|
|
|
|
|
|
let curentFilterItems = this.state.filterItems.slice();
|
|
|
|
let indexFilterItem = curentFilterItems.findIndex(x => x.key === key);
|
|
|
|
if(indexFilterItem != -1){
|
|
|
|
curentFilterItems.splice(indexFilterItem, 1);
|
|
|
|
}
|
|
|
|
this.setState({ filterItems: curentFilterItems});
|
2019-08-19 13:35:39 +00:00
|
|
|
let filterValues = cloneProperty(curentFilterItems);
|
|
|
|
filterValues = filterValues.map(function(item){
|
|
|
|
item.key = item.key.replace(item.group + "_",'');
|
|
|
|
return item;
|
|
|
|
})
|
2019-07-15 12:53:12 +00:00
|
|
|
if(typeof this.props.onChangeFilter === "function")
|
|
|
|
this.props.onChangeFilter({
|
2019-08-13 08:07:35 +00:00
|
|
|
inputValue: this.state.inputValue,
|
2019-08-08 09:45:00 +00:00
|
|
|
filterValue: this.props.isNeedFilter ?
|
2019-08-19 13:35:39 +00:00
|
|
|
filterValues.filter(function(item) {
|
|
|
|
return item.key != '-1';
|
2019-08-08 09:45:00 +00:00
|
|
|
}) : null
|
2019-07-15 12:53:12 +00:00
|
|
|
});
|
2019-07-17 12:36:13 +00:00
|
|
|
this.isUpdateFilter = false;
|
2019-07-15 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 14:24:17 +00:00
|
|
|
getFilterItems(){
|
|
|
|
let _this = this;
|
2019-07-17 12:36:13 +00:00
|
|
|
let result = [];
|
|
|
|
let openItems = [];
|
|
|
|
let hideItems = [];
|
|
|
|
if(this.state.filterItems.length > 0 && !this.isUpdateFilter){
|
|
|
|
openItems = this.state.filterItems.map(function(item) {
|
|
|
|
return <FilterItem
|
|
|
|
isDisabled={_this.props.isDisabled}
|
|
|
|
key={item.key}
|
2019-08-15 13:27:13 +00:00
|
|
|
groupItems={_this.props.getFilterData().filter(function(t) {
|
|
|
|
return (t.group == item.group && t.group != t.key);
|
|
|
|
})}
|
|
|
|
onSelectFilterItem={_this.onClickDropDownItem}
|
2019-08-08 09:45:00 +00:00
|
|
|
id={item.key}
|
2019-07-17 12:36:13 +00:00
|
|
|
groupLabel={item.groupLabel}
|
|
|
|
label={item.label}
|
2019-08-20 11:52:49 +00:00
|
|
|
opened={item.key.indexOf('_-1') == -1 ? false : true}
|
2019-07-17 12:36:13 +00:00
|
|
|
onClose={_this.onDeleteFilterItem}>
|
|
|
|
</FilterItem>
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(this.state.hideFilterItems.length > 0 && this.isUpdateFilter){
|
|
|
|
hideItems.push(
|
2019-08-08 09:45:00 +00:00
|
|
|
<HideFilter key="hide-filter" count={this.state.hideFilterItems.length} isDisabled={this.props.isDisabled}>
|
2019-07-17 12:36:13 +00:00
|
|
|
{
|
|
|
|
this.state.hideFilterItems.map(function(item) {
|
|
|
|
return <FilterItem
|
|
|
|
block={true}
|
|
|
|
isDisabled={_this.props.isDisabled}
|
|
|
|
key={item.key}
|
2019-08-15 13:27:13 +00:00
|
|
|
groupItems={_this.props.getFilterData().filter(function(t) {
|
|
|
|
return (t.group == item.group && t.group != t.key);
|
|
|
|
})}
|
|
|
|
onSelectFilterItem={_this.onClickDropDownItem}
|
2019-07-17 12:36:13 +00:00
|
|
|
id={item.key}
|
|
|
|
groupLabel={item.groupLabel}
|
2019-08-20 11:52:49 +00:00
|
|
|
opened={item.key.indexOf('_-1') == -1 ? false : true}
|
2019-07-17 12:36:13 +00:00
|
|
|
label={item.label}
|
|
|
|
onClose={_this.onDeleteFilterItem}>
|
|
|
|
</FilterItem>
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</HideFilter>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if(this.state.openFilterItems.length > 0 && this.isUpdateFilter){
|
|
|
|
openItems = this.state.openFilterItems.map(function(item) {
|
|
|
|
return <FilterItem
|
|
|
|
isDisabled={_this.props.isDisabled}
|
|
|
|
key={item.key}
|
2019-08-15 13:27:13 +00:00
|
|
|
groupItems={_this.props.getFilterData().filter(function(t) {
|
|
|
|
return (t.group == item.group && t.group != t.key);
|
|
|
|
})}
|
|
|
|
onSelectFilterItem={_this.onClickDropDownItem}
|
2019-07-17 12:36:13 +00:00
|
|
|
id={item.key}
|
|
|
|
groupLabel={item.groupLabel}
|
2019-08-20 11:52:49 +00:00
|
|
|
opened={item.key.indexOf('_-1') == -1 ? false : true}
|
2019-07-17 12:36:13 +00:00
|
|
|
label={item.label}
|
|
|
|
onClose={_this.onDeleteFilterItem}>
|
|
|
|
</FilterItem>
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
result = hideItems.concat(openItems);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFilter(){
|
|
|
|
let fullWidth = this.searchWrapper.current.getBoundingClientRect().width;
|
|
|
|
let filterWidth = this.filterWrapper.current.getBoundingClientRect().width;
|
|
|
|
if(fullWidth <= this.minWidth){
|
|
|
|
this.setState({ openFilterItems: []});
|
|
|
|
this.setState({ hideFilterItems: this.state.filterItems.slice()});
|
|
|
|
}else if(filterWidth > fullWidth/2){
|
|
|
|
let newOpenFilterItems = [];
|
|
|
|
let newHideFilterItems = [];
|
|
|
|
let openFilterWidth = 0;
|
|
|
|
|
|
|
|
let sortArr = Array.from(this.filterWrapper.current.children).sort(function(a,b) {
|
|
|
|
return a.getBoundingClientRect().width - b.getBoundingClientRect().width;
|
|
|
|
});
|
|
|
|
sortArr.forEach(element => {
|
|
|
|
openFilterWidth = openFilterWidth + element.getBoundingClientRect().width;
|
|
|
|
if(openFilterWidth < fullWidth/3){
|
2019-08-08 09:45:00 +00:00
|
|
|
newOpenFilterItems.push(this.state.filterItems.find(x => x.key === element.getAttribute('id')));
|
2019-07-17 12:36:13 +00:00
|
|
|
}else{
|
2019-08-08 09:45:00 +00:00
|
|
|
newHideFilterItems.push(this.state.filterItems.find(x => x.key === element.getAttribute('id')));
|
2019-07-17 12:36:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.setState({ openFilterItems: newOpenFilterItems});
|
|
|
|
this.setState({ hideFilterItems: newHideFilterItems});
|
|
|
|
}else{
|
|
|
|
this.setState({ openFilterItems: this.state.filterItems.slice()});
|
|
|
|
this.setState({ hideFilterItems: []});
|
2019-08-08 09:45:00 +00:00
|
|
|
|
2019-07-17 12:36:13 +00:00
|
|
|
}
|
2019-08-08 09:45:00 +00:00
|
|
|
this.isUpdatedHiddenItems = true;
|
2019-07-17 12:36:13 +00:00
|
|
|
this.isUpdateFilter = true;
|
|
|
|
}
|
2019-08-08 09:45:00 +00:00
|
|
|
|
2019-08-13 08:07:35 +00:00
|
|
|
onInputChange(e){
|
|
|
|
this.setState({
|
|
|
|
inputValue: e.target.value
|
|
|
|
});
|
|
|
|
this.props.onChange(e)
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:36:13 +00:00
|
|
|
resize(){
|
2019-08-08 09:45:00 +00:00
|
|
|
this.isResizeUpdate = true;
|
2019-07-17 12:36:13 +00:00
|
|
|
this.isUpdateFilter = false;
|
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
componentDidUpdate(){
|
2019-08-08 09:45:00 +00:00
|
|
|
if(!this.isUpdateFilter){
|
|
|
|
this.updateFilter();
|
|
|
|
}else if(this.isNewProps && this.props.isNeedFilter){
|
|
|
|
let filterData = this.props.getFilterData();
|
|
|
|
let filterItems = [];
|
|
|
|
|
|
|
|
let curentFilterItems = cloneProperty(this.props.selectedFilterData);
|
|
|
|
|
|
|
|
for(let i=0; i < curentFilterItems.length; i++){
|
2019-08-19 13:35:39 +00:00
|
|
|
let filterValue = filterData.find(x => ((x.key === curentFilterItems[i].key.replace(curentFilterItems[i].group + "_",'')) && x.group === curentFilterItems[i].group && !x.inSubgroup));
|
|
|
|
if(filterValue){
|
|
|
|
curentFilterItems[i].key = curentFilterItems[i].group + "_" + curentFilterItems[i].key;
|
|
|
|
curentFilterItems[i].label = filterValue.label;
|
|
|
|
curentFilterItems[i].groupLabel = !filterData.inSubgroup ? filterData.find(x => (x.group === curentFilterItems[i].group)).label : curentFilterItems[i].groupLabel;
|
|
|
|
filterItems.push(curentFilterItems[i]);
|
|
|
|
}else{
|
|
|
|
filterValue = filterData.find(x => ((x.key === curentFilterItems[i].key.replace(curentFilterItems[i].group + "_",'')) && x.group === curentFilterItems[i].group && x.inSubgroup));
|
2019-08-20 11:30:29 +00:00
|
|
|
if(filterValue){
|
|
|
|
curentFilterItems[i].key = curentFilterItems[i].group + "_" + curentFilterItems[i].key;
|
|
|
|
curentFilterItems[i].label = filterValue.label;
|
|
|
|
curentFilterItems[i].groupLabel = filterData.find(x => (x.subgroup === curentFilterItems[i].group)).label;
|
|
|
|
filterItems.push(curentFilterItems[i]);
|
|
|
|
}else{
|
|
|
|
filterValue = filterData.find(x => ((x.subgroup === curentFilterItems[i].group)));
|
|
|
|
if(filterValue){
|
|
|
|
curentFilterItems[i].key = curentFilterItems[i].group + "_-1";
|
|
|
|
curentFilterItems[i].label = filterValue.defaultSelectLabel;
|
|
|
|
curentFilterItems[i].groupLabel = filterData.find(x => (x.subgroup === curentFilterItems[i].group)).label;
|
|
|
|
filterItems.push(curentFilterItems[i]);
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 09:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.isNewProps= false;
|
|
|
|
this.isUpdateFilter = false;
|
|
|
|
this.setState({ filterItems: filterItems});
|
|
|
|
}
|
2019-07-17 12:36:13 +00:00
|
|
|
}
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('resize', _.throttle(this.resize), 100);
|
2019-08-05 12:23:07 +00:00
|
|
|
this.isUpdateFilter = false; this.updateFilter();
|
2019-07-17 12:36:13 +00:00
|
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.resize());
|
2019-07-15 14:24:17 +00:00
|
|
|
}
|
2019-08-08 09:45:00 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState){
|
|
|
|
if(!this.isNew){
|
|
|
|
let isNeedUpdate = false;
|
|
|
|
|
|
|
|
if(nextProps.selectedFilterData && this.props.selectedFilterData && this.props.isNeedFilter){
|
|
|
|
if(this.props.selectedFilterData.length != nextProps.selectedFilterData.length){
|
|
|
|
this.isNewProps = true;
|
|
|
|
isNeedUpdate = true;
|
|
|
|
}else{
|
|
|
|
let newFilterItems = nextProps.selectedFilterData;
|
|
|
|
let oldFilterItems = this.props.selectedFilterData;
|
|
|
|
|
|
|
|
for(let i = 0; i < newFilterItems.length; i++){
|
|
|
|
if(oldFilterItems.find(x => (x.key === newFilterItems[i].key && x.group === newFilterItems[i].group)) == undefined){
|
|
|
|
this.isNewProps = true;
|
|
|
|
isNeedUpdate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 13:35:39 +00:00
|
|
|
for (let propsKey in this.props) {
|
|
|
|
if(typeof this.props[propsKey] != "function" && typeof this.props[propsKey] != "object" && this.props[propsKey] != nextProps[propsKey]){
|
|
|
|
if(propsKey=='value') this.setState({inputValue: nextProps[propsKey]})
|
|
|
|
isNeedUpdate = true;
|
|
|
|
break;
|
2019-08-08 09:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-19 13:35:39 +00:00
|
|
|
|
2019-08-08 09:45:00 +00:00
|
|
|
if(!isNeedUpdate){
|
|
|
|
for (let key in this.state) {
|
|
|
|
if(this.state[key].length != nextState[key].length){
|
|
|
|
isNeedUpdate = true;
|
|
|
|
}else{
|
|
|
|
for(let i = 0; i < this.state[key].length; i++){
|
2019-08-13 08:07:35 +00:00
|
|
|
if(typeof nextState[key] == "object" && nextState[key].find(x => (x.key === this.state[key][i].key && x.group === this.state[key][i].group)) == undefined){
|
2019-08-08 09:45:00 +00:00
|
|
|
isNeedUpdate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!isNeedUpdate && !this.isResizeUpdate){
|
|
|
|
if(nextProps.value == this.props.value){
|
|
|
|
if(!this.isUpdatedHiddenItems){
|
|
|
|
this.isUpdatedHiddenItems = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else if(this.isResizeUpdate){
|
|
|
|
this.isResizeUpdate = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.isNew = false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-15 14:24:17 +00:00
|
|
|
|
2019-07-15 12:53:12 +00:00
|
|
|
render() {
|
2019-08-08 09:45:00 +00:00
|
|
|
//console.log("Search input render");
|
2019-07-15 12:53:12 +00:00
|
|
|
let _this = this;
|
2019-07-15 14:24:17 +00:00
|
|
|
let iconSize = 32;
|
2019-08-13 08:07:35 +00:00
|
|
|
let clearButtonSize = 15;
|
2019-07-15 14:24:17 +00:00
|
|
|
switch (this.props.size) {
|
|
|
|
case 'base':
|
2019-08-13 08:07:35 +00:00
|
|
|
iconSize = 32;
|
|
|
|
clearButtonSize = !!this.state.inputValue || this.state.filterItems.length > 0 ? 12 : 15;
|
2019-07-15 14:24:17 +00:00
|
|
|
break;
|
|
|
|
case 'middle':
|
2019-08-13 08:07:35 +00:00
|
|
|
iconSize = 41;
|
|
|
|
clearButtonSize = !!this.state.inputValue || this.state.filterItems.length > 0 ? 16 : 18;
|
|
|
|
break;
|
2019-07-15 14:24:17 +00:00
|
|
|
case 'big':
|
2019-08-13 08:07:35 +00:00
|
|
|
iconSize = 41;
|
|
|
|
clearButtonSize = !!this.state.inputValue || this.state.filterItems.length > 0 ? 19 : 21;
|
|
|
|
break;
|
2019-07-15 14:24:17 +00:00
|
|
|
case 'huge':
|
2019-08-13 08:07:35 +00:00
|
|
|
iconSize = 41;
|
|
|
|
clearButtonSize = !!this.state.inputValue || this.state.filterItems.length > 0 ? 22 : 24;
|
2019-07-15 14:24:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-07-15 12:53:12 +00:00
|
|
|
return (
|
2019-07-17 12:36:13 +00:00
|
|
|
<StyledSearchInput ref={this.searchWrapper}>
|
|
|
|
<InputBlock
|
2019-08-13 08:07:35 +00:00
|
|
|
ref={this.input}
|
2019-07-15 12:53:12 +00:00
|
|
|
id={this.props.id}
|
|
|
|
isDisabled={this.props.isDisabled}
|
2019-08-13 08:07:35 +00:00
|
|
|
iconName={!!this.state.inputValue || this.state.filterItems.length > 0 ? "CrossIcon" : "SearchIcon"}
|
2019-07-15 12:53:12 +00:00
|
|
|
isIconFill={true}
|
2019-08-13 08:07:35 +00:00
|
|
|
iconSize={clearButtonSize}
|
2019-07-15 12:53:12 +00:00
|
|
|
iconColor={"#A3A9AE"}
|
2019-08-13 08:07:35 +00:00
|
|
|
onIconClick={!!this.state.inputValue || this.state.filterItems.length > 0 ? this.clearFilter : undefined }
|
2019-07-15 12:53:12 +00:00
|
|
|
size={this.props.size}
|
|
|
|
scale={true}
|
2019-08-13 08:07:35 +00:00
|
|
|
value={this.state.inputValue}
|
2019-07-15 12:53:12 +00:00
|
|
|
placeholder={this.props.placeholder}
|
2019-08-13 08:07:35 +00:00
|
|
|
onChange={this.onInputChange}
|
2019-07-17 12:36:13 +00:00
|
|
|
>
|
|
|
|
{ this.props.isNeedFilter &&
|
2019-07-19 09:48:19 +00:00
|
|
|
<StyledFilterBlock ref={this.filterWrapper}>
|
2019-07-17 12:36:13 +00:00
|
|
|
{this.getFilterItems()}
|
2019-07-19 09:48:19 +00:00
|
|
|
</StyledFilterBlock>
|
2019-07-17 12:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ this.props.isNeedFilter &&
|
2019-07-18 09:35:03 +00:00
|
|
|
<FilterButton iconSize={iconSize} getData={_this.getData} isDisabled={this.props.isDisabled}/>
|
2019-07-17 12:36:13 +00:00
|
|
|
}
|
|
|
|
</InputBlock>
|
|
|
|
</StyledSearchInput>
|
2019-07-15 12:53:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SearchInput.propTypes = {
|
|
|
|
id: PropTypes.string,
|
|
|
|
size: PropTypes.oneOf(['base', 'middle', 'big', 'huge']),
|
|
|
|
value:PropTypes.string,
|
|
|
|
scale: PropTypes.bool,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
getFilterData:PropTypes.func,
|
|
|
|
isNeedFilter: PropTypes.bool,
|
2019-08-08 09:45:00 +00:00
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
selectedFilterData: PropTypes.array
|
2019-07-15 12:53:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SearchInput.defaultProps = {
|
|
|
|
size: 'base',
|
|
|
|
value: '',
|
|
|
|
scale: false,
|
|
|
|
isNeedFilter: false,
|
2019-08-08 09:45:00 +00:00
|
|
|
isDisabled: false,
|
|
|
|
selectedFilterData: []
|
2019-07-15 12:53:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default SearchInput;
|