AnthologySelect.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { Select } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { get } from "../../request";
  4. import type { IAnthologyListResponse } from "../../api/Article";
  5. interface IOptions {
  6. value: string;
  7. label: string;
  8. }
  9. interface IWidget {
  10. studioName?: string;
  11. onSelect?: Function;
  12. }
  13. const AnthologyTocTreeWidget = ({ studioName, onSelect }: IWidget) => {
  14. const [anthology, setAnthology] = useState<IOptions[]>([
  15. { value: "all", label: "全部" },
  16. { value: "none", label: "没有加入文集的" },
  17. ]);
  18. useEffect(() => {
  19. const url = `/v2/anthology?view=studio&name=${studioName}`;
  20. get<IAnthologyListResponse>(url).then((json) => {
  21. if (json.ok) {
  22. const data = json.data.rows.map((item) => {
  23. return {
  24. value: item.uid,
  25. label: item.title,
  26. };
  27. });
  28. setAnthology([
  29. { value: "all", label: "全部" },
  30. { value: "none", label: "没有加入文集的" },
  31. ...data,
  32. ]);
  33. }
  34. });
  35. }, [studioName]);
  36. return (
  37. <Select
  38. defaultValue="all"
  39. style={{ width: 180 }}
  40. onChange={(value: string) => {
  41. console.log(`selected ${value}`);
  42. if (typeof onSelect !== "undefined") {
  43. onSelect(value);
  44. }
  45. }}
  46. options={anthology}
  47. />
  48. );
  49. };
  50. export default AnthologyTocTreeWidget;