All files / src/transfer/_example TransferExample.jsx

69.23% Statements 18/26
75% Branches 3/4
58.82% Functions 10/17
75% Lines 18/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                      1x                                                                 1x         6x                         6x   4x   2x               1x       30x 6x                                   1x                     1x 1x   1x                                             6x                     6x                   1x 1x 1x                                                  
import React, { useState } from "react";
import { Transfer } from "@tencent/tea-component/lib/transfer";
import { Table } from "@tencent/tea-component/lib/table";
import { selectable } from "@tencent/tea-component/lib/table/addons/selectable";
import { removeable } from "@tencent/tea-component/lib/table/addons/removeable";
import { SearchBox } from "@tencent/tea-component/lib/searchbox";
import { Select } from "@tencent/tea-component/lib/select";
import { Button } from "@tencent/tea-component/lib/button";
import { Modal } from "@tencent/tea-component/lib/modal";
import { scrollable } from "@tencent/tea-component/lib/table/addons/scrollable";
 
const cvmList = [
  {
    instanceId: "ins-f5a68io6",
    instanceName: "Hongkong VPN1",
    status: "running",
  },
  {
    instanceId: "ins-4m99aio4",
    instanceName: "Hongkong VPN2",
    status: "running",
  },
  {
    instanceId: "ins-3e7y5ww3",
    instanceName: "Guangzhou Test",
    status: "stopped",
  },
  {
    instanceId: "ins-49as515o",
    instanceName: "Hongkong Test",
    status: "running",
  },
  {
    instanceId: "ins-3e7y5ww2",
    instanceName: "Guangzhou Test",
    status: "stopped",
  },
  {
    instanceId: "ins-49as5151",
    instanceName: "Hongkong Test",
    status: "running",
  },
];
 
const columns = [
  {
    key: "instance",
    header: "ID/实例名",
    render: cvm => (
      <>
        <p>
          <a>{cvm.instanceId}</a>
        </p>
        <p>{cvm.instanceName}</p>
      </>
    ),
  },
  {
    key: "status",
    header: "状态",
    width: 100,
    render: cvm => {
      switch (cvm.status) {
        case "running":
          return <span style={{ color: "green" }}>运行中</span>;
        case "stopped":
          return <span style={{ color: "red" }}>已关机</span>;
      }
      return cvm.status;
    },
  },
];
 
function SourceTable({ dataSource, targetKeys, onChange }) {
  return (
    <Table
      records={dataSource}
      recordKey="instanceId"
      rowDisabled={record => record.status === "stopped"}
      rowClassName={record => record.status}
      columns={columns}
      addons={[
        scrollable({
          maxHeight: 310,
          onScrollBottom: () => console.log("到达底部"),
        }),
        selectable({
          value: targetKeys,
          onChange,
          rowSelect: true,
        }),
      ]}
    />
  );
}
 
function TargetTable({ dataSource, onRemove }) {
  return (
    <Table
      records={dataSource}
      recordKey="instanceId"
      columns={columns}
      addons={[removeable({ onRemove })]}
    />
  );
}
 
function TransferDemo() {
  const [targetKeys, setTargetKeys] = useState([]);
  const [inputValue, setInputValue] = useState("");
 
  return (
    <Transfer
      header={
        <>
          <h4 style={{ display: "inline-block" }}>选择地域</h4>
          <Select placeholder="请选择" options={[]} />
        </>
      }
      leftCell={
        <Transfer.Cell
          scrollable={false}
          title="选择云服务器"
          tip="支持按住 shift 键进行多选"
          header={
            <SearchBox
              value={inputValue}
              onChange={value => setInputValue(value)}
            />
          }
        >
          <SourceTable
            dataSource={cvmList.filter(
              i =>
                i.instanceId.includes(inputValue) ||
                i.instanceName.includes(inputValue)
            )}
            targetKeys={targetKeys}
            onChange={keys => setTargetKeys(keys)}
          />
        </Transfer.Cell>
      }
      rightCell={
        <Transfer.Cell title={`已选择 (${targetKeys.length})`}>
          <TargetTable
            dataSource={cvmList.filter(i => targetKeys.includes(i.instanceId))}
            onRemove={key => setTargetKeys(targetKeys.filter(i => i !== key))}
          />
        </Transfer.Cell>
      }
    />
  );
}
 
export default function TransferExample() {
  const [modalVisible, setModalVisible] = useState(false);
  const close = () => setModalVisible(false);
  return (
    <>
      <section>
        <Button onClick={() => setModalVisible(true)}>打开穿梭框</Button>
        <Modal caption="请选择" size="l" visible={modalVisible} onClose={close}>
          <Modal.Body>
            <TransferDemo />
          </Modal.Body>
          <Modal.Footer>
            <Button type="primary" onClick={close}>
              确定
            </Button>
            <Button type="weak" onClick={close}>
              取消
            </Button>
          </Modal.Footer>
        </Modal>
      </section>
 
      <section style={{ marginTop: 60 }}>
        <TransferDemo />
      </section>
    </>
  );
}