Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
fix(select): add custom selected event for select component (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
annawen1 committed Jul 8, 2021
1 parent dec2a17 commit 2a4e7f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/select/select-story.ts
Expand Up @@ -69,7 +69,7 @@ export const Default = args => {
size="${ifNonNull(size)}"
validity-message="${ifNonNull(validityMessage)}"
value="${ifNonNull(value)}"
@input="${ifNonNull(onInput)}"
@bx-select-selected="${ifNonNull(onInput)}"
>
${children}
</bx-select>
Expand All @@ -88,7 +88,7 @@ Default.parameters = {
size: select('Dropdown size (size)', sizes, null),
validityMessage: textNullable('The validity message (validity-message)', ''),
value: textNullable('The value of the selected item (value)', ''),
onInput: action('input'),
onInput: action('bx-select-selected'),
}),
},
};
Expand Down
20 changes: 19 additions & 1 deletion src/components/select/select.ts
Expand Up @@ -57,7 +57,18 @@ class BXSelect extends ValidityMixin(FormMixin(LitElement)) {
* @param event The event.
*/
private _handleInput({ target }: Event) {
this.value = (target as HTMLSelectElement).value;
const { value } = target as HTMLSelectElement;
this.value = value;
const { eventSelect } = this.constructor as typeof BXSelect;
this.dispatchEvent(
new CustomEvent(eventSelect, {
bubbles: true,
composed: true,
detail: {
value,
},
})
);
}

/**
Expand Down Expand Up @@ -379,6 +390,13 @@ class BXSelect extends ValidityMixin(FormMixin(LitElement)) {
return `${prefix}-select-item`;
}

/**
* The name of the custom event fired after item is selected.
*/
static get eventSelect() {
return `${prefix}-select-selected`;
}

static styles = styles; // `styles` here is a `CSSResult` generated by custom WebPack loader
}

Expand Down

0 comments on commit 2a4e7f0

Please sign in to comment.