pageList.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { useDrag } from "ahooks";
  2. import { Col, Row, Space, Tabs, Typography } from "antd";
  3. import React, { useState, useImperativeHandle, forwardRef, useRef, useCallback } from "react";
  4. import styles from './index.less'
  5. import { useModel } from "@umijs/max";
  6. import { useToken } from "@ant-design/pro-components";
  7. const DragItem = ({ children }: any) => {
  8. const dragRef = useRef(null);
  9. const [dragging, setDragging] = useState(false);
  10. useDrag(children.key, dragRef, {
  11. onDragStart: () => {
  12. console.log(children.key)
  13. setDragging(true);
  14. },
  15. onDragEnd: () => {
  16. setDragging(false);
  17. },
  18. });
  19. return (
  20. <div
  21. ref={dragRef}
  22. style={{ display: 'inline-block' }}
  23. >
  24. {children}
  25. </div>
  26. );
  27. };
  28. type Item = {
  29. componentName: string,//组件名称 也是type
  30. defaultComponentConfigList: any[],//组件配置
  31. enabled: boolean,//是否可编辑
  32. enabledAppTypes: number[]//支持的小程序
  33. id: number,//组件id
  34. remark: string,//备注 一般为中文名称
  35. pagePath: string,//所在页面地址
  36. disabled: boolean,//是否看使用,在已经添加进页面后禁止使用
  37. }
  38. const Comp = ({ item }: { item: Item }) => {
  39. let { token } = useToken()
  40. return item.disabled ? <div key={`${item.id}-${item.componentName}-${item.pagePath}`} className={styles.box} style={{ background: token.colorBgContainerDisabled, cursor: 'no-drop' }}>
  41. {item.remark || item.componentName}
  42. </div> : <DragItem >
  43. <div key={`${item.id}-${item.componentName}-${item.pagePath}`} className={styles.box} style={{ cursor: "copy" }}>
  44. {item.remark || item.componentName}
  45. </div>
  46. </DragItem>
  47. }
  48. // 定义组件的 Props 类型
  49. interface Props {
  50. data: any[]
  51. }
  52. // 使用 forwardRef 以支持传递 ref
  53. const PageList = forwardRef((props: Props, ref) => {
  54. let { initialState } = useModel("@@initialState")
  55. let { state, dispatch } = useModel('appPageConifg')
  56. let { data } = props
  57. // 使用 useImperativeHandle 暴露方法给父组件
  58. useImperativeHandle(ref, () => ({
  59. }));
  60. return <Tabs
  61. onChange={(activeKey) => {
  62. dispatch({ type: "setAll", params: { activePage: activeKey } })
  63. }}
  64. activeKey={state.activePage}
  65. items={data?.map(item => {
  66. return {
  67. label: item.pageName,
  68. key: item.pagePath,
  69. disabled: !item.enabledComponent,
  70. children: item.pagePath === state.activePage ? <Row gutter={[20, 20]} className={styles.pageList} >
  71. <Col span={24} style={{ order: 0 }}>
  72. <Typography.Title level={4} >顶部组件</Typography.Title>
  73. {
  74. item?.appComponentList?.filter((i: Item) => i.componentName === 'banners')?.map((comp: Item) => {
  75. let newComp = { ...comp, pagePath: item.pagePath }
  76. let appType = initialState?.selectApp?.appType || 0
  77. let pageConfig = state.pageConfigList?.find(page => page.pageUrl === item.pagePath)
  78. let thePage = pageConfig?.workDirectionList?.find(page => page.workDirection == state.tabs)
  79. let list = thePage?.componentConfigList || []
  80. newComp.disabled = !!list.find(i => i.appComponentId == newComp.id)
  81. if (newComp.enabledAppTypes.includes(appType)) {
  82. return <Comp item={newComp} key={item.id} />
  83. }
  84. })
  85. }
  86. </Col>
  87. <Col span={24}>
  88. <Typography.Title level={4} >内容组件</Typography.Title>
  89. <Space wrap>
  90. {
  91. item?.appComponentList?.filter((i: Item) => i.componentName !== 'banners')?.map((comp: Item) => {
  92. let newComp = { ...comp, pagePath: item.pagePath }
  93. let appType = initialState?.selectApp?.appType || 0
  94. let pageConfig = state.pageConfigList?.find((page: { pageUrl: any; }) => page.pageUrl === item.pagePath)
  95. let thePage = pageConfig?.workDirectionList?.find(page => page.workDirection == state.tabs)
  96. let list = thePage?.componentConfigList || []
  97. newComp.disabled = !!list.find(i => i.appComponentId == newComp.id)
  98. if (newComp.enabledAppTypes.includes(appType)) {
  99. return <Comp item={newComp} key={comp.id} />
  100. }
  101. })
  102. }
  103. </Space>
  104. </Col>
  105. </Row> : <>无组件</>,
  106. }
  107. })} />
  108. });
  109. export default PageList;