Skip to content
Open
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
2 changes: 1 addition & 1 deletion addon/components/select-light.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<select
{{on "change" this.change}}
{{on "change" this.onChange}}
...attributes>
{{#if @placeholder}}
<option value="" selected disabled>
Expand Down
21 changes: 17 additions & 4 deletions addon/components/select-light.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import Component from '@glimmer/component';
import { isNone } from '@ember/utils';
import { isNone, isPresent } from '@ember/utils';
import { action } from '@ember/object';
import { deprecate } from '@ember/debug';

const noop = () => {};

export default class extends Component {
constructor() {
super(...arguments);

this.valueKey = this.args.valueKey ?? 'value';
this.displayKey = this.args.displayKey ?? 'label';
this.change = this.args.onChange ?? this.args.change ?? noop;
this.change = this.args.onChange ?? this.args.change;

deprecate(`Triggering @change on <SelectLight /> is deprecated in favor of @onChange due to ember-template-lint's no-passed-in-event-handlers rule`, !this.args.change, {
id: 'ember-select-light.no-passed-in-event-handlers',
Expand All @@ -28,4 +27,18 @@ export default class extends Component {
this.args.options?.[0][this.displayKey],
].some(isNone);
}

@action
onChange(event) {
if (isPresent(this.change)) {
const { args: { options, valueKey }, change, hasDetailedOptions } = this
let selectedOptions = [...event.target.selectedOptions].map(function(option) {
return hasDetailedOptions ? options.findBy(valueKey, option.value) : option.value
})
if (!event.target.multiple) {
selectedOptions = selectedOptions[0]
}
change(selectedOptions, event)
}
}
}