-
-
Save krisselden/e9560064df52865c5042487739d4e836 to your computer and use it in GitHub Desktop.
New Twiddle
This file contains 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"; | |
class Cell { | |
@tracked value; | |
constructor(key, value) { | |
this.key = key; | |
this.value = value; | |
} | |
} | |
function wrap(index, length) { | |
let wrapped = Math.abs(index) % length; | |
return index < 0 ? length - wrapped : wrapped; | |
} | |
function clamp(index, length) { | |
return Math.max(0, Math.min(index, length - 1)); | |
} | |
export default class SelectList extends Component { | |
selectedIndexCell = new Cell(0, -1); | |
get selectedIndex() { | |
let selectedIndexCell = this.selectedIndexCell; | |
let length = this.items.length; | |
if (selectedIndexCell.key !== length) { | |
let clampedIndex = clamp(selectedIndexCell.value, length); | |
selectedIndexCell = this.selectedIndexCell = new Cell( | |
length, | |
clampedIndex | |
); | |
} | |
return selectedIndexCell.value; | |
} | |
set selectedIndex(value) { | |
this.selectedIndexCell.value = wrap(value, this.items.length); | |
} | |
get items() { | |
return this.args.items; | |
} | |
@action _didInsertElement() { | |
document.addEventListener("keydown", this.onKeydown); | |
} | |
@action _willDestroyElement() { | |
document.removeEventListener("keydown", this.onKeydown); | |
} | |
@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() { | |
this.selectedIndex--; | |
} | |
@action selectNext() { | |
this.selectedIndex++; | |
} | |
} |
This file contains 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 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 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