SelectModal.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. Input,
  3. Modal,
  4. Table,
  5. TableColumnsType,
  6. TablePaginationConfig,
  7. Button,
  8. Select,
  9. } from "antd";
  10. import React, { useRef, useState } from "react";
  11. import { useRequest } from "ahooks";
  12. import { SearchOutlined } from "@ant-design/icons";
  13. export default function SelectModal({
  14. api,
  15. columns,
  16. onSuccess,
  17. onChange,
  18. defaultParams,
  19. value,
  20. disabled,
  21. }: {
  22. api: (params: any) => Promise<any>;
  23. columns: TableColumnsType<any>;
  24. onSuccess: (data: any, params: any[]) => any[];
  25. onChange: (selectedRows: any[]) => void;
  26. defaultParams?: [params: any];
  27. value: any;
  28. disabled: boolean;
  29. }) {
  30. const [open, setOpen] = React.useState(false);
  31. const [searchKey, setSearchKey] = useState("");
  32. const [selectedRowRows, setSelectedRowRows] = React.useState<any[]>([]);
  33. const [dataSource, setDataSource] = React.useState<any[]>([]);
  34. const [pagination, setPagination] = useState<TablePaginationConfig>({
  35. current: 1,
  36. pageSize: 10,
  37. pageSizeOptions: ["10", "20", "30", "40"],
  38. });
  39. const dataRef = useRef<any[]>([]);
  40. const { loading, run } = useRequest(api, {
  41. defaultParams,
  42. onSuccess(data, params) {
  43. const result = onSuccess(data, params);
  44. setDataSource(result);
  45. dataRef.current = result;
  46. },
  47. });
  48. const handleSlected = (_keys: React.Key[], rows: any[]) => {
  49. setSelectedRowRows(rows);
  50. };
  51. const handleChange = (pagination: TablePaginationConfig) => {
  52. setPagination(pagination);
  53. if (defaultParams) {
  54. run({
  55. currentPage: pagination.current,
  56. pageSize: pagination.pageSize,
  57. });
  58. }
  59. };
  60. const handleSearch = () => {
  61. setDataSource(dataRef.current.filter((item) => item.name.includes(searchKey)))
  62. }
  63. const handleOk = () => {
  64. onChange(selectedRowRows);
  65. setOpen(false);
  66. };
  67. return (
  68. <>
  69. <Modal
  70. open={open}
  71. title="请选择"
  72. footer={
  73. <div>
  74. <Button className="m-r-8px" onClick={() => setOpen(false)}>
  75. 取消
  76. </Button>
  77. <Button type="primary" onClick={handleOk}>
  78. 确定
  79. </Button>
  80. </div>
  81. }
  82. closable={false}
  83. >
  84. <Input
  85. suffix={<SearchOutlined />}
  86. placeholder="关键字搜索"
  87. value={searchKey}
  88. onChange={(e) => setSearchKey(e.target.value)}
  89. onBlur={handleSearch}
  90. onPressEnter={handleSearch}
  91. />
  92. <Table
  93. dataSource={dataSource}
  94. columns={columns}
  95. rowKey={"id"}
  96. rowSelection={{
  97. type: "radio",
  98. onChange: handleSlected,
  99. }}
  100. pagination={pagination}
  101. loading={loading}
  102. onChange={handleChange}
  103. />
  104. </Modal>
  105. <Select
  106. dropdownStyle={{ display: "none" }}
  107. placeholder="请选择"
  108. onClick={() => !disabled && setOpen(true)}
  109. value={value.label}
  110. disabled={disabled}
  111. />
  112. </>
  113. );
  114. }