2016-10-12 37 views
2

難以弄清楚爲什麼Material-UI會爲其Popover設置一個等於它自己的變量。在Material-UI中,他們爲什麼要將這個變量設置爲自己?

下面是代碼片段檢查返回前的兩個塊。

設置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, 
    }; 
    } 
+0

如果'vertical'是一個具有getter/setter邏輯的屬性,那麼該賦值可能有副作用。 – Dai

+0

我用'Object.getOwnPropertyDescriptor(obj,「foo」)。get;'來檢查 - 看起來不像getter/setter。 – JordanHendrix

回答

0

看起來像一個錯字。

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

它看起來像一個'包容性'的重疊意味着錨和目標是相同的。我最好的猜測是,原來的貢獻者,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 
    } 
} 

錨剛剛更新,因此如果重疊是包容的,這將保持錨和目標同步。

相關問題