默认效果
image.png

鼠标 Hover效果
image.png
dropdown下拉菜单文档
https://ant-design.antgroup.com/components/dropdown-cn

  1. import React from "react";
  2. import { node, object, string } from "prop-types";
  3. import { Dropdown } from 'antd';
  4. import ChildNode from "./ChildNode";
  5. const items = [
  6. {
  7. key: '1',
  8. label: '待开始',
  9. },
  10. {
  11. key: '2',
  12. label: '进行中',
  13. },
  14. {
  15. key: '3',
  16. label: '已完成',
  17. },
  18. ];
  19. DropDownMenu.propTypes = {
  20. element: node,
  21. buttonProps: object,
  22. trigger: string,
  23. };
  24. DropDownMenu.defaultProps = {
  25. trigger: 'click',
  26. }
  27. function DropDownMenu(props) {
  28. const {
  29. element,
  30. buttonProps,
  31. trigger,
  32. } = props;
  33. const handleClick = () => {
  34. }
  35. return (
  36. <Dropdown
  37. menu={{
  38. items,
  39. selectable: true, // 支持选择
  40. defaultSelectedKeys: ['3'],
  41. }}
  42. >
  43. <ChildNode
  44. element={element}
  45. buttonProps={buttonProps}
  46. trigger={trigger}
  47. />
  48. </Dropdown>
  49. );
  50. }
  51. export default DropDownMenu;

merge 属性

image.png
lodash.merge 合并参数

  1. function App() {
  2. const handleClick = ({ item, key, keyPath, domEvent }) => {
  3. console.log({ item, key, keyPath, domEvent })
  4. }
  5. return (
  6. <DropDownList
  7. onClick={handleClick}
  8. menu={{
  9. items: [
  10. {key: 'nj', label: '南京'},
  11. {key: 'sh', label: '上海'}
  12. ]
  13. }}
  14. // element={<a>批处理</a>}
  15. />
  16. )
  17. }

ChildNode

子节点支持动态参数

  1. import React, { cloneElement, isValidElement } from "react";
  2. import { node, object } from "prop-types";
  3. import { Button } from "antd";
  4. import { DownOutlined } from "@ant-design/icons";
  5. import { isObject } from "lodash-es";
  6. /**
  7. * dropdown 有额外的属性,需要传递到子节点
  8. * className: "ant-dropdown-trigger ant-dropdown-open"
  9. * onMouseEnter
  10. * onMouseLeave
  11. * onClick
  12. */
  13. ChildNode.propTypes = {
  14. element: node,
  15. buttonProps: object,
  16. };
  17. ChildNode.defaultProps = {
  18. buttonProps: {
  19. type: "primary",
  20. children: "批量操作",
  21. icon: <DownOutlined />
  22. },
  23. };
  24. function ChildNode(props) {
  25. const {
  26. element,
  27. buttonProps,
  28. ...restProps
  29. } = props;
  30. // element优先级大于 buttonProps
  31. if (isValidElement(element)) {
  32. return cloneElement(element, restProps);
  33. }
  34. // 默认显示 Button
  35. if (isObject(buttonProps)) {
  36. const { children, icon, ...restButton } = buttonProps;
  37. return (
  38. <Button
  39. {...restButton}
  40. {...restProps}
  41. >
  42. {children}{icon}
  43. </Button>
  44. );
  45. }
  46. return null;
  47. }
  48. // function propsEqual(prevProps, nextProps) {
  49. // return isEqual(prevProps, nextProps)
  50. // }
  51. // export default memo(ChildNode, propsEqual);
  52. export default ChildNode;