Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "FocusZone: redoing the \"reset alignment on mouse down\" change in a less intrusive manner, with test coverage.",
"type": "patch"
}
],
"packageName": "office-ui-fabric-react",
"email": "dzearing@microsoft.com"
}
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,91 @@ describe('FocusZone', () => {
expect(lastFocusedElement).toBe(buttonA);
});

it('can reset alignment on mouse down', () => {
const component = ReactTestUtils.renderIntoDocument(
<div { ...{ onFocusCapture: _onFocus } }>
<FocusZone>
<button className='a'>a</button>
<button className='b'>b</button>
<button className='c'>c</button>
<button className='d'>d</button>
</FocusZone>
</div>
);

const focusZone = ReactDOM.findDOMNode(component as React.ReactInstance).firstChild as Element;
const buttonA = focusZone.querySelector('.a') as HTMLElement;
const buttonB = focusZone.querySelector('.b') as HTMLElement;
const buttonC = focusZone.querySelector('.c') as HTMLElement;
const buttonD = focusZone.querySelector('.d') as HTMLElement;

// Set up a grid like so:
// A B
// C D
setupElement(buttonA, {
clientRect: {
top: 0,
bottom: 20,
left: 0,
right: 20
}
});

setupElement(buttonB, {
clientRect: {
top: 0,
bottom: 20,
left: 20,
right: 40
}
});

setupElement(buttonC, {
clientRect: {
top: 20,
bottom: 40,
left: 0,
right: 20
}
});

setupElement(buttonD, {
clientRect: {
top: 20,
bottom: 40,
left: 20,
right: 40
}
});

// Focus the first button.
ReactTestUtils.Simulate.focus(buttonA);
expect(lastFocusedElement).toBe(buttonA);

// Pressing up should stay on a.
ReactTestUtils.Simulate.keyDown(focusZone, { which: KeyCodes.up });
expect(lastFocusedElement).toBe(buttonA);

// Pressing left should stay on a.
ReactTestUtils.Simulate.keyDown(focusZone, { which: KeyCodes.left });
expect(lastFocusedElement).toBe(buttonA);

// Pressing right should go to b.
ReactTestUtils.Simulate.keyDown(focusZone, { which: KeyCodes.right });
expect(lastFocusedElement).toBe(buttonB);

// Pressing down should go to d.
ReactTestUtils.Simulate.keyDown(focusZone, { which: KeyCodes.down });
expect(lastFocusedElement).toBe(buttonD);

// Mousing down on a should reset alignment to a.
ReactTestUtils.Simulate.mouseDown(focusZone, { target: buttonA });

// Pressing down should go to c.
ReactTestUtils.Simulate.keyDown(focusZone, { which: KeyCodes.down });
expect(lastFocusedElement).toBe(buttonC);
});

it('can use arrows bidirectionally with data-no-horizontal-wrap and data-no-vertical-wrap', () => {
const component = ReactTestUtils.renderIntoDocument(
<div { ...{ onFocusCapture: _onFocus } }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class FocusZone extends BaseComponent<IFocusZoneProps, {}> implements IFo
target = path.pop() as HTMLElement;

if (target && isElementTabbable(target)) {
this._setActiveElement(target);
this._setActiveElement(target, true);
}

if (isElementFocusZone(target)) {
Expand All @@ -269,7 +269,7 @@ export class FocusZone extends BaseComponent<IFocusZoneProps, {}> implements IFo
}
}

private _setActiveElement(element: HTMLElement): void {
private _setActiveElement(element: HTMLElement, forceAlignemnt?: boolean): void {
const previousActiveElement = this._activeElement;

this._activeElement = element;
Expand All @@ -283,7 +283,7 @@ export class FocusZone extends BaseComponent<IFocusZoneProps, {}> implements IFo
}

if (this._activeElement) {
if (!this._focusAlignment) {
if (!this._focusAlignment || forceAlignemnt) {
this._setFocusAlignment(element, true, true);
}

Expand Down