Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 8286aa7

Browse files
author
Matt Goo
authored
fix(select): Fix enhanced select issue where it does not stay open on long press #4173 (#4590)
1 parent 0e9f3f5 commit 8286aa7

File tree

3 files changed

+28
-70
lines changed

3 files changed

+28
-70
lines changed

packages/mdc-select/component.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ import {MDCSelectHelperText, MDCSelectHelperTextFactory} from './helper-text/ind
3737
import {MDCSelectIcon, MDCSelectIconFactory} from './icon/index';
3838
import {MDCSelectEventDetail, MDCSelectFoundationMap} from './types';
3939

40-
type PointerEventType = 'mousedown' | 'touchstart';
41-
42-
const POINTER_EVENTS: PointerEventType[] = ['mousedown', 'touchstart'];
4340
const VALIDATION_ATTR_WHITELIST = ['required', 'aria-required'];
4441

4542
export class MDCSelect extends MDCComponent<MDCSelectFoundation> implements MDCRippleCapableSurface {
@@ -71,7 +68,7 @@ export class MDCSelect extends MDCComponent<MDCSelectFoundation> implements MDCR
7168
private handleChange_!: SpecificEventListener<'change'>; // assigned in initialize()
7269
private handleFocus_!: SpecificEventListener<'focus'>; // assigned in initialize()
7370
private handleBlur_!: SpecificEventListener<'blur'>; // assigned in initialize()
74-
private handleClick_!: SpecificEventListener<PointerEventType>; // assigned in initialize()
71+
private handleClick_!: SpecificEventListener<'click'>; // assigned in initialize()
7572
private handleKeydown_!: SpecificEventListener<'keydown'>; // assigned in initialize()
7673
private handleMenuOpened_!: EventListener; // assigned in initialize()
7774
private handleMenuClosed_!: EventListener; // assigned in initialize()
@@ -179,9 +176,7 @@ export class MDCSelect extends MDCComponent<MDCSelectFoundation> implements MDCR
179176
this.targetElement_.addEventListener('focus', this.handleFocus_);
180177
this.targetElement_.addEventListener('blur', this.handleBlur_);
181178

182-
POINTER_EVENTS.forEach((evtType) => {
183-
this.targetElement_.addEventListener(evtType, this.handleClick_ as EventListener);
184-
});
179+
this.targetElement_.addEventListener('click', this.handleClick_ as EventListener);
185180

186181
if (this.menuElement_) {
187182
this.selectedText_!.addEventListener('keydown', this.handleKeydown_);
@@ -215,9 +210,7 @@ export class MDCSelect extends MDCComponent<MDCSelectFoundation> implements MDCR
215210
this.targetElement_.removeEventListener('focus', this.handleFocus_);
216211
this.targetElement_.removeEventListener('blur', this.handleBlur_);
217212
this.targetElement_.removeEventListener('keydown', this.handleKeydown_);
218-
POINTER_EVENTS.forEach((evtType) => {
219-
this.targetElement_.removeEventListener(evtType, this.handleClick_ as EventListener);
220-
});
213+
this.targetElement_.removeEventListener('click', this.handleClick_ as EventListener);
221214

222215
if (this.menu_) {
223216
this.menu_.unlisten(menuSurfaceConstants.strings.CLOSED_EVENT, this.handleMenuClosed_);

test/unit/mdc-select/mdc-select-enhanced.test.js

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -984,43 +984,29 @@ test(`#destroy removes the listener for the ${MDCMenuFoundation.strings.SELECTED
984984
document.body.removeChild(fixture);
985985
});
986986

987-
test('#destroy removes the mousedown listener', () => {
988-
const {bottomLine, component, fixture} = setupTest();
989-
const event = document.createEvent('MouseEvent');
987+
test('#destroy removes the click listener', () => {
988+
const {component, selectedText} = setupTest();
990989
const clientX = 200;
991-
const clientY = 200;
992-
990+
component.foundation_.handleClick = td.func();
993991
component.destroy();
994-
// IE11 mousedown event.
995-
event.initMouseEvent('mousedown', true, true, window, 0, 0, 0, clientX, clientY, false, false, false, false, 0, null);
996-
fixture.querySelector('.mdc-select__selected-text').dispatchEvent(event);
997-
998-
td.verify(bottomLine.setRippleCenter(200), {times: 0});
992+
domEvents.emit(selectedText, 'click', {clientX});
993+
td.verify(component.foundation_.handleClick(200), {times: 0});
999994
});
1000995

1001-
test('mousedown on the select sets the line ripple origin', () => {
1002-
const {bottomLine, fixture} = setupTest();
1003-
const event = document.createEvent('MouseEvent');
996+
test('click on the selectedText calls foundation.handleClick()', () => {
997+
const {component, selectedText} = setupTest();
1004998
const clientX = 200;
1005-
const clientY = 200;
1006-
// IE11 mousedown event.
1007-
event.initMouseEvent('mousedown', true, true, window, 0, 0, 0, clientX, clientY, false, false, false, false, 0, null);
1008-
fixture.querySelector('.mdc-select__selected-text').dispatchEvent(event);
1009-
1010-
td.verify(bottomLine.setRippleCenter(200), {times: 1});
999+
component.foundation_.handleClick = td.func();
1000+
domEvents.emit(selectedText, 'click', {clientX});
1001+
td.verify(component.foundation_.handleClick(200), {times: 1});
10111002
});
10121003

1013-
test('mousedown on the select does nothing if the it does not have a lineRipple', () => {
1014-
const hasOutline = true;
1015-
const {bottomLine, fixture} = setupTest(hasOutline);
1016-
const event = document.createEvent('MouseEvent');
1004+
test('click on the selectedText focuses on the selectedText element', () => {
1005+
const {selectedText} = setupTest();
10171006
const clientX = 200;
1018-
const clientY = 200;
1019-
// IE11 mousedown event.
1020-
event.initMouseEvent('mousedown', true, true, window, 0, 0, 0, clientX, clientY, false, false, false, false, 0, null);
1021-
fixture.querySelector('.mdc-select__selected-text').dispatchEvent(event);
1022-
1023-
td.verify(bottomLine.setRippleCenter(200), {times: 0});
1007+
selectedText.focus = td.func();
1008+
domEvents.emit(selectedText, 'click', {clientX});
1009+
td.verify(selectedText.focus(), {times: 1});
10241010
});
10251011

10261012
test('menu surface opened event causes the first element (if not element is selected) to be focused', () => {

test/unit/mdc-select/mdc-select.test.js

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -577,43 +577,22 @@ test('#destroy removes the blur handler', () => {
577577
td.verify(component.foundation_.handleBlur(), {times: 0});
578578
});
579579

580-
test('mousedown on the select sets the line ripple origin', () => {
581-
const {bottomLine, fixture} = setupTest();
582-
const event = document.createEvent('MouseEvent');
580+
test('click on the select calls foundation.handleClick()', () => {
581+
const {component, nativeControl} = setupTest();
583582
const clientX = 200;
584-
const clientY = 200;
585-
// IE11 mousedown event.
586-
event.initMouseEvent('mousedown', true, true, window, 0, 0, 0, clientX, clientY, false, false, false, false, 0, null);
587-
fixture.querySelector('select').dispatchEvent(event);
588-
589-
td.verify(bottomLine.setRippleCenter(200), {times: 1});
583+
component.foundation_.handleClick = td.func();
584+
domEvents.emit(nativeControl, 'click', {clientX});
585+
td.verify(component.foundation_.handleClick(200), {times: 1});
590586
});
591587

592-
test('mousedown on the select does nothing if the it does not have a lineRipple', () => {
593-
const hasOutline = true;
594-
const {bottomLine, fixture} = setupTest(hasOutline);
595-
const event = document.createEvent('MouseEvent');
596-
const clientX = 200;
597-
const clientY = 200;
598-
// IE11 mousedown event.
599-
event.initMouseEvent('mousedown', true, true, window, 0, 0, 0, clientX, clientY, false, false, false, false, 0, null);
600-
fixture.querySelector('select').dispatchEvent(event);
601588

602-
td.verify(bottomLine.setRippleCenter(200), {times: 0});
603-
});
604-
605-
test('#destroy removes the mousedown listener', () => {
606-
const {bottomLine, component, fixture} = setupTest();
607-
const event = document.createEvent('MouseEvent');
589+
test('#destroy removes the click listener', () => {
590+
const {component, nativeControl} = setupTest();
608591
const clientX = 200;
609-
const clientY = 200;
610-
592+
component.foundation_.handleClick = td.func();
611593
component.destroy();
612-
// IE11 mousedown event.
613-
event.initMouseEvent('mousedown', true, true, window, 0, 0, 0, clientX, clientY, false, false, false, false, 0, null);
614-
fixture.querySelector('select').dispatchEvent(event);
615-
616-
td.verify(bottomLine.setRippleCenter(200), {times: 0});
594+
domEvents.emit(nativeControl, 'click', {clientX});
595+
td.verify(component.foundation_.handleClick(200), {times: 0});
617596
});
618597

619598
test('keydown is not added to the native select when initialized', () => {

0 commit comments

Comments
 (0)