WbwDetailBasicRelation.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Badge, Button, Collapse, Space, Tooltip } from "antd";
  2. import { QuestionCircleOutlined } from "@ant-design/icons";
  3. import WbwDetailRelation from "./WbwDetailRelation";
  4. import store from "../../../store";
  5. import { grammar } from "../../../reducers/command";
  6. import type { IWbw, IWbwField } from "./WbwWord"
  7. import { useIntl } from "react-intl";
  8. import { useState } from "react";
  9. import { openPanel } from "../../../reducers/right-panel";
  10. interface IWidget {
  11. data: IWbw;
  12. showRelation?: boolean;
  13. onChange?: Function;
  14. onRelationAdd?: Function;
  15. }
  16. const WbwDetailBasicRelationWidget = ({
  17. data,
  18. showRelation,
  19. onChange,
  20. onRelationAdd,
  21. }: IWidget) => {
  22. const intl = useIntl();
  23. const [fromList, setFromList] = useState<string[]>();
  24. const relationCount = data.relation?.value
  25. ? JSON.parse(data.relation.value).length
  26. : 0;
  27. return (
  28. <Collapse bordered={false} collapsible={"icon"}>
  29. <Collapse.Panel
  30. header={
  31. <div style={{ display: "flex", justifyContent: "space-between" }}>
  32. <Space>
  33. {intl.formatMessage({ id: "buttons.relate" })}
  34. <Badge color="geekblue" count={relationCount} />
  35. </Space>
  36. <Tooltip
  37. title={intl.formatMessage({
  38. id: "columns.library.palihandbook.title",
  39. })}
  40. >
  41. <Button
  42. type="link"
  43. onClick={() => {
  44. if (fromList) {
  45. const endCase = fromList
  46. .map((item) => item + ".relations")
  47. .join(",");
  48. console.debug("from", fromList, endCase);
  49. store.dispatch(grammar(endCase));
  50. store.dispatch(openPanel("grammar"));
  51. }
  52. }}
  53. icon={<QuestionCircleOutlined />}
  54. />
  55. </Tooltip>
  56. </div>
  57. }
  58. key="relation"
  59. style={{ display: showRelation ? "block" : "none" }}
  60. >
  61. <WbwDetailRelation
  62. data={data}
  63. onChange={(e: IWbwField) => {
  64. if (typeof onChange !== "undefined") {
  65. onChange(e);
  66. }
  67. }}
  68. onAdd={onRelationAdd}
  69. onFromList={(value: string[]) => setFromList(value)}
  70. />
  71. </Collapse.Panel>
  72. </Collapse>
  73. );
  74. };
  75. export default WbwDetailBasicRelationWidget;