2023-03-06 15:06:56 +08:00
|
|
|
/*
|
|
|
|
|
* Author: 黎永顺
|
|
|
|
|
* name: 设置项目
|
|
|
|
|
* Description:
|
|
|
|
|
* Date: 2023/3/6
|
|
|
|
|
*/
|
|
|
|
|
import React, { Component } from "react";
|
|
|
|
|
import { WeaCheckbox, WeaSearchGroup } from "ecCom";
|
2022-03-14 17:07:26 +08:00
|
|
|
|
2023-03-06 15:06:56 +08:00
|
|
|
class SelectItemsWrapper extends Component {
|
2023-03-07 14:47:47 +08:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2024-03-15 09:19:59 +08:00
|
|
|
searchVal: "", selectItem: [], groupItem: [], showOnlyChecked: false
|
2023-03-07 14:47:47 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 15:03:08 +08:00
|
|
|
componentDidMount() {
|
2024-03-15 09:19:59 +08:00
|
|
|
this.initSelectItem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initSelectItem = () => {
|
|
|
|
|
const { dataSource } = this.props;
|
2023-03-08 15:03:08 +08:00
|
|
|
this.setState({
|
|
|
|
|
selectItem: _.map(_.filter(_.reduce(dataSource, (pre, cur) => {
|
|
|
|
|
return [...pre, ...cur.items];
|
|
|
|
|
}, []), item => !!item.checked), it => it.id)
|
|
|
|
|
});
|
2024-03-15 09:19:59 +08:00
|
|
|
};
|
2023-03-08 15:03:08 +08:00
|
|
|
|
2023-03-08 13:54:27 +08:00
|
|
|
handleSearchItemSet = (searchVal) => this.setState({ searchVal });
|
|
|
|
|
handleShowOnlyChecked = (showOnlyChecked) => this.setState({ showOnlyChecked: !!Number(showOnlyChecked) });
|
2023-03-07 14:47:47 +08:00
|
|
|
handleSelectGroupAll = (groupId, checked) => {
|
2023-03-08 13:54:27 +08:00
|
|
|
const { selectItem, groupItem } = this.state;
|
|
|
|
|
const { dataSource } = this.props;
|
|
|
|
|
_.map(dataSource, item => {
|
|
|
|
|
if (item.groupId === groupId) {
|
|
|
|
|
if (!!Number(checked)) {
|
|
|
|
|
this.setState({
|
|
|
|
|
groupItem: [...groupItem, ..._.map(item.items, child => child.id)]
|
|
|
|
|
}, () => this.setState({ selectItem: [...selectItem, ...this.state.groupItem] }));
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
groupItem: _.differenceWith(groupItem, _.map(item.items, child => child.id), _.isEqual)
|
|
|
|
|
}, () => this.setState({ selectItem: this.state.groupItem }));
|
2023-03-07 14:47:47 +08:00
|
|
|
}
|
2023-03-08 13:54:27 +08:00
|
|
|
}
|
2023-03-07 14:47:47 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
handleSelectItem = (id, checked) => {
|
2023-03-08 13:54:27 +08:00
|
|
|
const { selectItem } = this.state;
|
2023-03-07 14:47:47 +08:00
|
|
|
this.setState({
|
2023-03-08 13:54:27 +08:00
|
|
|
selectItem: !!Number(checked) ? [...selectItem, id] : _.xorWith(selectItem, [id], _.isEqual)
|
2023-03-07 14:47:47 +08:00
|
|
|
});
|
|
|
|
|
};
|
2023-03-06 15:06:56 +08:00
|
|
|
renderTitle = (item) => {
|
2023-03-06 17:13:08 +08:00
|
|
|
const { onSelectGroupAll } = this.props;
|
2023-03-08 13:54:27 +08:00
|
|
|
const { groupName, groupId, items } = item;
|
|
|
|
|
const number = _.filter(items, it => !!it.checked).length;
|
2023-03-08 15:03:08 +08:00
|
|
|
const value = _.every(items, it => !!it.checked) ? "1" : "0";
|
2023-03-06 15:06:56 +08:00
|
|
|
return <div className="setGroupWrapper">
|
2023-03-08 15:03:08 +08:00
|
|
|
<WeaCheckbox content={groupName} value={value} onChange={(val) => onSelectGroupAll(groupId, val)}/>
|
2023-03-08 13:54:27 +08:00
|
|
|
<span className="checkedtitle">已选择{number}个字段</span>
|
2023-03-06 15:06:56 +08:00
|
|
|
</div>;
|
|
|
|
|
};
|
2022-03-23 13:11:01 +08:00
|
|
|
|
2022-09-13 16:39:15 +08:00
|
|
|
render() {
|
2023-03-08 13:54:27 +08:00
|
|
|
const { searchVal, selectItem, showOnlyChecked } = this.state;
|
|
|
|
|
const { onSelectItem, dataSource } = this.props;
|
|
|
|
|
let setItemList = _.map(dataSource, item => {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
items: _.filter(item.items || [], child => child.name.indexOf(searchVal) !== -1)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
setItemList = _.map(setItemList, item => {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
items: _.map(item.items, child => {
|
|
|
|
|
return { ...child, checked: selectItem.includes(child.id) };
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
if (showOnlyChecked) {
|
|
|
|
|
setItemList = _.map(setItemList, item => {
|
|
|
|
|
return { ...item, items: _.filter(item.items, it => !!it.checked) };
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-09-13 16:39:15 +08:00
|
|
|
return (
|
2023-03-06 15:06:56 +08:00
|
|
|
<React.Fragment>
|
2022-09-13 16:39:15 +08:00
|
|
|
{
|
2023-03-08 13:54:27 +08:00
|
|
|
_.map(setItemList, item => {
|
2023-03-06 15:06:56 +08:00
|
|
|
const { items } = item;
|
|
|
|
|
return <WeaSearchGroup title={this.renderTitle(item)} showGroup>
|
|
|
|
|
<div className="itemsWrapper">
|
|
|
|
|
{
|
|
|
|
|
_.isEmpty(items) ?
|
|
|
|
|
<span className="empty">暂无数据</span> :
|
|
|
|
|
<ul className="itemContUl">
|
|
|
|
|
{
|
|
|
|
|
_.map(items, child => {
|
2023-03-06 17:13:08 +08:00
|
|
|
const { name, checked, id } = child;
|
2023-03-06 15:06:56 +08:00
|
|
|
return <li title={name}>
|
|
|
|
|
<WeaCheckbox
|
|
|
|
|
content={name} value={checked ? "1" : "0"}
|
2023-03-06 17:13:08 +08:00
|
|
|
onChange={(val) => onSelectItem(id, val)}
|
2023-03-06 15:06:56 +08:00
|
|
|
/>
|
|
|
|
|
</li>;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</ul>
|
|
|
|
|
}
|
|
|
|
|
</div>
|
|
|
|
|
</WeaSearchGroup>;
|
|
|
|
|
})
|
2022-09-13 16:39:15 +08:00
|
|
|
}
|
2023-03-06 15:06:56 +08:00
|
|
|
</React.Fragment>
|
2022-09-13 16:39:15 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 15:06:56 +08:00
|
|
|
|
|
|
|
|
export default SelectItemsWrapper;
|