index.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. interface Props {
  13. value: Partial<DeviceInstance>;
  14. onChange: (data: Partial<DeviceInstance>) => void;
  15. }
  16. export const service = new Service('device/instance');
  17. const FSelectDevice = connect((props: Props) => {
  18. const [visible, setVisible] = useState<boolean>(false);
  19. const intl = useIntl();
  20. const actionRef = useRef<ActionType>();
  21. const columns: ProColumns<DeviceInstance>[] = [
  22. {
  23. dataIndex: 'index',
  24. valueType: 'indexBorder',
  25. width: 48,
  26. },
  27. {
  28. title: 'ID',
  29. dataIndex: 'id',
  30. },
  31. {
  32. title: intl.formatMessage({
  33. id: 'pages.table.deviceName',
  34. defaultMessage: '设备名称',
  35. }),
  36. dataIndex: 'name',
  37. ellipsis: true,
  38. },
  39. {
  40. title: intl.formatMessage({
  41. id: 'pages.table.productName',
  42. defaultMessage: '产品名称',
  43. }),
  44. dataIndex: 'productName',
  45. ellipsis: true,
  46. },
  47. {
  48. title: intl.formatMessage({
  49. id: 'pages.device.instance.registrationTime',
  50. defaultMessage: '注册时间',
  51. }),
  52. dataIndex: 'registryTime',
  53. width: '200px',
  54. render: (text: any) => (text ? moment(text).format('YYYY-MM-DD HH:mm:ss') : ''),
  55. sorter: true,
  56. },
  57. {
  58. title: intl.formatMessage({
  59. id: 'pages.table.description',
  60. defaultMessage: '说明',
  61. }),
  62. dataIndex: 'description',
  63. width: '15%',
  64. ellipsis: true,
  65. },
  66. ];
  67. const [data, setData] = useState<Partial<DeviceInstance>>(props.value);
  68. const rowSelection = {
  69. onChange: (selectedRowKeys: Key[], selectedRows: DeviceInstance[]) => {
  70. setData(selectedRows[0]);
  71. },
  72. selectedRowKeys: [data?.id] as Key[],
  73. };
  74. return (
  75. <>
  76. <Input
  77. size="small"
  78. disabled
  79. value={props.value?.name}
  80. addonAfter={<EditOutlined onClick={() => setVisible(true)} />}
  81. />
  82. {visible && (
  83. <Modal
  84. maskClosable={false}
  85. visible
  86. title="选择设备"
  87. width="80vw"
  88. onCancel={() => setVisible(false)}
  89. onOk={() => {
  90. setVisible(false);
  91. props.onChange(data);
  92. }}
  93. >
  94. <ProTable<DeviceInstance>
  95. tableAlertRender={false}
  96. rowSelection={{
  97. type: 'radio',
  98. ...rowSelection,
  99. }}
  100. toolBarRender={false}
  101. rowKey="id"
  102. pagination={{
  103. pageSize: 10,
  104. }}
  105. columnEmptyText={''}
  106. columns={columns}
  107. actionRef={actionRef}
  108. request={(params) => service.query(params)}
  109. />
  110. </Modal>
  111. )}
  112. </>
  113. );
  114. });
  115. export default FSelectDevice;