inputAcc.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Button, Dropdown, Menu } from "antd";
  2. import React, { useEffect, useState } from "react";
  3. import style from './index.less'
  4. import SelectAccount from "./selectAccount";
  5. import { getChannelName } from '@/utils/utils'
  6. type Props = {
  7. onChange: (value: { MP?: any, ADQ?: any, id: number, accountName: string }[]) => void,
  8. value: { MP?: any, ADQ?: any, id: number, accountName: string }[]
  9. }
  10. function InputAcc(props: Props) {
  11. const { value = [], onChange } = props
  12. const [selectAccShow, setSelectAccShow] = useState<boolean>(false)
  13. const [data, setData] = useState<{ title: String, num: Number }>({ title: '', num: 0 })
  14. useEffect(() => {
  15. if (value.length > 0) {
  16. let num = 0
  17. let title = ""
  18. for (const iterator of value) {
  19. if (iterator?.MP?.length > 0) {
  20. title = getChannelName(iterator.accountName) + '-' + iterator?.MP[0].uname
  21. break
  22. }
  23. }
  24. value?.forEach((item: any) => {
  25. if(item?.MP?.length > 0) {
  26. num += item?.MP?.length
  27. }
  28. })
  29. setData(() => ({ title, num: num !==0 ? num - 1 : 0 }))
  30. }
  31. }, [value])
  32. const menu = () => {
  33. return <Menu style={{ width: 220, borderRadius: 4 }}>
  34. {value?.map((item: { id: number, accountName: string, MP?: any[], ADQ?: any[] }) => {
  35. if ((item?.MP && item?.MP?.length > 0) || (item?.ADQ && item?.ADQ?.length > 0)) {
  36. return <div key={item?.id}>
  37. {item?.MP && item?.MP?.length > 0 && item?.MP?.map((mp: { id: number, uname: string }) => <Menu.Item style={{ cursor: 'default' }} key={mp?.id} className={style.acc}>
  38. <div className={style.cc}>
  39. <span>{getChannelName(item?.accountName)}-{mp?.uname}</span>
  40. </div>
  41. </Menu.Item>)}
  42. </div>
  43. } else {
  44. return null
  45. }
  46. })}
  47. </Menu>
  48. }
  49. return <>
  50. {selectAccShow && <SelectAccount show={selectAccShow} onClose={() => { setSelectAccShow(false) }} onChange={(e: any) => { onChange(e); setSelectAccShow(false) }} channelAccounts={value} />}
  51. {value?.some((item: any) => (item?.MP?.length > 0 || item?.ADQ?.length > 0)) ? <div className={style.inputAcc}>
  52. <Dropdown overlay={menu} trigger={['click']}>
  53. <div className={style.acc}>
  54. {data.title} {data.num > 0 && <span>+{data.num}</span>}
  55. </div>
  56. </Dropdown>
  57. <Button type="text" className={style.edit} size="small" onClick={() => { setSelectAccShow(true) }}>修改</Button>
  58. </div> : <Button type="primary" className={style.edit} size="small" onClick={() => { setSelectAccShow(true) }}>选择公众号</Button>}
  59. </>
  60. }
  61. export default React.memo(InputAcc)