Skip to content

Instantly share code, notes, and snippets.

@Bestra
Last active July 3, 2019 19:51
Show Gist options
  • Save Bestra/52f53a0913cc4d143ae1453a466bc2ac to your computer and use it in GitHub Desktop.
Save Bestra/52f53a0913cc4d143ae1453a466bc2ac to your computer and use it in GitHub Desktop.
Accordion list
// components/accordion-item.js
import Ember from 'ember';
export default Ember.Component.extend({
item: null,
activeItem: null,
isExpanded: Ember.computed('activeItem', 'item', function() {
return this.get('activeItem') === this.get('item');
})
});
// components/accordion-list.js
import Ember from 'ember';
export default Ember.Component.extend({
items: null,
activeItem: null,
actions: {
toggleActiveItem(item) {
if (this.get('activeItem') !== item) {
this.set('activeItem', item);
} else {
this.set('activeItem', null);
}
}
}
});
// controllers/application.js
import Ember from 'ember';
export default Ember.Controller.extend({
items: Ember.computed(function() {
return [
{title: "Item 1", details: "Details here"},
{title: "Item 2", details: "Details here"},
{title: "Item 3", details: "Details here"}
]
})
});
+---------------------------------+
| accordion list |
+---+------------------+----------+
^ |
| | item, expandedItem
toggleExpanded |
| |
| +---------v----------+
| | accordion-item |
+--------+ |
+--------------------+
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.header {
background: #eee;
padding: 10px;
}
.details {
padding: 10px 10px 20px 10px;
}
<!-- application.hbs -->
<h1>Accordion!</h1>
{{accordion-list items=items}}
<br>
<br>
<!-- components/accordion-item.hbs -->
<div class='header' {{action headerClicked}}>{{item.title}} (click to {{if isExpanded 'close' 'open'}})</div>
{{#if isExpanded}}
<div class='details'>{{item.details}}</div>
{{/if}}
<!-- components/accordion-list.hbs -->
<div>
{{#each items as |item|}}
{{accordion-item
item=item
activeItem=activeItem
headerClicked=(action 'toggleActiveItem' item)}}
{{/each}}
</div>
{
"version": "0.10.4",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.7.0",
"ember-data": "2.7.0",
"ember-template-compiler": "2.7.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment