Last active
April 19, 2018 08:53
-
-
Save olegdovger/c7efa8affae30c97a478c02047a25887 to your computer and use it in GitHub Desktop.
input-phone
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 Ember from 'ember'; | |
import { parse, parseNumber, format, formatNumber, isValidNumber } from 'libphonenumber-js'; | |
//const allowedChars = ['+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '(', ')']; | |
export default Ember.Component.extend({ | |
tagName: 'input', | |
type: 'tel', | |
attributeBindings: ['placeholder', 'value', 'type'], | |
classNames: ['no_outline'], | |
classNameBindings: ['invalid:error'], | |
invalid: true, | |
allowedChars: ["+", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
withFormat: true, | |
val: null, | |
change({ target, key }) { | |
let value = target.value; | |
let allowedChars = this.get('allowedChars'); | |
if (value.indexOf('+') === -1) { | |
alert(123); | |
value = '+' + value; | |
} | |
this.set('val', target.value); | |
let lastChar = value.substr(value.length - 1); | |
if (!allowedChars.includes(lastChar) && value.indexOf(key) > -1) { | |
value = value.substr(0, value.length - 1); | |
} | |
if (!allowedChars.includes(key) && value.indexOf(key) > -1) { | |
value = value.replace(key, ''); | |
} | |
if (this.get('withFormat')) { | |
value = formatNumber(value, 'International', { extended: true }); | |
} else { | |
//target.value = value; | |
} | |
Ember.run(() => { | |
this.set('value', value); | |
this.set('invalid', !isValidNumber(value)); | |
}); | |
}, | |
keyPress({ target, key }) { | |
//this.change({ target, key }); | |
}, | |
keyDown({ target, key }) { | |
Ember.run.debounce(this, this.change, { target, key }, 300); | |
}, | |
keyUp({ target, key }) { | |
Ember.run.debounce(this, this.change, { target, key }, 300); | |
} | |
}); |
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 Ember from 'ember'; | |
import { parse, parseNumber, format, formatNumber } from 'libphonenumber-js'; | |
const allowedChars = ['+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; | |
export default Ember.Controller.extend({ | |
value: '79053453512', | |
actions: { | |
change(val, { target, key }) { | |
let value = target.value; | |
if (value.indexOf('+') === -1) { | |
value = '+' + value; | |
} | |
let lastChar = value.substr(value.length - 1); | |
if (!allowedChars.includes(lastChar)) { | |
value = value.substr(0, value.length - 1); | |
} | |
if (!allowedChars.includes(key) && value.indexOf(key) > -1) { | |
value = value.replace(key, ''); | |
} | |
target.value = formatNumber(value, 'International'); | |
//target.value = value; | |
Ember.run(() => { | |
this.set('value', target.value); | |
}); | |
} | |
} | |
}); |
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; | |
} | |
.error { | |
border-color: red; | |
} | |
.no_outline { | |
outline: none; | |
} |
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.13.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.16.2", | |
"ember-template-compiler": "2.16.2", | |
"ember-testing": "2.16.2" | |
}, | |
"addons": { | |
"ember-data": "2.16.3", | |
"ember-libphonenumber-js": "v0.2.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment