Skip to content

Instantly share code, notes, and snippets.

@zigotica
Forked from Serabe/components.close-button.js
Created October 4, 2016 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zigotica/ebc9f115f2ce82ac91661aef4f717a6d to your computer and use it in GitHub Desktop.
Save zigotica/ebc9f115f2ce82ac91661aef4f717a6d to your computer and use it in GitHub Desktop.
Repeatable Fields
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'button',
click() {
let action = this.get('action');
if (typeof action === 'function') {
action();
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
init() {
this._super(...arguments);
this.set('components', Ember.A([Ember.Object.create({value: ''})]));
},
didReceiveAttrs() {
this._super(...arguments);
this.set('maxRepeats', Math.max(1, parseInt(this.get('max-repeats'), 10)) || 5);
this.set('minRepeats', Math.max(1, parseInt(this.get('min-repeats'), 10)) || 2);
},
addElementIfNeeded() {
console.log(this.get('components.length'), this.get('maxRepeats'), this.get('components.lastObject.value'));
if (this.get('components.length') < this.get('maxRepeats') &&
Ember.isPresent(this.get('components.lastObject.value'))) {
this.get('components').pushObject(Ember.Object.create({value:''}));
}
},
actions: {
removeRepetition(idx) {
this.get('components').removeAt(idx, 1);
this.addElementIfNeeded();
},
onInput(idx, value) {
},
onChange(idx, value) {
this.get('components')
.objectAt(idx)
.set('value', value);
this.get('onchange')(this.get('components'), idx, value);
this.addElementIfNeeded();
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
init() {
this._super(...arguments);
this.set('value', '');
},
actions: {
onInput(value) {
this.get('oninput')(value);
},
onChange(value) {
this.get('onchange')(value);
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
repeatableFieldChanged() { }
}
});
import Ember from 'ember';
export function arr(params=[]/*, hash*/) {
return params.slice(0);
}
export default Ember.Helper.helper(arr);
import Ember from 'ember';
export function isDeletable([value, idx]/*, hash*/) {
return Ember.isPresent(value) && idx > 0;
}
export default Ember.Helper.helper(isDeletable);
import Ember from 'ember';
export function isExtraField([idx]/*, hash*/) {
return idx > 0;
}
export default Ember.Helper.helper(isExtraField);
import Ember from 'ember';
export function isMainField([idx]/*, hash*/) {
return idx === 0;
}
export default Ember.Helper.helper(isMainField);
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
input:required {
border-color: red;
}
<!--
<h2> A basic text component </h2>
<div class="basic-text">
{{text-component value=inputValue oninput=(action (mut inputValue)) onchange=(action (mut changeValue))}}<br>
<p>
InputValue: {{inputValue}}
</p>
<p>
ChangeValue: {{changeValue}}
</p>
</div>
-->
<h2> A repeatable text field </h2>
{{repeatable-field
field=(component "text-component")
max-repeats=maxRepeats
min-repeats=minRepeats
onchange=(action "repeatableFieldChanged")}}
<div>
</div>
<br>
<br>
<h3> Some fieldsets </h3>
{{#each components as |comp idx|}}
{{component field value=(readonly comp.value)
oninput=(action "onInput" idx)
onchange=(action "onChange" idx)
required=(is-main-field idx)
afterComponents=(if (is-deletable comp.value idx) (arr (component "close-button" action=(action "removeRepetition" idx))) (arr))
}} ({{comp.value}})
{{/each}}
<h3> Values </h3>
<ol>
{{#each components as |comp idx|}}
<li>{{comp.value}}</li>
{{/each}}
</ol>
<input value={{value}}
oninput={{action "onInput" value="target.value"}}
onchange={{action "onChange" value="target.value"}}
required={{required}}>
{{#if afterComponents}}
|
{{/if}}
{{#each afterComponents as |comp|}}
{{component comp}}
{{/each}}
{
"version": "0.10.5",
"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.9.0-beta.4",
"ember-template-compiler": "2.9.0-beta.4"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment