[Code Snippets] 全选、反选、多选(未全选)、多选(到全选)

import {useEffect, useState} from "react";
import NewBaseDataService from "src/services/baseData/newIndex";
import Bus from "../utils/eventBus";
import {useControlInfoParam} from "./keyMan/useControlInfoParam";
export function useBizTag(type = '') {
    const [bizTags, setBizTags] = useState([
        {
            name: '不限',
            code: 'all',
            num: 0,
        },
    ])

    useEffect(() => {
        loadData().then(r => r)
        Bus.addListener('refresh-tags-biz', (payload) => {
            let {param} = useControlInfoParam(String(payload.sourceUnit))
            console.log('output-> [refresh-tags-biz]', param)
            refreshBizTags(param).then(r => r)
        })
        return () => {
            Bus.off('refresh-tags-biz', () => {})
        }
    }, []);

    const refreshBizTags = async (payload) => {
        const labelRes = await NewBaseDataService.getBusinessLabel(payload);
        setBizTags([
            {
                name: '不限',
                code: 'all',
                num: 0,
            },
            ...labelRes.data,
        ])
    }

    const loadData = async () => {
        if (type === 'controlInfo') {
            setBizTags([
                {
                    name: '全部',
                    code: 'all',
                },
                {
                    name: '反恐',
                    code: '1',
                    num: 12,
                },
                {
                    name: '禁毒',
                    code: '2',
                    num: 23,
                },
                {
                    name: '治安',
                    code: '3',
                    num: 4
                }
            ])
        } else {
            const labelRes = await NewBaseDataService.getBusinessLabel({sourceUnit: '', dataSource: '',});
            setBizTags([
                {
                    name: '不限',
                    code: 'all',
                    num: 0,
                },
                ...labelRes.data,
            ])
        }
    }

    const [activeBizTag, setActiveBizTag] = useState([])

    const handleBizTag = (tag) => {
        if (activeBizTag.includes('all') && tag.code === 'all') {
            setActiveBizTag([])
            return
        }
        if (!activeBizTag.includes('all') && tag.code === 'all') {
            setActiveBizTag(bizTags.map(tag => tag.code))
            return
        }
        if (activeBizTag.includes(tag.code)) {

            if (activeBizTag.includes('all')) {
                setActiveBizTag(activeBizTag.filter(arrTag => (arrTag !== 'all' && arrTag !== tag.code)))
            } else {
                setActiveBizTag(activeBizTag.filter(arrTag => arrTag !== tag.code))
            }
        } else {
            setActiveBizTag([...activeBizTag, tag.code])
            if ((activeBizTag.length === bizTags.length - 2) && !activeBizTag.includes('all')) {
                setActiveBizTag(bizTags.map(tag => tag.code))
            }
        }
    }
    return {
        bizTags,
        handleBizTag,
        activeBizTag,
        setActiveBizTag
    }
}

posted @ 2024-06-25 16:30  Felix_Openmind  阅读(6)  评论(0编辑  收藏  举报