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

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