Skip to content

Instantly share code, notes, and snippets.

@lukesargeant
Created December 3, 2017 21:53
Show Gist options
  • Save lukesargeant/7f9420086e5d2c52b3d0d65628e1adf7 to your computer and use it in GitHub Desktop.
Save lukesargeant/7f9420086e5d2c52b3d0d65628e1adf7 to your computer and use it in GitHub Desktop.
DDAU Input Component
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'input',
attributeBindings: [ 'type', 'value', 'placeholder', 'data-stripe', 'name' ],
type: 'text',
value: '',
caret: 0,
input() {
let newValue = this.readDOMAttr('value');
let newCaret = this.element.selectionStart;
console.log('onInput', newValue, newCaret);
this._syncDom();
this.sendAction('update', newValue, newCaret);
},
_syncDom() {
let oldValue = this.get('value') || '';
let oldCaret = this.get('caret') || 0;
this.element.value = oldValue;
this.element.setSelectionRange(oldCaret, oldCaret);
},
didRender: function() {
this.element.value = this.get('value') || '';
let caret = this.get('caret') || 0;
this.element.setSelectionRange(caret, caret);
},
});
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
update(newValue, newCaret) {
let isValid = newValue.match(/^[\d]*[\.]?[\d]*$/);
if (isValid) {
this.set('value', newValue);
this.set('caret', newCaret);
}
}
}
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
label, input {
display: block;
box-sizing: border-box;
}
input {
margin: .5em 0;
font-size: 16px;
padding:.5em;
}
<h1>DDAU Input Example</h1>
<div>
<label>Field</label>
{{ddau-input
value=value
caret=caret
update=(action "update")}}
</div>
{
"version": "0.12.1",
"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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment