index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 { useIntl } from '@@/plugin-locale/localeExports';
  10. import Service from '@/pages/device/Instance/service';
  11. import SearchComponent from '../SearchComponent';
  12. import _ from 'lodash';
  13. interface Props {
  14. value: Partial<DeviceInstance>[];
  15. onChange: (data: Partial<DeviceInstance>[]) => void;
  16. productId?: string;
  17. }
  18. export const service = new Service('device/instance');
  19. const FSelectDevices = connect((props: Props) => {
  20. // todo 考虑与单选设备合并
  21. const [visible, setVisible] = useState<boolean>(false);
  22. const intl = useIntl();
  23. const actionRef = useRef<ActionType>();
  24. const [searchParam, setSearchParam] = useState({});
  25. const columns: ProColumns<DeviceInstance>[] = [
  26. {
  27. title: 'ID',
  28. dataIndex: 'id',
  29. ellipsis: true,
  30. },
  31. {
  32. title: intl.formatMessage({
  33. id: 'pages.table.deviceName',
  34. defaultMessage: '设备名称',
  35. }),
  36. dataIndex: 'name',
  37. ellipsis: true,
  38. },
  39. {
  40. title: '固件版本',
  41. dataIndex: 'firmwareInfo',
  42. ellipsis: true,
  43. render: (text: any, record: any) => record?.version || '',
  44. },
  45. {
  46. title: intl.formatMessage({
  47. id: 'pages.device.instance.registrationTime',
  48. defaultMessage: '注册时间',
  49. }),
  50. dataIndex: 'registerTime',
  51. width: '200px',
  52. valueType: 'dateTime',
  53. },
  54. ];
  55. const [data, setData] = useState<Partial<DeviceInstance>[]>(props?.value || []);
  56. const rowSelection = {
  57. onChange: (selectedRowKeys: Key[], selectedRows: DeviceInstance[]) => {
  58. const list = [...data];
  59. selectedRows.map((item) => {
  60. if (!_.map(data, 'id').includes(item.id)) {
  61. list.push(item);
  62. }
  63. });
  64. setData(list);
  65. },
  66. selectedRowKeys: data?.map((item) => item.id) as Key[],
  67. };
  68. return (
  69. <>
  70. <Input
  71. disabled
  72. value={props.value?.map((item) => item.name).join(',')}
  73. addonAfter={<EditOutlined onClick={() => setVisible(true)} />}
  74. />
  75. {visible && (
  76. <Modal
  77. maskClosable={false}
  78. visible
  79. title="选择设备"
  80. width="80vw"
  81. onCancel={() => setVisible(false)}
  82. onOk={() => {
  83. setVisible(false);
  84. props.onChange(data);
  85. }}
  86. >
  87. <SearchComponent<DeviceInstance>
  88. field={columns}
  89. enableSave={false}
  90. model="simple"
  91. onSearch={async (data1) => {
  92. setSearchParam(data1);
  93. actionRef.current?.reset?.();
  94. }}
  95. target="choose-device"
  96. />
  97. <ProTable<DeviceInstance>
  98. tableAlertRender={false}
  99. rowSelection={{
  100. type: 'checkbox',
  101. ...rowSelection,
  102. }}
  103. search={false}
  104. columnEmptyText={''}
  105. toolBarRender={false}
  106. rowKey="id"
  107. pagination={{
  108. pageSize: 10,
  109. }}
  110. params={searchParam}
  111. columns={columns}
  112. actionRef={actionRef}
  113. request={async (params) => {
  114. return service.queryDetailList({
  115. context: {
  116. includeTags: false,
  117. includeBind: false,
  118. includeRelations: false,
  119. },
  120. ...params,
  121. terms: [
  122. ...(params?.terms || []),
  123. {
  124. terms: [
  125. {
  126. column: 'productId',
  127. value: props?.productId,
  128. },
  129. ],
  130. type: 'and',
  131. },
  132. ],
  133. sorts: [{ name: 'createTime', order: 'desc' }],
  134. });
  135. }}
  136. />
  137. </Modal>
  138. )}
  139. </>
  140. );
  141. });
  142. export default FSelectDevices;