Created
July 15, 2020 08:00
-
-
Save chancancode/9fc546ae6e66b9a012fb72d087e36e77 to your computer and use it in GitHub Desktop.
New Twiddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
import { tracked } from '@glimmer/tracking'; | |
import { action } from '@ember/object'; | |
export default class SelectList extends Component { | |
@tracked selectedIndex = 0; | |
get items() { | |
return this.args.items; | |
} | |
get firstIndex() { | |
return 0; | |
} | |
get lastIndex() { | |
return this.items.length - 1; | |
} | |
@action _didInsertElement() { | |
document.addEventListener('keydown', this.onKeydown); | |
} | |
@action _willDestroyElement() { | |
document.removeEventListener('keydown', this.onKeydown); | |
} | |
@action _didUpdateAttrs() { | |
if (this.selectedIndex > this.lastIndex) { | |
this.selectedIndex = this.lastIndex; | |
} | |
} | |
@action onKeydown(e) { | |
switch (e.key) { | |
case 'ArrowUp': | |
return this.selectPrev(); | |
case 'ArrowDown': | |
return this.selectNext(); | |
// case 'Enter': | |
// return this.didCommit(); | |
// case 'Escape': | |
//. return this.didCancel(); | |
} | |
} | |
@action selectPrev() { | |
if (this.selectedIndex === this.firstIndex) { | |
this.selectedIndex = this.lastIndex; | |
} else { | |
this.selectedIndex--; | |
} | |
} | |
@action selectNext() { | |
if (this.selectedIndex === this.lastIndex) { | |
this.selectedIndex = this.firstIndex; | |
} else { | |
this.selectedIndex++; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
import { action } from '@ember/object'; | |
import { tracked } from '@glimmer/tracking'; | |
const ITEMS = ['one', 'two', 'three', 'four']; | |
export default class ApplicationController extends Controller { | |
@tracked items = [...ITEMS]; | |
@action updateItems(count) { | |
this.items = ITEMS.slice(0, count); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
ul { | |
border: 1px solid black; | |
} | |
li.selected { | |
background: #efefef; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"@ember/render-modifiers": "1.0.2", | |
"ember-truth-helpers": "2.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment