Last active
April 21, 2017 18:59
-
-
Save fushi/a4aa47cd6721b959956a7271621a7d92 to your computer and use it in GitHub Desktop.
validated-input
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 Ember from 'ember'; | |
export default Ember.Component.extend({ | |
tagName: 'p', | |
classNames: ['c-input-error-text'], | |
classNameBindings: ['inlineError:inline-input-error-text'] | |
}); |
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
/** | |
* A component which handles input. Block usage and the inputComponent property use components in the validated-input directory | |
* | |
* Required Properties: | |
* | |
* labelText (string): Text to use as the label (All inputs must have labels. If you wish not to display a label, use the `hideLabel` boolean) | |
* | |
* Optional Properties: | |
* | |
* autofocus (boolean): Whether the input should be autofocused or not | |
* | |
* filter (regex literal): Regular expression that the input will be tested against (defaults to /^.*$/) | |
* | |
* hideLabel (boolean): Set to true to hide the label (defaults to false) | |
* | |
* inputComponent (string): Name of the component to use for the input | |
* | |
* inputId (string): Text to use as the `id` attribute for the input, and the `for` attribute for the label | |
* | |
* inputValue: Initial input value | |
* | |
* helpText (string): Text to display when displayHelp is true | |
* | |
* placeholder (string): Placeholder text in input (defaults to '') | |
* | |
* Usage: Inline, Block | |
* | |
* {{validated-input hideLabel=true labelText="Label" helpText="foobar" filter=regex}} | |
* | |
* {{#validated-input as |subcomponents|}} | |
* {{subcomponents.labelSection text="Foo"}} | |
* {{subcomponents.inputSection filter=regex placeholder="name"}} | |
* {{subcomponents.helpSection text="Bar"}} | |
* {{/validated-input}} | |
*/ | |
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
hideLabel: false, | |
isValid: true, | |
inputComponent: 'validated-input/text-input', | |
inputId: generateUuid(), | |
showHelpText: false, | |
actions: { | |
onBlur() { | |
this.set('showHelpText', !this.get('isValid')); | |
}, | |
onInputChanged(isValid, parsedValue) { | |
this.setProperties({ isValid, inputValue: parsedValue }); | |
if (isValid) { this.set('showHelpText', false); } | |
// Call the passed in action | |
(this.get('onInputChanged') || (() => {}))(); | |
} | |
} | |
}); | |
function generateUuid() { | |
let values = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
let uuid = ''; | |
for (let i = 0; i < 13; i++) { | |
let rand = Math.floor(36 * Math.random()); | |
uuid += values[rand]; | |
} | |
return uuid; | |
} | |
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 Ember from 'ember'; | |
export default Ember.Component.extend({ | |
tagName: 'label', | |
attributeBindings: ['for'], | |
classNameBindings: ['hideLabel:sr-only'], | |
classNames: ['c-input-label'] | |
}); |
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
/** | |
* A component which handles basic text input. Designed to be modular, and is the base for all of the *-input components in the validated-input directory | |
* | |
* Required Properties: | |
* | |
* Optional Properties: | |
* | |
* autofocus (boolean): Whether the input should be autofocused or not | |
* | |
* filter (regex literal): Regular expression that the input will be tested against (defaults to /^.*$/) | |
* | |
* formatter (function): Function that takes a value, and returns a formatted version of that value | |
* | |
* inputId (string): Text to use as the `id` attribute for the input, and the `for` attribute for the label | |
* | |
* parser (function): Function that takes a value, and returns a parsed version of that value | |
* | |
* placeholder (string): Placeholder text in input (defaults to '') | |
* | |
* Usage: Inline | |
*/ | |
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
tagName: '', | |
filter: /^.*$/, | |
value: '', | |
formattedValue: Ember.computed('value', function () { | |
return this.get('formatter')(this.get('value')); | |
}), | |
isValid: Ember.computed('filter', 'value', function () { | |
return this.get('filter').test(this.get('value')); | |
}), | |
parsedValue: Ember.computed('value', function () { | |
return this.get('parser')(this.get('value')); | |
}), | |
onInit: Ember.on('init', function () { | |
this.setDisplayValue(); | |
}), | |
formatter: value => value, | |
parser: value => value, | |
setDisplayValue() { | |
let formattedValue = this.get('formattedValue'); | |
this.set('displayValue', formattedValue); | |
}, | |
actions: { | |
fieldUpdated(newValue) { | |
this.setProperties({ displayValue: newValue, value: newValue }); | |
(this.get('onInputChanged') || (() => {}))(this.get('isValid'), newValue); | |
}, | |
onBlur() { | |
this.setDisplayValue(); | |
this.get('onBlur')(); | |
} | |
} | |
}); |
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 Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
regexAllowAll: /^\.*$/ | |
regexAllowWords: /^\w*$/ | |
regexAllowDigits: /^\d*$/ | |
}); |
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.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", | |
"ember-one-way-controls": "2.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment