2
0

Options.tsx 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Button, Dropdown, type MenuProps } from "antd"
  2. import { useState } from "react";
  3. export interface IMenu {
  4. key: string;
  5. label: string;
  6. }
  7. interface IWidget {
  8. items: IMenu[];
  9. icon?: React.ReactNode;
  10. text?: string;
  11. initKey?: string;
  12. onChange?: (key: string) => void;
  13. }
  14. const Options = ({ items, icon, text, initKey = "1", onChange }: IWidget) => {
  15. const [currKey, setCurrKey] = useState(initKey);
  16. const currValue = items.find(
  17. (item) => item.key === (currKey ?? initKey)
  18. )?.label;
  19. const onClick: MenuProps["onClick"] = ({ key }) => {
  20. if (onChange) {
  21. onChange(key);
  22. }
  23. setCurrKey(key);
  24. };
  25. return (
  26. <Dropdown
  27. menu={{
  28. items,
  29. onClick,
  30. selectable: true,
  31. defaultSelectedKeys: [currKey],
  32. }}
  33. trigger={["click"]}
  34. placement="bottomLeft"
  35. >
  36. <Button type="text" icon={icon}>
  37. {text}
  38. {currValue}
  39. </Button>
  40. </Dropdown>
  41. );
  42. };
  43. export default Options;