Skip to content

Commit

Permalink
Dropdown: do not select first option automatically unless it's select…
Browse files Browse the repository at this point in the history
…ed using keyboard navigation (#10755)

* do not select first option by default for non-aria case

* update snapshots

* code cleanup

* yarn change

* add Aria doc link inline

* resolve comment

* simplify to use one class vars rather than two

* resolve comments
  • Loading branch information
xugao authored and msft-github-bot committed Oct 9, 2019
1 parent 25b3c4b commit 31396ed
Show file tree
Hide file tree
Showing 25 changed files with 68 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "patch",
"comment": "Fixed dropdown to not select first option automatically unless it's selected using keyboard navigation per aria",
"packageName": "office-ui-fabric-react",
"email": "xgao@microsoft.com",
"commit": "43f40feb7596224a82cecf98f48121567c883783",
"date": "2019-10-09T04:55:21.494Z",
"file": "/Users/xugao/Projects/office-ui-fabric-react/change/office-ui-fabric-react-2019-10-08-21-55-21-dropdown-fix.json"
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ export class DropdownBase extends React.Component<IDropdownInternalProps, IDropd
private _sizePosCache: DropdownSizePosCache = new DropdownSizePosCache();
private _classNames: IProcessedStyleSet<IDropdownStyles>;
private _requestAnimationFrame = safeRequestAnimationFrame(this);

/** Flag for when we get the first mouseMove */
private _gotMouseMove: boolean;

/** Flag for identifiying dropdown is opened by getting focus using keyboard */
private _isOpenedByKeyboardFocus: boolean;
/** Flag for tracking whether focus is triggered by click (alternatively triggered by keyboard nav) */
private _isFocusedByClick: boolean;

constructor(props: IDropdownProps) {
super(props);
Expand Down Expand Up @@ -279,6 +277,7 @@ export class DropdownBase extends React.Component<IDropdownInternalProps, IDropd
onKeyDown={this._onDropdownKeyDown}
onKeyUp={this._onDropdownKeyUp}
onClick={this._onDropdownClick}
onMouseDown={this._onDropdownMouseDown}
onFocus={this._onFocus}
>
<span
Expand Down Expand Up @@ -1036,33 +1035,36 @@ export class DropdownBase extends React.Component<IDropdownInternalProps, IDropd
const { isOpen } = this.state;
const disabled = this._isDisabled();

if (!disabled && !(this._isOpenedByKeyboardFocus && isOpen)) {
if (!disabled && !this._shouldOpenOnFocus()) {
this.setState({
isOpen: !isOpen
});
}

this._isOpenedByKeyboardFocus = false;
this._isFocusedByClick = false; // reset
};

private _onDropdownMouseDown = (): void => {
this._isFocusedByClick = true;
};

private _onFocus = (ev: React.FocusEvent<HTMLDivElement>): void => {
const { isOpen, selectedIndices, hasFocus } = this.state;
const { multiSelect, openOnKeyboardFocus } = this.props;
const { isOpen, selectedIndices } = this.state;
const { multiSelect } = this.props;

const disabled = this._isDisabled();

if (!disabled) {
if (!isOpen && selectedIndices.length === 0 && !multiSelect) {
// Per aria
if (!this._isFocusedByClick && !isOpen && selectedIndices.length === 0 && !multiSelect) {
// Per aria: https://www.w3.org/TR/wai-aria-practices-1.1/#listbox_kbd_interaction
this._moveIndex(ev, 1, 0, -1);
}
if (this.props.onFocus) {
this.props.onFocus(ev);
}
const state: Pick<IDropdownState, 'hasFocus'> | Pick<IDropdownState, 'hasFocus' | 'isOpen'> = { hasFocus: true };
if (openOnKeyboardFocus && !hasFocus) {
if (this._shouldOpenOnFocus()) {
(state as Pick<IDropdownState, 'hasFocus' | 'isOpen'>).isOpen = true;
this._isOpenedByKeyboardFocus = true;
}

this.setState(state);
Expand Down Expand Up @@ -1106,4 +1108,14 @@ export class DropdownBase extends React.Component<IDropdownInternalProps, IDropd
</Label>
) : null;
};

/**
* Returns true if dropdown should set to open on focus.
* Otherwise, isOpen state should be toggled on click
*/
private _shouldOpenOnFocus(): boolean {
const { hasFocus } = this.state;
const { openOnKeyboardFocus } = this.props;
return !this._isFocusedByClick && openOnKeyboardFocus === true && !hasFocus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ describe('Dropdown', () => {
});

it('opens on click if openOnKeyboardFocus is true', () => {
wrapper = mount(<Dropdown openOnKeyboardFocus={true} label="testgroup" options={DEFAULT_OPTIONS} />);
wrapper = mount(<Dropdown openOnKeyboardFocus label="testgroup" options={DEFAULT_OPTIONS} />);

wrapper.find('.ms-Dropdown').simulate('mousedown');
wrapper.find('.ms-Dropdown').simulate('click');

const secondItemElement = document.querySelector('.ms-Dropdown-item[data-index="2"]') as HTMLElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ exports[`Dropdown multi-select Renders multiselect Dropdown correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
tabIndex={0}
>
<span
Expand Down Expand Up @@ -272,6 +273,7 @@ exports[`Dropdown single-select Renders single-select Dropdown correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ exports[`Component Examples renders Callout.Cover.Example.tsx correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ exports[`Component Examples renders Callout.Directional.Example.tsx correctly 1`
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ exports[`Component Examples renders ChoiceGroup.Custom.Example.tsx correctly 1`]
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
tabIndex={-1}
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ exports[`Component Examples renders Coachmark.Basic.Example.tsx correctly 1`] =
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ exports[`Component Examples renders ContextualMenu.Directional.Example.tsx corre
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ exports[`Component Examples renders DatePicker.Basic.Example.tsx correctly 1`] =
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ exports[`Component Examples renders DatePicker.WeekNumbers.Example.tsx correctly
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ exports[`Component Examples renders Dropdown.Basic.Example.tsx correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -346,6 +347,7 @@ exports[`Component Examples renders Dropdown.Basic.Example.tsx correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
tabIndex={-1}
>
<span
Expand Down Expand Up @@ -559,6 +561,7 @@ exports[`Component Examples renders Dropdown.Basic.Example.tsx correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
tabIndex={0}
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ exports[`Component Examples renders Dropdown.Controlled.Example.tsx correctly 1`
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ exports[`Component Examples renders Dropdown.ControlledMulti.Example.tsx correct
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
tabIndex={0}
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ exports[`Component Examples renders Dropdown.Custom.Example.tsx correctly 1`] =
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -515,6 +516,7 @@ exports[`Component Examples renders Dropdown.Custom.Example.tsx correctly 1`] =
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ exports[`Component Examples renders Dropdown.Error.Example.tsx correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ exports[`Component Examples renders Dropdown.Required.Example.tsx correctly 1`]
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -502,6 +503,7 @@ exports[`Component Examples renders Dropdown.Required.Example.tsx correctly 1`]
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ exports[`Component Examples renders Facepile.Basic.Example.tsx correctly 1`] = `
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@ exports[`Component Examples renders Facepile.Overflow.Example.tsx correctly 1`]
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ exports[`Component Examples renders PeoplePicker.Types.Example.tsx correctly 1`]
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4066,6 +4066,7 @@ exports[`Component Examples renders ResizeGroup.OverflowSet.Example.tsx correctl
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3122,6 +3122,7 @@ exports[`Component Examples renders Stack.Horizontal.Configure.Example.tsx corre
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -3347,6 +3348,7 @@ exports[`Component Examples renders Stack.Horizontal.Configure.Example.tsx corre
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ exports[`Component Examples renders Stack.Horizontal.WrapAdvanced.Example.tsx co
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -988,6 +989,7 @@ exports[`Component Examples renders Stack.Horizontal.WrapAdvanced.Example.tsx co
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -1214,6 +1216,7 @@ exports[`Component Examples renders Stack.Horizontal.WrapAdvanced.Example.tsx co
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,7 @@ exports[`Component Examples renders Stack.Vertical.Configure.Example.tsx correct
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -2059,6 +2060,7 @@ exports[`Component Examples renders Stack.Vertical.Configure.Example.tsx correct
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ exports[`Component Examples renders Stack.Vertical.WrapAdvanced.Example.tsx corr
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -988,6 +989,7 @@ exports[`Component Examples renders Stack.Vertical.WrapAdvanced.Example.tsx corr
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down Expand Up @@ -1214,6 +1216,7 @@ exports[`Component Examples renders Stack.Vertical.WrapAdvanced.Example.tsx corr
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
role="listbox"
tabIndex={0}
>
Expand Down

0 comments on commit 31396ed

Please sign in to comment.