index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Input, Modal } from 'antd';
  2. import { EditOutlined } from '@ant-design/icons';
  3. import type { Key } from 'react';
  4. import { useRef, useState } from 'react';
  5. import { connect } from '@formily/react';
  6. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  7. import ProTable from '@jetlinks/pro-table';
  8. import type { DeviceInstance } from '@/pages/device/Instance/typings';
  9. import moment from 'moment';
  10. import { useIntl } from '@@/plugin-locale/localeExports';
  11. import Service from '@/pages/device/Instance/service';
  12. import SearchComponent from '../SearchComponent';
  13. interface Props {
  14. value: Partial<DeviceInstance>[];
  15. onChange: (data: Partial<DeviceInstance>[]) => void;
  16. }
  17. export const service = new Service('device/instance');
  18. const FSelectDevices = connect((props: Props) => {
  19. // todo 考虑与单选设备合并
  20. const [visible, setVisible] = useState<boolean>(false);
  21. const intl = useIntl();
  22. const actionRef = useRef<ActionType>();
  23. const [searchParam, setSearchParam] = useState({});
  24. const columns: ProColumns<DeviceInstance>[] = [
  25. // {
  26. // dataIndex: 'index',
  27. // valueType: 'indexBorder',
  28. // width: 48,
  29. // },
  30. {
  31. title: 'ID',
  32. dataIndex: 'id',
  33. },
  34. {
  35. title: intl.formatMessage({
  36. id: 'pages.table.deviceName',
  37. defaultMessage: '设备名称',
  38. }),
  39. dataIndex: 'name',
  40. ellipsis: true,
  41. },
  42. {
  43. title: intl.formatMessage({
  44. id: 'pages.table.productName',
  45. defaultMessage: '产品名称',
  46. }),
  47. dataIndex: 'productName',
  48. ellipsis: true,
  49. },
  50. {
  51. title: intl.formatMessage({
  52. id: 'pages.device.instance.registrationTime',
  53. defaultMessage: '注册时间',
  54. }),
  55. dataIndex: 'registryTime',
  56. width: '200px',
  57. render: (text: any) => (text ? moment(text).format('YYYY-MM-DD HH:mm:ss') : ''),
  58. sorter: true,
  59. },
  60. // {
  61. // title: intl.formatMessage({
  62. // id: 'pages.table.description',
  63. // defaultMessage: '说明',
  64. // }),
  65. // dataIndex: 'description',
  66. // width: '15%',
  67. // ellipsis: true,
  68. // },
  69. ];
  70. const [data, setData] = useState<Partial<DeviceInstance>[]>(props.value);
  71. const rowSelection = {
  72. onChange: (selectedRowKeys: Key[], selectedRows: DeviceInstance[]) => {
  73. setData(selectedRows);
  74. },
  75. selectedRowKeys: data?.map((item) => item.id) as Key[],
  76. };
  77. return (
  78. <>
  79. <Input
  80. disabled
  81. value={props.value?.map((item) => item.name).join(',')}
  82. addonAfter={<EditOutlined onClick={() => setVisible(true)} />}
  83. />
  84. {visible && (
  85. <Modal
  86. maskClosable={false}
  87. visible
  88. title="选择设备"
  89. width="80vw"
  90. onCancel={() => setVisible(false)}
  91. onOk={() => {
  92. setVisible(false);
  93. props.onChange(data);
  94. }}
  95. >
  96. <SearchComponent<DeviceInstance>
  97. field={columns}
  98. enableSave={false}
  99. model="simple"
  100. onSearch={async (data1) => {
  101. setSearchParam(data1);
  102. }}
  103. target="choose-device"
  104. />
  105. <ProTable<DeviceInstance>
  106. tableAlertRender={false}
  107. rowSelection={{
  108. type: 'checkbox',
  109. ...rowSelection,
  110. }}
  111. search={false}
  112. columnEmptyText={''}
  113. toolBarRender={false}
  114. rowKey="id"
  115. pagination={{
  116. pageSize: 10,
  117. }}
  118. params={searchParam}
  119. columns={columns}
  120. actionRef={actionRef}
  121. request={(params) => service.query(params)}
  122. />
  123. </Modal>
  124. )}
  125. </>
  126. );
  127. });
  128. export default FSelectDevices;