Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Selected Value as First Arg to onChange #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ember-select-light Changelog

### v3.0.0 (January 20, 2021)
- [#30](https://github.com/ember-a11y/ember-select-light/pull/43) [BREAKING] Change event now has value as first param, followed by the full event (thanks rtablada & pzuraq!)

### v2.0.4 (December 3, 2020)
- [#26](https://github.com/ember-a11y/ember-select-light/pull/26) [BUGFIX] Fixes use case where passed in options objects have an empty string as the value or label (thanks javve!)

Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ ember install ember-select-light
@onChange={{this.handleChange}} />
```

`handleChange` should expect the first parameter to be the chosen value(s), the second is the full event, then you can handle it however you wish

#### With [ember-set-helper](https://github.com/pzuraq/ember-set-helper)

A quick and easy to implement option with minimal boilerplate.

```js
class MyComponent extends Component {
@tracked selected = "turtle";

handleChange = (value, event) => {
this.selected = value;
};
}
```

```handlebars
<SelectLight
@value={{this.selected}}
@options={{array "turtle" "tortoise"}}
@change={{set this "selected"}} />
```

#### With an array of objects...

```handlebars
Expand Down
4 changes: 3 additions & 1 deletion addon/components/select-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ const noop = () => {};
export default class extends Component {
constructor() {
super(...arguments);
const changeCallback = this.args.onChange ?? this.args.change ?? noop;

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

this.change = (ev) => changeCallback(ev.target.value, ev);

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 Down
8 changes: 4 additions & 4 deletions tests/integration/components/select-light-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ module('Integration | Component | select-light', function(hooks) {
this.set('myValue', null);

await render(hbs`
<SelectLight @change={{action (mut myValue) value="target.value"}}>
<SelectLight @change={{action (mut myValue)}}>
<option value="turtle">Turtle</option>
</SelectLight>
`);
Expand All @@ -203,7 +203,7 @@ module('Integration | Component | select-light', function(hooks) {
this.set('myValue', null);

await render(hbs`
<SelectLight @onChange={{action (mut myValue) value="target.value"}}>
<SelectLight @onChange={{action (mut myValue)}}>
<option value="turtle">Turtle</option>
</SelectLight>
`);
Expand All @@ -227,7 +227,7 @@ module('Integration | Component | select-light', function(hooks) {
<SelectLight
@options={{this.options}}
@value={{this.value}}
@onChange={{action (mut this.myValue) value="target.value"}} />
@onChange={{action (mut this.myValue)}} />
`);

await fillIn('select', options[0]);
Expand All @@ -242,7 +242,7 @@ module('Integration | Component | select-light', function(hooks) {
this.setProperties({
options,
value: options[1],
customAction: ({ target: { value } }) => {
customAction: (value) => {
assert.step('handled action');
assert.equal(value, options[0]);
},
Expand Down