Skip to content

Commit

Permalink
allow paper item to proxy radios
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelcobain committed Jan 6, 2017
1 parent df788e1 commit 9c4b874
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 60 deletions.
59 changes: 59 additions & 0 deletions addon/components/paper-radio-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @module ember-paper
*/
import Ember from 'ember';
import layout from '../templates/components/paper-radio-base';
import FocusableMixin from 'ember-paper/mixins/focusable-mixin';
import RippleMixin from 'ember-paper/mixins/ripple-mixin';
import ColorMixin from 'ember-paper/mixins/color-mixin';

const { Component, computed, assert } = Ember;

/**
* @class PaperRadio
* @extends Ember.Component
* @uses FocusableMixin
* @uses ColorMixin
* @uses RippleMixin
*/
export default Component.extend(FocusableMixin, RippleMixin, ColorMixin, {
layout,
tagName: 'md-radio-button',
classNames: ['md-default-theme'],
classNameBindings: ['checked:md-checked'],

tabindex: null,

toggle: false,

/* Ripple Overrides */
rippleContainerSelector: '.md-container',
center: true,
dimBackground: false,
fitRipple: true,

/* FocusableMixin Overrides */
focusOnlyOnKey: true,

// Lifecycle hooks
init() {
assert('{{paper-radio}} requires an `onChange` action or null for no action.', this.get('onChange') !== undefined);
this._super(...arguments);
},

checked: computed('groupValue', 'value', function() {
return this.get('groupValue') === this.get('value');
}),

click() {
if (!this.get('disabled')) {
if (this.get('toggle')) {
this.sendAction('onChange', this.get('checked') ? null : this.get('value'));
} else {
this.sendAction('onChange', this.get('value'));
}
}
// Prevent bubbling, if specified. If undefined, the event will bubble.
return this.get('bubbles');
}
});
16 changes: 16 additions & 0 deletions addon/components/paper-radio-proxiable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @module ember-paper
*/
import PaperRadioBaseComponent from './paper-radio-base';
import ProxiableMixin from 'ember-paper/mixins/proxiable-mixin';

/**
* @class PaperRadio
* @extends PaperRadioBaseComponent
* @uses ProxiableMixin
*/
export default PaperRadioBaseComponent.extend(ProxiableMixin, {
processProxy() {
this.click();
}
});
58 changes: 4 additions & 54 deletions addon/components/paper-radio.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,14 @@
/**
* @module ember-paper
*/
import Ember from 'ember';
import layout from '../templates/components/paper-radio';
import FocusableMixin from 'ember-paper/mixins/focusable-mixin';
import RippleMixin from 'ember-paper/mixins/ripple-mixin';
import ColorMixin from 'ember-paper/mixins/color-mixin';
import PaperRadioBaseComponent from './paper-radio-base';
import { ChildMixin } from 'ember-composability-tools';

const { Component, computed, assert } = Ember;

/**
* @class PaperRadio
* @extends Ember.Component
* @uses FocusableMixin
* @uses ColorMixin
* @uses RippleMixin
* @extends PaperRadioBaseComponent
* @uses ChildMixin
*/
export default Component.extend(FocusableMixin, RippleMixin, ColorMixin, ChildMixin, {
layout,
tagName: 'md-radio-button',
classNames: ['md-default-theme'],
classNameBindings: ['checked:md-checked'],

tabindex: null,

toggle: false,

/* Ripple Overrides */
rippleContainerSelector: '.md-container',
center: true,
dimBackground: false,
fitRipple: true,

/* FocusableMixin Overrides */
focusOnlyOnKey: true,

/* ChildMixin Overrides */
shouldRegister: false,

// Lifecycle hooks
init() {
assert('{{paper-radio}} requires an `onChange` action or null for no action.', this.get('onChange') !== undefined);
this._super(...arguments);
},

checked: computed('groupValue', 'value', function() {
return this.get('groupValue') === this.get('value');
}),

click() {
if (!this.get('disabled')) {
if (this.get('toggle')) {
this.sendAction('onChange', this.get('checked') ? null : this.get('value'));
} else {
this.sendAction('onChange', this.get('value'));
}
}
// Prevent bubbling, if specified. If undefined, the event will bubble.
return this.get('bubbles');
}
export default PaperRadioBaseComponent.extend(ChildMixin, {
shouldRegister: false
});
1 change: 1 addition & 0 deletions addon/templates/components/paper-item.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
checkbox=(component "paper-checkbox" parentComponent=this bubbles=false shouldRegister=true)
button=(component "paper-button" parentComponent=this bubbles=false shouldRegister=true skipProxy=true)
switch=(component "paper-switch" parentComponent=this bubbles=false shouldRegister=true)
radio=(component "paper-radio-proxiable" parentComponent=this bubbles=false shouldRegister=true)
) as |controls|}}

{{#if hasPrimaryAction}}
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions app/components/paper-radio-group.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import PaperRadioGroup from 'ember-paper/components/paper-radio-group';

export default PaperRadioGroup;
export { default } from 'ember-paper/components/paper-radio-group';
1 change: 1 addition & 0 deletions app/components/paper-radio-proxiable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-paper/components/paper-radio-proxiable';
4 changes: 1 addition & 3 deletions app/components/paper-radio.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import PaperRadio from 'ember-paper/components/paper-radio';

export default PaperRadio;
export { default } from 'ember-paper/components/paper-radio';
12 changes: 12 additions & 0 deletions tests/dummy/app/templates/demo/list.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@
{{/paper-item}}
{{/each}}

{{#paper-subheader}}Single Action Radios{{/paper-subheader}}
{{#each toppings as |topping|}}
{{#paper-item as |controls|}}
<p>{{topping.name}}</p>
{{controls.radio
groupValue=radioSelectedTopping
value=topping
secondary=true
onChange=(action (mut radioSelectedTopping))}}
{{/paper-item}}
{{/each}}

{{/paper-list}}
{{! END-SNIPPET }}
{{/paper-content}}
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/components/paper-item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ test('single action checkboxes should react to item clicks', function(assert) {
});
});

test('single action radios should react to item clicks', function(assert) {
assert.expect(2);

this.set('selectedValue', null);
this.render(hbs`
{{#paper-list}}
{{#paper-item as |controls|}}
<p>Checkbox 1</p>
{{controls.radio
groupValue=selectedValue
value="some value 1"
onChange=(action (mut selectedValue))}}
{{/paper-item}}
{{#paper-item as |controls|}}
<p>Checkbox 2</p>
{{controls.radio
groupValue=selectedValue
value="some value 2"
onChange=(action (mut selectedValue))}}
{{/paper-item}}
{{/paper-list}}
`);
return wait().then(() => {
let firstItem = this.$('.md-list-item-inner').eq(0);
firstItem.click();
assert.equal(this.get('selectedValue'), 'some value 1');
let secondItem = this.$('.md-list-item-inner').eq(1);
secondItem.click();
assert.equal(this.get('selectedValue'), 'some value 2');
});
});

test('Clickable Items with Secondary Controls must not bubble main item action', function(assert) {
// the switch test is tricky as it involves passing hammer
// tap event.
Expand Down

0 comments on commit 9c4b874

Please sign in to comment.