All files / src/popover/_example PopoverPositionExample.jsx

24.14% Statements 7/29
0% Branches 0/8
9.09% Functions 1/11
22.22% Lines 6/27

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                1x 1x 101x       1x   1x                                                                                                                                                   1x                                                                                              
import React, { useState, Fragment } from "react";
import { Text } from "@tencent/tea-component/lib/text";
import { Button } from "@tencent/tea-component/lib/button";
import { Modal } from "@tencent/tea-component/lib/modal";
import { Table } from "@tencent/tea-component/lib/table";
import { Bubble } from "@tencent/tea-component/lib/bubble";
import { scrollable } from "@tencent/tea-component/lib/table/addons/scrollable";
 
const naturalNumbers = [];
for (let i = 0; i <= 100; i++) {
  naturalNumbers.push(i);
}
 
export default function PopoverPositionExample() {
  const [modalVisible, setModalVisible] = useState(false);
 
  return (
    <Fragment>
      <Button onClick={() => setModalVisible(true)}>打开信息表</Button>
      <Modal visible={modalVisible} onClose={() => setModalVisible(false)}>
        <Table
          columns={[
            { key: "number", header: "自然数", render: x => x },
            {
              key: "square",
              header: "平方",
              render: x => (
                <Bubble closeOnScroll content={`${x} * ${x} = ${x * x}`}>
                  {x * x}
                </Bubble>
              ),
            },
            {
              key: "odd",
              header: "奇偶性",
              render: x => <OddProperty x={x} />,
            },
            {
              key: "prime",
              header: "质合性",
              render: x => <PrimeProperty x={x} />,
            },
          ]}
          records={naturalNumbers}
          addons={[scrollable({ maxHeight: 400 })]}
        />
      </Modal>
    </Fragment>
  );
}
 
/**
 * Tooltip 是基于 Popover 来的,所以可以用 Tooltip 测试 Popover
 */
function OddProperty({ x }) {
  if (x % 2) {
    return (
      <Text tooltip={`${x} 是奇数,因为它不能被 2 整除`} overflow>
        奇数
      </Text>
    );
  }
  return (
    <Text tooltip={`${x} 是偶数,因为它能被 2 整除`} overflow>
      偶数
    </Text>
  );
}
 
function PrimeProperty({ x }) {
  if (isPrime(x)) {
    return (
      <Text
        tooltip={`${x} 是素数,因为它不能被除了 1 和它自身的任何自然数整除`}
        overflow
      >
        素数
      </Text>
    );
  }
  return (
    <Text
      tooltip={`${x} 是合数,因为它能被 ${getFactors(x).join(", ")} 整除`}
      overflow
    >
      合数
    </Text>
  );
}
 
const primeNumbers = new Set([
  2,
  3,
  5,
  7,
  11,
  13,
  17,
  19,
  23,
  29,
  31,
  37,
  41,
  43,
  47,
  53,
  57,
  59,
  61,
  67,
  71,
  73,
  77,
  83,
  89,
  91,
  97,
]);
 
function isPrime(num) {
  return primeNumbers.has(num);
}
 
function getFactors(num) {
  const factors = [];
  for (let i = 2; i <= num / 2; i++) {
    if (num % i === 0) {
      factors.push(i);
      if (i * i !== num) {
        factors.push(num / i);
      }
    }
  }
  factors.sort();
  return factors;
}