AnchorCard.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { useIntl } from "react-intl";
  2. import { useState } from "react";
  3. import { Card, Space, Segmented } from "antd";
  4. import store from "../../store";
  5. import { modeChange } from "../../reducers/article-mode";
  6. import type { ArticleMode } from "../article/Article"
  7. interface IWidgetArticleCard {
  8. title?: React.ReactNode;
  9. children?: React.ReactNode;
  10. onModeChange?: Function;
  11. }
  12. const AnchorCardWidget = ({
  13. title,
  14. children,
  15. onModeChange,
  16. }: IWidgetArticleCard) => {
  17. const intl = useIntl();
  18. const [mode, setMode] = useState<string>("read");
  19. const modeSwitch = (
  20. <Segmented
  21. size="middle"
  22. options={[
  23. {
  24. label: intl.formatMessage({ id: "buttons.translate" }),
  25. value: "edit",
  26. },
  27. {
  28. label: intl.formatMessage({ id: "buttons.wbw" }),
  29. value: "wbw",
  30. },
  31. ]}
  32. value={mode}
  33. onChange={(value) => {
  34. const newMode = value.toString();
  35. if (typeof onModeChange !== "undefined") {
  36. if (mode === "read" || newMode === "read") {
  37. onModeChange(newMode);
  38. }
  39. }
  40. setMode(newMode);
  41. //发布mode变更
  42. store.dispatch(modeChange({ mode: newMode as ArticleMode }));
  43. }}
  44. />
  45. );
  46. return (
  47. <Card size="small" title={title} extra={<Space>{modeSwitch}</Space>}>
  48. {children}
  49. </Card>
  50. );
  51. };
  52. export default AnchorCardWidget;