All files / src/table/_example TableComplexExample.jsx

81.08% Statements 30/37
81.25% Branches 13/16
76.47% Functions 13/17
80.56% Lines 29/36

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499            1x   1x       1x       1x     1x         1x       1x 1x 4x 24x     1x     1x       1x             28x             8x                   8x                             1x       8x                                                             4x         32x 20x   12x                     1x 8x 4x 4x 4x                               4x                                       1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
import React, { useState, useMemo, useCallback } from "react";
import { Table } from "@tencent/tea-component/lib/table";
import { Icon } from "@tencent/tea-component/lib/icon";
import { Button } from "@tencent/tea-component/lib/button";
import { Modal } from "@tencent/tea-component/lib/modal";
 
const { expandable, selectable, indentable } = Table.addons;
 
const records = getMockRecords();
 
export default function TableComplexExample() {
  // 当前展开的产品
  const [expandedKeys, setExpandedKeys] = useState([
    // 默认展开第一个产品
    String(records[0].fType),
  ]);
  const expandedSet = new Set(expandedKeys);
 
  // 当前选中的消息
  const [selectedKeys, setSelectedKeys] = useState([]);
 
  /**
   * 生成子孙关系信息
   */
  const getRecordRelations = useCallback(records => {
    /**
     * @type {import('@tencent/tea-component/lib/checktree').CheckTreeRelation}
     */
    const relations = {};
    for (const record of records) {
      for (const msg of record.msgList) {
        relations[msg.msgType] = record.fType;
      }
    }
    return relations;
  }, []);
 
  const relations = useMemo(() => getRecordRelations(records), [
    getRecordRelations,
  ]);
 
  return (
    <Table
      // 配置行
      records={records}
      // 配置行键值
      recordKey={record => {
        // 这里要注意,原始数据中数据类型全是字符串,不转成字符串会导致 key 值匹配不上
        return String(record.msgList ? record.fType : record.msgType);
      }}
      // 配置列
      columns={[
        {
          key: "name",
          header: "消息类型",
          render: record => record.name,
          width: 250,
        },
        renderIconColumn("enableSiteMsg", "站内信"),
        renderIconColumn("enableEMail", "邮件"),
        renderIconColumn("enableSms", "短信"),
        renderIconColumn("enableWX", "微信"),
        {
          key: "receivers",
          header: "接收人",
          render: record => record.receivers || null,
        },
        {
          key: "settings",
          header: "操作",
          width: 100,
          render: renderOperationColumn(expandedSet, setExpandedKeys),
        },
      ]}
      // 配置插件
      addons={[
        expandable({
          // 已经展开的产品
          expandedKeys,
          // 产品展开为消息行
          expand: record => record.msgList || null,
          // 发生展开行为时,回调更新展开键值
          onExpandedKeysChange: keys => setExpandedKeys(keys),
          // 只有产品行允许展开
          shouldRecordExpandable: record => Boolean(record.msgList),
        }),
        selectable({
          // 选框放在「消息类型」列上
          targetColumnKey: "name",
          // 提供子孙关系
          relations,
          // 禁用全选
          all: false,
          // 已选中的键值
          value: selectedKeys,
          // 选中键值发生变更
          onChange: value => setSelectedKeys(value),
        }),
        indentable({
          // 缩进放在「消息类型」列上
          targetColumnKey: "name",
          // 提供层级关系
          relations,
        }),
      ]}
    />
  );
}
 
/**
 * 渲染图标列
 * @param {string} key
 * @param {string} header
 */
function renderIconColumn(key, header) {
  return {
    key,
    header,
    width: 80,
    render: record => {
      if (!record.currentChannel || !record.currentChannel[key]) {
        return null;
      }
      return <Icon type="success" />;
    },
  };
}
 
/**
 * 渲染操作列
 * @param {Set<string>} expandedSet
 * @param {(keys: string[]) => void} setExpandedKeys
 */
function renderOperationColumn(expandedSet, setExpandedKeys) {
  return ({ fType, msgList }) => {
    if (msgList) {
      const type = String(fType);
      const text = expandedSet.has(type) ? "收起" : "展开";
      return (
        <Button
          type="link"
          onClick={() => {
            if (expandedSet.has(type)) {
              expandedSet.delete(type);
            } else {
              expandedSet.add(type);
            }
            setExpandedKeys(Array.from(expandedSet));
          }}
        >
          <strong>{text}</strong>
        </Button>
      );
    }
    return (
      <Button
        type="link"
        onClick={() =>
          Modal.success({
            message: "设置点啥",
            description: "我也不知道呀",
          })
        }
      >
        设置
      </Button>
    );
  };
}
 
/**
 * 模拟数据
 */
function getMockRecords() {
  return [
    {
      name: "财务消息",
      displayWeight: 1000,
      msgList: [
        {
          msgType: 213,
          fType: 106,
          name: "账户欠费通知",
          currentChannel: {
            enableSiteMsg: true,
            enableEMail: true,
            enableSms: true,
            enableWX: true,
          },
          receivers:
            "Coder体验账号, lewiszeng, vincehuang, cobipan, powellli, alexpang, asinli, neilsun, barry, hugooliu, ruippan, subAccount_huiping, frostzhao, condichen11, condichen22",
        },
        {
          msgType: 268,
          fType: 106,
          name: "余额预警通知",
          currentChannel: {
            enableSiteMsg: true,
            enableEMail: true,
            enableSms: true,
            enableWX: true,
          },
          receivers:
            "Coder体验账号, lewiszeng, vincehuang, cobipan, frostzhao, condichen, condichen22",
        },
        {
          msgType: 215,
          fType: 106,
          name: "账户提现通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号, lewiszeng, vincehuang, cobipan, alexpang",
        },
        {
          msgType: 310,
          fType: 106,
          name: "账单出账通知",
          currentChannel: {
            enableSiteMsg: true,
            enableEMail: true,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
      ],
      fType: "106",
    },
    {
      name: "产品消息",
      displayWeight: 900,
      msgList: [
        {
          msgType: 217,
          fType: 103,
          name: "产品到期、回收通知",
          currentChannel: {
            enableSiteMsg: true,
            enableEMail: false,
            enableSms: false,
            enableWX: true,
          },
          receivers: "lewiszeng, vincehuang, alexpang, frostzhao",
        },
        {
          msgType: 219,
          fType: 103,
          name: "产品自动续费通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: true,
            enableSms: false,
            enableWX: false,
          },
          receivers: "Coder体验账号, lewiszeng, alexpang",
        },
        {
          msgType: 283,
          fType: 103,
          name: "CDM云数据迁移消息通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers:
            "Coder体验账号, lewiszeng, vincehuang, cobipan, powellli, daniclin, kurukhuang, alexpang, franklan, asinli",
        },
        {
          msgType: 208,
          fType: 103,
          name: "CDN相关通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
        {
          msgType: 294,
          fType: 103,
          name: "产品服务相关通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: false,
            enableWX: true,
          },
          receivers: "Coder体验账号, frostzhao",
        },
        {
          msgType: 267,
          fType: 103,
          name: "HTTPDNS相关通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: true,
            enableSms: false,
            enableWX: false,
          },
          receivers:
            "Coder体验账号, lewiszeng, vincehuang, cobipan, powellli, daniclin, kurukhuang, alexpang",
        },
        {
          msgType: 292,
          fType: 103,
          name: "DNSPod邮件通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: true,
            enableSms: false,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
        {
          msgType: 233,
          fType: 103,
          name: "天御业务安全防护相关通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: true,
            enableSms: false,
            enableWX: false,
          },
          receivers: "Coder体验账号, lewiszeng, alexpang",
        },
        {
          msgType: 269,
          fType: 103,
          name: "事件告警通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: true,
          },
          receivers: "Coder体验账号, frostzhao",
        },
        {
          msgType: 309,
          fType: 103,
          name: "工单服务通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: false,
            enableWX: true,
          },
          receivers: "Coder体验账号, frostzhao",
        },
        {
          msgType: 222,
          fType: 103,
          name: "SSL证书相关通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
        {
          msgType: 232,
          fType: 103,
          name: "官网服务通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: true,
          },
          receivers: "Coder体验账号, frostzhao",
        },
        {
          msgType: 256,
          fType: 103,
          name: "URL安全解决方案",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
        {
          msgType: 264,
          fType: 103,
          name: "渠道服务通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号, kurukhuang",
        },
        {
          msgType: 279,
          fType: 103,
          name: "证书两码发放",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: true,
            enableSms: false,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
        {
          msgType: 302,
          fType: 103,
          name: "云市场服务通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
      ],
      fType: "103",
    },
    {
      name: "安全消息",
      displayWeight: 800,
      msgList: [
        {
          msgType: 207,
          fType: 104,
          name: "安全事件通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: true,
          },
          receivers:
            "Coder体验账号, lewiszeng, vincehuang, cobipan, powellli, daniclin, kurukhuang, alexpang, frostzhao",
        },
        {
          msgType: 303,
          fType: 104,
          name: "内容违规通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: true,
            enableSms: false,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
      ],
      fType: "104",
    },
    {
      name: "腾讯云动态",
      displayWeight: 700,
      msgList: [
        {
          msgType: 305,
          fType: 102,
          name: "云+社区相关通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: false,
            enableWX: true,
          },
          receivers: "Coder体验账号, frostzhao",
        },
        {
          msgType: 211,
          fType: 102,
          name: "活动通知",
          currentChannel: {
            enableSiteMsg: false,
            enableEMail: false,
            enableSms: true,
            enableWX: false,
          },
          receivers: "Coder体验账号",
        },
      ],
      fType: "102",
    },
  ];
}