Skip to content

Commit

Permalink
refactor(select): make selects events use Event rather than subclassi…
Browse files Browse the repository at this point in the history
…ng it

PiperOrigin-RevId: 553583222
  • Loading branch information
Elliott Marquez authored and copybara-github committed Aug 3, 2023
1 parent ff60a88 commit 98daf66
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
8 changes: 5 additions & 3 deletions select/internal/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {DEFAULT_TYPEAHEAD_BUFFER_TIME, Menu} from '../../menu/internal/menu.js';
import {CloseMenuEvent, isElementInSubtree, isSelectableKey} from '../../menu/internal/shared.js';
import {TYPEAHEAD_RECORD} from '../../menu/internal/typeaheadController.js';

import {getSelectedItems, RequestDeselectionEvent, RequestSelectionEvent, SelectOption, SelectOptionRecord} from './shared.js';
import {createRequestDeselectionEvent, createRequestSelectionEvent, getSelectedItems, SelectOption, SelectOptionRecord} from './shared.js';

const VALUE = Symbol('value');

Expand Down Expand Up @@ -488,7 +488,8 @@ export abstract class Select extends LitElement {
* Handles updating selection when an option element requests selection via
* property / attribute change.
*/
private handleRequestSelection(event: RequestSelectionEvent) {
private handleRequestSelection(
event: ReturnType<typeof createRequestSelectionEvent>) {
const requestingOptionEl = event.target as SelectOption & HTMLElement;

// No-op if this item is already selected.
Expand All @@ -504,7 +505,8 @@ export abstract class Select extends LitElement {
* Handles updating selection when an option element requests deselection via
* property / attribute change.
*/
private handleRequestDeselection(event: RequestDeselectionEvent) {
private handleRequestDeselection(
event: ReturnType<typeof createRequestDeselectionEvent>) {
const requestingOptionEl = event.target as SelectOption & HTMLElement;

// No-op if this item is not even in the list of tracked selected items.
Expand Down
17 changes: 8 additions & 9 deletions select/internal/selectoption/select-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import {PropertyValues} from 'lit';
import {property} from 'lit/decorators.js';

import {ListItemRole, MenuItemEl} from '../../../menu/internal/menuitem/menu-item.js';
import {RequestDeselectionEvent, RequestSelectionEvent, SelectOption} from '../shared.js';
import {createRequestDeselectionEvent, createRequestSelectionEvent, SelectOption} from '../shared.js';

/**
* @fires close-menu {CloseMenuEvent} Closes the encapsulating menu on
* @fires request-selection {RequestSelectionEvent} Requests the parent
* md-select to select this element (and deselect others if single-selection)
* when `selected` changed to `true`.
* @fires request-deselection {RequestDeselectionEvent} Requests the parent
* md-select to deselect this element when `selected` changed to `false`.
* @fires close-menu Closes the encapsulating menu on
* @fires request-selection Requests the parent md-select to select this element
* (and deselect others if single-selection) when `selected` changed to `true`.
* @fires request-deselection Requests the parent md-select to deselect this
* element when `selected` changed to `false`.
*/
export class SelectOptionEl extends MenuItemEl implements SelectOption {
/**
Expand Down Expand Up @@ -60,9 +59,9 @@ export class SelectOptionEl extends MenuItemEl implements SelectOption {
// handled by md-select because it needs to coordinate the
// single-selection behavior.
if (this.selected) {
this.dispatchEvent(new RequestSelectionEvent());
this.dispatchEvent(createRequestSelectionEvent());
} else {
this.dispatchEvent(new RequestDeselectionEvent());
this.dispatchEvent(createRequestDeselectionEvent());
}
}
}
Expand Down
26 changes: 14 additions & 12 deletions select/internal/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type SelectOptionRecord = [SelectOption, number];
/**
* Given a list of select options, this function will return an array of
* SelectOptionRecords that are selected.
*
*
* @return An array of SelectOptionRecords describing the options that are
* selected.
*/
Expand All @@ -53,21 +53,23 @@ export function getSelectedItems(items: SelectOption[]) {
}

/**
* An event fired by a SelectOption to request selection from md-select.
* Creates an event fired by a SelectOption to request selection from md-select.
* Typically fired after `selected` changes from `false` to `true`.
*/
export class RequestSelectionEvent extends Event {
constructor() {
super('request-selection', {bubbles: true, composed: true});
}
export function createRequestSelectionEvent() {
return new Event('request-selection', {
bubbles: true,
composed: true,
});
}

/**
* An event fired by a SelectOption to request deselection from md-select.
* Typically fired after `selected` changes from `true` to `false`.
* Creates an event fired by a SelectOption to request deselection from
* md-select. Typically fired after `selected` changes from `true` to `false`.
*/
export class RequestDeselectionEvent extends Event {
constructor() {
super('request-deselection', {bubbles: true, composed: true});
}
export function createRequestDeselectionEvent() {
return new Event('request-deselection', {
bubbles: true,
composed: true,
});
}

0 comments on commit 98daf66

Please sign in to comment.