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

Switch to yield the select to the block #45

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ and binding aware. It is used in conjuction with the `x-option`
component to construct select boxes. E.g.

```handlebars
{{#x-select value=bob action="selectPerson"}}
{{#x-option value=fred}}Fred Flintstone{{/x-option}}
{{#x-option value=bob}}Bob Newhart{{/x-option}}
{{#x-select value=bob action="selectPerson" as |select|}}
{{#x-option value=fred select=select}}Fred Flintstone{{/x-option}}
{{#x-option value=bob select=select}}Bob Newhart{{/x-option}}
{{/x-select}}
```

Expand All @@ -40,10 +40,10 @@ option. This means you can pass an array as its value, and it will set
its selections directly on that array.

```handlebars
{{#x-select value=selections multiple=true action="selectionsChanged"}}
{{#x-option value=fred}}Fred Flintstone{{/x-option}}
{{#x-option value=bob}}Bob Newhart{{/x-option}}
{{#x-option value=andrew}}Andrew WK{{/x-option}}
{{#x-select value=selections multiple=true action="selectionsChanged" as |select|}}
{{#x-option value=fred select=select}}Fred Flintstone{{/x-option}}
{{#x-option value=bob select=select}}Bob Newhart{{/x-option}}
{{#x-option value=andrew select=select}}Andrew WK{{/x-option}}
{{/x-select}}
```

Expand Down Expand Up @@ -134,9 +134,9 @@ cases, you can associate arbitrary attributes with the component
itself and use them later inside your action handler. For example:

```handlebars
{{#x-select action="didMakeSelection" default=anything}}
{{#x-select action="didMakeSelection" default=anything as |select|}}
<option>Nothing</option>
{{#x-option value=something}}Something{{/x-option}}
{{#x-option value=something select=select}}Something{{/x-option}}
{{/x-select}}
```
then, inside your action handler:
Expand Down
13 changes: 11 additions & 2 deletions addon/components/x-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export default Ember.Component.extend({
*/
value: null,

/**
* The value of the parent `x-select`
*
* @property x-select
* @type XSelectComponent
* @default null
*/
select: null,

/**
* Property bound to the `selected` attribute of the native
* `<option>` element. It is aware of the containing `x-select`'s
Expand Down Expand Up @@ -55,9 +64,9 @@ export default Ember.Component.extend({
this._super.apply(this, arguments);

Ember.run.scheduleOnce('afterRender', () => {
var select = this.nearestOfType(XSelectComponent);
/* Use an assigned select, or search for one nearby */
var select = this.get('select') || this.nearestOfType(XSelectComponent);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still check for nearestOfType to try to avoid breaking anyone that hasn't upgrade to a version of ember that breaks it anyway.

Ember.assert("x-option component declared without enclosing x-select", !!select);
this.set('select', select);
select.registerOption(this);
});
},
Expand Down
4 changes: 2 additions & 2 deletions app/templates/components/x-select.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{#if hasBlock}}
{{yield}}
{{yield this}}
{{else}}
{{#if placeholder}}
<option>{{placeholder}}</option>
{{/if}}
{{#each _optionValues as |option|}}
{{#x-option value=option.value}}{{option.label}}{{/x-option}}
{{#x-option value=option.value select=this}}{{option.label}}{{/x-option}}
{{/each}}
{{/if}}
8 changes: 4 additions & 4 deletions tests/dummy/app/templates/multiple.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p>
{{#x-select value=selections action="selectionsChanged" disabled=isDisabled multiple=true title=title required=isRequired
autofocus=hasAutofocus name=attrName form=attrForm size=attrSize}}
{{#x-option value=charles}}Charles{{/x-option}}
{{#x-option value=bastion}}Bastion{{/x-option}}
{{#x-option value=stanley}}Stanley{{/x-option}}
autofocus=hasAutofocus name=attrName form=attrForm size=attrSize as |select|}}
{{#x-option value=charles select=select}}Charles{{/x-option}}
{{#x-option value=bastion select=select}}Bastion{{/x-option}}
{{#x-option value=stanley select=select}}Stanley{{/x-option}}
<option>Nobody</option>
{{/x-select}}
</p>
Expand Down
8 changes: 4 additions & 4 deletions tests/dummy/app/templates/single.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p>
{{#x-select value=it action="tagYouAreIt" disabled=isDisabled title=title required=isRequired
autofocus=hasAutofocus name=attrName form=attrForm size=attrSize}}
{{#x-option value=charles}}Charles{{/x-option}}
{{#x-option value=bastion}}Bastion{{/x-option}}
{{#x-option value=stanley}}Stanley{{/x-option}}
autofocus=hasAutofocus name=attrName form=attrForm size=attrSize as |select|}}
{{#x-option value=charles select=select}}Charles{{/x-option}}
{{#x-option value=bastion select=select}}Bastion{{/x-option}}
{{#x-option value=stanley select=select}}Stanley{{/x-option}}
<option>Nobody</option>
{{/x-select}}
</p>
Expand Down