在Material-UI中,为什么要将这个变量设置为自身

In Material-UI why do they set this variable to itself?

本文关键字:变量 设置 Material-UI 为什么      更新时间:2023-09-26

不明白为什么Material-UI会为它的Popover设置一个等于它自己的变量

下面是检查返回前两个if块的代码片段:

设置t.vertical = t.vertical;的目的是什么?

getPositions(anchor, target) {
    const a = {...anchor};
    const t = {...target};
    const positions = {
      x: ['left', 'right'].filter((p) => p !== t.horizontal),
      y: ['top', 'bottom'].filter((p) => p !== t.vertical),
    };
    const overlap = {
      x: this.getOverlapMode(a.horizontal, t.horizontal, 'middle'),
      y: this.getOverlapMode(a.vertical, t.vertical, 'center'),
    };
    positions.x.splice(overlap.x === 'auto' ? 0 : 1, 0, 'middle');
    positions.y.splice(overlap.y === 'auto' ? 0 : 1, 0, 'center');
    if (overlap.y !== 'auto') {
      a.vertical = a.vertical === 'top' ? 'bottom' : 'top';
      if (overlap.y === 'inclusive') {
        t.vertical = t.vertical; // HERE
      }
    }
    if (overlap.x !== 'auto') {
      a.horizontal = a.horizontal === 'left' ? 'right' : 'left';
      if (overlap.y === 'inclusive') {
        t.horizontal = t.horizontal; // HERE
      }
    }
    return {
      positions: positions,
      anchorPos: a,
    };
  }

好像是打错了。

getOverlapMode(anchor, target, median) {
  if ([anchor, target].indexOf(median) >= 0) return 'auto';
  if (anchor === target) return 'inclusive';
  return 'exclusive';
}

看起来'inclusive'的重叠意味着锚点和目标是相同的。我最可能的猜测是,最初的贡献者chrismcv打算这样做:

if (overlap.y !== 'auto') {
  a.vertical = a.vertical === 'top' ? 'bottom' : 'top';
  if (overlap.y === 'inclusive') {
    t.vertical = a.vertical; // update target to match anchor
  }
}
if (overlap.x !== 'auto') {
  a.horizontal = a.horizontal === 'left' ? 'right' : 'left';
  if (overlap.y === 'inclusive') {
    t.horizontal = a.horizontal; // update target to match anchor
  }
}

锚点刚刚更新,所以如果包含重叠,这将使锚点和目标保持同步。