CaseList.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { List, Tag, Typography } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { get } from "../../request";
  4. import { ICaseListResponse } from "../api/Dict";
  5. const { Text } = Typography;
  6. export interface ICaseListData {
  7. word: string;
  8. count: number;
  9. bold: number;
  10. }
  11. interface IWidget {
  12. word?: string;
  13. }
  14. const Widget = ({ word }: IWidget) => {
  15. const [caseData, setCaseData] = useState<ICaseListData[]>();
  16. const [count, setCount] = useState<number>();
  17. useEffect(() => {
  18. get<ICaseListResponse>(`/v2/case/${word}`).then((json) => {
  19. console.log("case", json);
  20. if (json.ok) {
  21. setCaseData(json.data.rows.sort((a, b) => b.count - a.count));
  22. setCount(json.data.count);
  23. }
  24. });
  25. }, [word]);
  26. return (
  27. <div style={{ padding: 4 }}>
  28. <List
  29. header={`单词数:${count}`}
  30. size="small"
  31. dataSource={caseData}
  32. renderItem={(item) => (
  33. <List.Item>
  34. <div
  35. style={{
  36. display: "flex",
  37. justifyContent: "space-between",
  38. width: "100%",
  39. }}
  40. >
  41. <Text strong={item.bold > 0 ? true : false}>{item.word}</Text>
  42. <Tag>{item.count}</Tag>
  43. </div>
  44. </List.Item>
  45. )}
  46. />
  47. </div>
  48. );
  49. };
  50. export default Widget;