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

fix(select): add custom selected event for select component #667

Merged
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
4 changes: 2 additions & 2 deletions src/components/select/select-story.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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