Skip to content

Commit

Permalink
Merge pull request #24 from rynam0/input-fields
Browse files Browse the repository at this point in the history
create input components for Date, Select, and TextArea
  • Loading branch information
Stefan Gasser committed Mar 27, 2015
2 parents 0af6a32 + a8c3abb commit 324fc62
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 35 deletions.
20 changes: 20 additions & 0 deletions addon/components/materialize-date-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import MaterializeInput from './materialize-input';
import layout from '../templates/components/materialize-date-input';

export default MaterializeInput.extend({
layout: layout,
selectMonths: true,
numberOfYears: 15,
min: '',
max: '',
didInsertElement: function() {
this._super();
var self = this;
this.$('.datepicker').pickadate({
selectMonths: self.selectMonths,
selectYears: self.numberOfYears,
min: self.min,
max: self.max
});
}
});
28 changes: 28 additions & 0 deletions addon/components/materialize-input-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Ember from 'ember';

export default Ember.Component.extend({
init: function() {
this._super();
// bind validation errors
var propertyPath = this.get('valueBinding._label');
if (Ember.isPresent(propertyPath)) {
Ember.Binding.from('targetObject.errors.' + propertyPath)
.to('errors')
.connect(this);
}
},
bindAttributes: ['disabled', 'readonly'],
tagName: 'div',
classNames: ['input-field'],
validate: false,
id: Ember.computed(function() {
var elementId = this.get('elementId');
return elementId + '-input';
}),
didInsertElement: function() {
// pad the errors element when an icon is present
if (Ember.isPresent(this.get('icon'))) {
this.$('>span').css('padding-left', '3rem');
}
}
});
22 changes: 3 additions & 19 deletions addon/components/materialize-input.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import Ember from 'ember';
import MaterializeInputField from './materialize-input-field';
import layout from '../templates/components/materialize-input';

export default Ember.Component.extend({
init: function() {
this._super();
// bind validation errors
var propertyPath = this.get('valueBinding._label');
if (Ember.isPresent(propertyPath)) {
Ember.Binding.from('targetObject.errors.' + propertyPath)
.to('errors')
.connect(this);
}
},
export default MaterializeInputField.extend({
layout: layout,
tagName: 'div',
classNames: ['input-field'],
validate: false,
id: Ember.computed(function() {
var elementId = this.get('elementId');
return elementId + '-input';
}),
didInsertElement: function() {
this._super();
// make sure the label moves when a value is bound.
var labelSelector = this.$('>label');
if (Ember.isPresent(this.get('value')) && !labelSelector.hasClass('active')) {
labelSelector.addClass('active');
labelSelector.trigger('validate');
}
}
});
Expand Down
26 changes: 26 additions & 0 deletions addon/components/materialize-select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Ember from 'ember';
import MaterializeInputField from './materialize-input-field';
import layout from '../templates/components/materialize-select';

export default MaterializeInputField.extend({
layout: layout,
didInsertElement: function() {
this._super();
this.$('select').material_select();
},
errorsDidChange: function() {
var self = this;
var inputSelector = this.$('input');
// monitor the select's validity and copy the appropriate validation class to the materialize input element.
Ember.run.later(function() {
var isValid = self.$('select').hasClass('valid');
if (isValid) {
inputSelector.removeClass('invalid');
inputSelector.addClass('valid');
} else {
inputSelector.removeClass('valid');
inputSelector.addClass('invalid');
}
}, 150);
}.observes('errors')
});
15 changes: 15 additions & 0 deletions addon/components/materialize-textarea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Ember from 'ember';
import InputField from './materialize-input-field';
import layout from '../templates/components/materialize-textarea';

export default InputField.extend({
layout: layout,
didInsertElement: function() {
this._super();
// make sure the label moves when a value is bound.
var labelSelector = this.$('>label');
if (Ember.isPresent(this.get('value')) && !labelSelector.hasClass('active')) {
labelSelector.addClass('active');
}
}
});
11 changes: 11 additions & 0 deletions addon/templates/components/materialize-date-input.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#if icon}}
<i {{bind-attr class="icon :prefix"}}></i>
{{/if}}
<input type="date" {{bind-atrr id=id}} {{bind-attr class="validate:validate: errors:invalid:valid :datepicker"}}
{{bind-attr data-value=value}} {{bind-attr required=required}} {{bind-attr readonly=readonly}}
{{bind-attr disabled=disabled}}/>
<label {{bind-attr for=id}}>{{label}}</label>
<small class="red-text">
{{#if errors}} {{errors.firstObject}} {{else}} &nbsp; {{/if}}
</small>

2 changes: 1 addition & 1 deletion addon/templates/components/materialize-input.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#if icon}}
<i {{bind-attr class="icon :prefix"}}></i>
{{/if}}
{{input id=id value=value classNameBindings="validate:validate: errors:invalid:valid" type="text"
{{input id=id value=value classNameBindings="validate:validate: errors.firstObject:invalid:valid" type="text"
required=required pattern=pattern maxlength=maxlength readonly=readonly disabled=disabled
autocomplete=autocomplete}}
<label {{bind-attr for=id}}>{{label}}</label>
Expand Down
6 changes: 6 additions & 0 deletions addon/templates/components/materialize-select.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<label {{bind-attr id=id}} class="active">{{label}}</label>
{{view "select" id=id content=content optionValuePath=optionValuePath optionLabelPath=optionLabelPath prompt=prompt
value=value classNameBindings="validate:validate: errors:invalid:valid"}}
<small class="red-text">
{{#if errors}} {{errors.firstObject}} {{else}} &nbsp; {{/if}}
</small>
10 changes: 10 additions & 0 deletions addon/templates/components/materialize-textarea.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#if icon}}
<i {{bind-attr class="icon :prefix"}}></i>
{{/if}}
{{textarea id=id value=value name=name required=required readonly=readonly disabled=disabled maxlength=maxlength
class="materialize-textarea"}}
<label {{bind-attr for=id}}>{{label}}</label>
<small class="red-text">
{{#if errors}} {{errors.firstObject}} {{else}} &nbsp; {{/if}}
</small>

3 changes: 3 additions & 0 deletions app/components/materialize-date-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import materializeDateInput from 'ember-cli-materialize/components/materialize-date-input';

export default materializeDateInput;
3 changes: 3 additions & 0 deletions app/components/materialize-input-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import materializeInputField from 'ember-cli-materialize/components/materialize-input-field';

export default materializeInputField;
3 changes: 3 additions & 0 deletions app/components/materialize-select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import materializeSelect from 'ember-cli-materialize/components/materialize-select';

export default materializeSelect;
3 changes: 3 additions & 0 deletions app/components/materialize-textarea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import materializeTextarea from 'ember-cli-materialize/components/materialize-textarea';

export default materializeTextarea;
32 changes: 25 additions & 7 deletions tests/dummy/app/controllers/input.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import Ember from 'ember';

export default Ember.ObjectController.extend({

frameworks: [
{id: 1, value: 'Materialize CSS'},
{id: 2, value: 'Ember-CLI Materialize'}
],
errors: Ember.Object.create({
name: [],
framework: []
}),
nameDidChange: function() {
var errors = { name: [] };
var errors = this.get('errors');
var messages = [];
if (!Ember.isPresent(this.get('name'))) {
errors.name = ['This field is required'];
} else {
errors.name = [];
messages = ['This field is required'];
}
errors.set('name', messages);
this.set('errors', errors);
}.observes('name')

}.observes('name'),
frameworkDidChange: function() {
var self = this;
var errors = self.get('errors');
Ember.run.later(function() {
var messages = [];
if (!Ember.isPresent(self.get('framework'))) {
messages = ['This field is required'];
}
errors.set('framework', messages);
self.set('errors', errors);
}, 100);
}.observes('framework')
});
2 changes: 1 addition & 1 deletion tests/dummy/app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{{#link-to 'navbar' class="collection-item"}}Navbar<span class="new badge">1</span>{{/link-to}}
{{#link-to 'cards' class="collection-item"}}Cards<span class="new badge">1</span>{{/link-to}}
{{#link-to 'collapsible' class="collection-item"}}Collapsible<span class="new badge">1</span>{{/link-to}}
{{#link-to 'input' class="collection-item"}}Input<span class="new badge">1</span>{{/link-to}}
{{#link-to 'input' class="collection-item"}}Input<span class="new badge">4</span>{{/link-to}}
</ul>
</div>

Expand Down
101 changes: 97 additions & 4 deletions tests/dummy/app/templates/input.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
</div>

<div class='container'>

<div class="section">
<h4 class="col s12 header">Input</h4>

<div class="intro">
<h4 class="col s12 header">The component supports the following options:</h4>
<h5 class="col s12 header">The component supports the following options:</h5>
<ul>
<li>label, default value <span class="default-value badge">null</span></li>
<li>icon, default value <span class="default-value badge">null</span></li>
Expand All @@ -25,9 +28,8 @@
<li>autocomplete, default value <span class="default-value badge">null</span></li>
</ul>
</div>
</div>
<div class="section">
<h4 class="col s12 header">Input</h4>


<div class="button-example">
<br/>
{{materialize-input value="Hi Mom" label='Example'}}
Expand Down Expand Up @@ -83,5 +85,96 @@ nameDidChange: function() {
</pre>
</div>
</div>

<div class="section">
<h4 class="col s12 header">Text Area</h4>
<div class="intro">
<h5 class="col s12 header">The component supports the following options:</h5>
<ul>
<li>label, default value <span class="default-value badge">null</span></li>
<li>icon, default value <span class="default-value badge">null</span></li>
<li>value, default value <span class="default-value badge">null</span></li>
<li>name, default value <span class="default-value badge">null</span></li>
<li>required, default value <span class="default-value badge">false</span></li>
<li>readonly, default value <span class="default-value badge">false</span></li>
<li>disabled, default value <span class="default-value badge">false</span></li>
<li>maxlength, default value <span class="default-value badge">null</span></li>
<li>validate, default value <span class="default-value badge">false</span></li>
</ul>
</div>
<div class="button-example">
<br/>

{{materialize-textarea label='Message' value=message icon="mdi-action-face-unlock"}}

<pre class=" language-markup">
<code class=" col s12 language-markup">
&#123;&#123;materialize-textarea label='Message' value="The textarea will auto-grow as you type." icon="mdi-action-face-unlock"&#125;&#125;
</code>
</pre>
</div>
</div>

<div class="section">
<h4 class="col s12 header">Select</h4>
<div class="intro">
<h5 class="col s12 header">The component supports the following options:</h5>
<ul>
<li>label, default value <span class="default-value badge">null</span></li>
<li>icon, default value <span class="default-value badge">null</span></li>
<li>prompt, default value <span class="default-value badge">null</span></li>
<li>content, default value <span class="default-value badge">null</span></li>
<li>value, default value <span class="default-value badge">null</span></li>
<li>optionValuePath, default value <span class="default-value badge">null</span></li>
<li>optionLabelPath, default value <span class="default-value badge">null</span></li>
<li>validate, default value <span class="default-value badge">false</span></li>
</ul>
</div>
<div class="button-example">
<br/>

{{materialize-select content=frameworks value=framework label="Framework" prompt="Please choose..."
optionLabelPath="content.value" optionValuePath="content"}}

<pre class=" language-markup">
<code class=" col s12 language-markup">
&#123;&#123;materialize-select content=frameworks value=framework label="Framework" prompt="Please choose..."
optionLabelPath="content.value" optionValuePath="content"&#125;&#125;
</code>
</pre>
</div>
</div>

<div class="section">
<h4 class="col s12 header">Date Input</h4>
<div class="intro">
<h5 class="col s12 header">The component supports the following options:</h5>
<ul>
<li>label, default value <span class="default-value badge">null</span></li>
<li>icon, default value <span class="default-value badge">null</span></li>
<li>value, default value <span class="default-value badge">null</span></li>
<li>required, default value <span class="default-value badge">null</span></li>
<li>readonly, default value <span class="default-value badge">null</span></li>
<li>disabled, default value <span class="default-value badge">null</span></li>
<li>selectMonths, default value <span class="default-value badge">true</span></li>
<li>numberOfYears, default value <span class="default-value badge">15</span></li>
<li>min, default value <span class="default-value badge">null</span></li>
<li>max, default value <span class="default-value badge">null</span></li>
<li>validate, default value <span class="default-value badge">false</span></li>
</ul>
</div>

<div class="button-example">
<br/>
{{materialize-date-input label='Birthday' icon="mdi-action-face-unlock"}}

<pre class=" language-markup">
<code class=" col s12 language-markup">
&#123;&#123;materialize-date-input label='Birthday' icon="mdi-action-face-unlock"&#125;&#125;
</code>
</pre>
</div>
</div>

</div>

Loading

0 comments on commit 324fc62

Please sign in to comment.