Search.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as React from 'react';
  2. import { Form, Select, Button } from 'antd';
  3. import { apis } from '@/apis';
  4. import { Query } from '../types';
  5. const FormItem = Form.Item;
  6. const { Option } = Select;
  7. interface Props {
  8. onClickSearch: (query: Query) => Promise<any>,
  9. onClickReset: (query: Query) => Promise<any>,
  10. };
  11. const Search: React.FC<Props> = (props: Props) => {
  12. const {
  13. onClickSearch,
  14. onClickReset
  15. } = props;
  16. const [form] = Form.useForm();
  17. type ApplicationList = {
  18. label: string,
  19. value: string,
  20. }[];
  21. const [applicationList, setApplicationList] = React.useState<ApplicationList>([]);
  22. const fetchApplicationList = async () => {
  23. try {
  24. const res = await apis.fetchApplicationList();
  25. const list = res.data.map((item: any) => {
  26. return {
  27. label: item.name,
  28. value: item.appId,
  29. }
  30. });
  31. setApplicationList(list);
  32. } catch (error: any) {
  33. console.error(error);
  34. }
  35. }
  36. const init = async () => {
  37. await fetchApplicationList();
  38. };
  39. React.useEffect(() => {
  40. init();
  41. }, []);
  42. // 点击查询
  43. const handleClickSearch = async () => {
  44. const values = form.getFieldsValue();
  45. await onClickSearch(values);
  46. };
  47. // 点击重置
  48. const handleClickReset = async () => {
  49. form.resetFields();
  50. const values = form.getFieldsValue();
  51. await onClickReset(values);
  52. };
  53. return (
  54. <Form form={form} layout='inline' colon={false}>
  55. <FormItem label='应用' name='appId'>
  56. <Select
  57. style={{ width: 200 }}
  58. placeholder='全部'
  59. allowClear={true}
  60. >
  61. {
  62. applicationList.map((item, index) => {
  63. return <Option value={item.value} key={index}>
  64. {item.label}
  65. </Option>
  66. })
  67. }
  68. </Select>
  69. </FormItem>
  70. <FormItem>
  71. <Button
  72. style={{ marginRight: 16 }}
  73. type='primary'
  74. onClick={handleClickSearch}
  75. >
  76. 查询
  77. </Button>
  78. <Button onClick={handleClickReset}>
  79. 重置
  80. </Button>
  81. </FormItem>
  82. </Form>
  83. );
  84. };
  85. export default Search;