Commit 7b1a217c authored by Maofei94's avatar Maofei94

Merge branch 'master' of g.civnet.cn:test/maintenance

parents a1fd5035 fb53a2a2
...@@ -5,7 +5,14 @@ import styles from './itemCard.less'; ...@@ -5,7 +5,14 @@ import styles from './itemCard.less';
import { checkChildrenByCondition, checkIsGroup, getId } from '.'; import { checkChildrenByCondition, checkIsGroup, getId } from '.';
const ListCardItem = props => { const ListCardItem = props => {
const { searchWord, item, valueList, updateValueList, ouid } = props; const {
searchWord,
item,
valueList,
updateValueList,
ouid,
changedItem,
} = props;
// id // id
const id = getId(item); const id = getId(item);
...@@ -18,21 +25,50 @@ const ListCardItem = props => { ...@@ -18,21 +25,50 @@ const ListCardItem = props => {
const [childrenKeys, setChildrenKeys] = useState([]); const [childrenKeys, setChildrenKeys] = useState([]);
useEffect(() => { useEffect(() => {
if (isGroup) { if (isGroup) {
const keys = checkChildrenByCondition(item, c => getId(c), 'map').flat(); const keys = item.children
.map(child => checkChildrenByCondition(child, c => getId(c), 'map'))
.flat(Infinity);
setChildrenKeys(keys); setChildrenKeys(keys);
} else { } else {
// //
} }
}, [item]); }, [item]);
useEffect(() => {
// 子节点勾选状态变化时
if (
isGroup &&
changedItem.item !== item &&
!checkIsGroup(changedItem.item) &&
childrenKeys.includes(getId(changedItem.item))
) {
if (changedItem.value && childrenKeys.every(c => valueList.includes(c))) {
// 全选
updateValueList([id], [id]);
setIndeterminate(false);
} else if (childrenKeys.some(c => valueList.includes(c))) {
// 半选
// eslint-disable-next-line no-unused-expressions
valueList.includes(id) && updateValueList([], [id]);
setIndeterminate(true);
} else {
// 零选
// eslint-disable-next-line no-unused-expressions
valueList.includes(id) && updateValueList([], [id]);
setIndeterminate(false);
}
}
}, [changedItem.item, changedItem.value]);
// 勾选事件处理 // 勾选事件处理
const handleChecked = e => { const handleChecked = e => {
const { checked: v } = e.target; const { checked: v } = e.target;
if (isGroup) { if (isGroup) {
const result = [...childrenKeys, id]; const result = [...childrenKeys, id];
updateValueList(v ? result : [], result); updateValueList(v ? result : [], result, { item, value: v });
setIndeterminate(false);
} else { } else {
updateValueList(v ? [id] : [], [id]); updateValueList(v ? [id] : [], [id], { item, value: v });
} }
}; };
...@@ -41,11 +77,11 @@ const ListCardItem = props => { ...@@ -41,11 +77,11 @@ const ListCardItem = props => {
item.children.map((c, i) => ( item.children.map((c, i) => (
<ListCardItem <ListCardItem
item={c} item={c}
itemid={i}
key={`item${i}key`} key={`item${i}key`}
updateValueList={updateValueList} updateValueList={updateValueList}
valueList={valueList} valueList={valueList}
ouid={ouid} ouid={ouid}
changedItem={changedItem}
searchWord={searchWord} searchWord={searchWord}
/> />
)); ));
......
...@@ -14,7 +14,10 @@ export const checkChildrenByCondition = (item, fn, tag = 'every') => { ...@@ -14,7 +14,10 @@ export const checkChildrenByCondition = (item, fn, tag = 'every') => {
if (!checkIsGroup(item)) { if (!checkIsGroup(item)) {
return fn(item); return fn(item);
} }
return item.children[tag](t => checkChildrenByCondition(t, fn, tag)); return [
getId(item),
...item.children[tag](t => checkChildrenByCondition(t, fn, tag)),
];
}; };
const ListCard = props => { const ListCard = props => {
...@@ -30,22 +33,24 @@ const ListCard = props => { ...@@ -30,22 +33,24 @@ const ListCard = props => {
loading, loading,
} = props; } = props;
const [valueList, setValueList] = useState([]); const [valueList, setValueList] = useState([]);
const [changedItem, setChangedItem] = useState({ item: {} });
useEffect(() => { useEffect(() => {
setValueList(checkList); setValueList(checkList);
}, [dataList, loading]); }, [dataList, loading]);
const updateValueList = (checkedKeys, childrenKeys) => { const updateValueList = (checkedKeys, childrenKeys, sourceItem) => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.time('updateValueList:'); // console.time('updateValueList:');
const removekeys = _.difference(childrenKeys, checkedKeys); const removekeys = _.difference(childrenKeys, checkedKeys);
let result = _.uniq(_.union(checkedKeys, valueList)); let result = _.uniq(_.union(checkedKeys, valueList));
_.remove(result, v => removekeys.includes(v)); _.remove(result, v => removekeys.includes(v));
setValueList(result); setValueList(result);
if (sourceItem) setChangedItem(sourceItem);
// eslint-disable-next-line no-unused-expressions // eslint-disable-next-line no-unused-expressions
onChange && onChange(result); onChange && onChange(result);
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.timeEnd('updateValueList:'); // console.timeEnd('updateValueList:');
}; };
return ( return (
...@@ -72,6 +77,7 @@ const ListCard = props => { ...@@ -72,6 +77,7 @@ const ListCard = props => {
<ListCardItem <ListCardItem
item={item} item={item}
ouid={ouid} ouid={ouid}
changedItem={changedItem}
updateValueList={updateValueList} updateValueList={updateValueList}
valueList={valueList} valueList={valueList}
key={`item${index}key`} key={`item${index}key`}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment