StudioSelect.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Select } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { get } from "../../request";
  4. import type { IStudio } from "../auth/Studio"
  5. interface IStudioListResponse {
  6. ok: boolean;
  7. message: string;
  8. data: {
  9. rows: IStudio[];
  10. count: number;
  11. };
  12. }
  13. interface IOptions {
  14. value: string;
  15. label?: string;
  16. }
  17. interface IWidget {
  18. studioName?: string;
  19. onSelect?: Function;
  20. }
  21. const StudioSelectWidget = ({ studioName, onSelect }: IWidget) => {
  22. const [anthology, setAnthology] = useState<IOptions[]>([
  23. { value: "all", label: "全部" },
  24. ]);
  25. useEffect(() => {
  26. const url = `/v2/studio?view=collaboration-channel&studio_name=${studioName}`;
  27. get<IStudioListResponse>(url).then((json) => {
  28. if (json.ok) {
  29. const data = json.data.rows
  30. .sort((a, b) => {
  31. if (a.nickName && b.nickName && a.nickName < b.nickName) {
  32. return -1;
  33. } else {
  34. return 1;
  35. }
  36. })
  37. .map((item) => {
  38. return {
  39. value: item.id,
  40. label: item.nickName,
  41. };
  42. });
  43. setAnthology([{ value: "all", label: "全部" }, ...data]);
  44. }
  45. });
  46. }, [studioName]);
  47. return (
  48. <Select
  49. defaultValue="all"
  50. style={{ width: 180 }}
  51. onChange={(value: string) => {
  52. console.log(`selected ${value}`);
  53. if (typeof onSelect !== "undefined") {
  54. onSelect(value);
  55. }
  56. }}
  57. options={anthology}
  58. />
  59. );
  60. };
  61. export default StudioSelectWidget;