Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Stop currying click event to action handlers #630

Merged
merged 3 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion addon/components/polaris-button.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { action, computed } from '@ember/object';
import { or } from '@ember/object/computed';
import { isPresent } from '@ember/utils';
import { classify } from '@ember/string';
Expand Down Expand Up @@ -371,4 +371,9 @@ export default class PolarisButtonComponent extends Component {
}
);
}

@action
handleClick(/* event */) {
this.onClick();
}
}
7 changes: 6 additions & 1 deletion addon/components/polaris-link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { action, computed } from '@ember/object';
import { tagName, layout as templateLayout } from '@ember-decorators/component';
import layout from '../templates/components/polaris-link';

Expand Down Expand Up @@ -73,4 +73,9 @@ export default class PolarisLink extends Component {

return linkClasses.join(' ');
}

@action
handleClick(/* event */) {
this.onClick();
}
}
9 changes: 6 additions & 3 deletions addon/components/polaris-page/action.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { action, computed } from '@ember/object';
import { isBlank } from '@ember/utils';
import { tagName, layout } from '@ember-decorators/component';
import template from '../../templates/components/polaris-page/action';
Expand Down Expand Up @@ -66,8 +66,6 @@ export default class ActionComponent extends Component {
*/
onAction() {}

type = 'button';

handleMouseUpByBlurring = handleMouseUpByBlurring;

@computed('text', 'icon')
Expand All @@ -88,4 +86,9 @@ export default class ActionComponent extends Component {

return classes.join(' ');
}

@action
handleClick(/* event */) {
this.onAction();
}
}
38 changes: 38 additions & 0 deletions addon/components/polaris-resource-list/checkable-button.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Component from '@ember/component';
import { action, computed } from '@ember/object';
import { not, and } from '@ember/object/computed';
import { tagName, layout as templateLayout } from '@ember-decorators/component';
import layout from '../../templates/components/polaris-resource-list/checkable-button';
Expand Down Expand Up @@ -76,4 +77,41 @@ export default class CheckableButton extends Component {

@and('isNotPlain', 'measuring')
shouldApplyMeasuringClass;

@computed(
'plain',
'shouldApplySelectModeClass',
'shouldApplySelectedClass',
'shouldApplyMeasuringClass'
)
get classes() {
let classes = ['Polaris-ResourceList-CheckableButton'];
if (this.plain) {
classes.push(
'Polaris-ResourceList-CheckableButton__CheckableButton--plain'
);
}
if (this.shouldApplySelectModeClass) {
classes.push(
'Polaris-ResourceList-CheckableButton__CheckableButton--selectMode'
);
}
if (this.shouldApplySelectedClass) {
classes.push(
'Polaris-ResourceList-CheckableButton__CheckableButton--selected'
);
}
if (this.shouldApplyMeasuringClass) {
classes.push(
'Polaris-ResourceList-CheckableButton__CheckableButton--measuring'
);
}

return classes.join(' ');
}

@action
toggleAll(/* event */) {
this.onToggleAll();
}
}
2 changes: 1 addition & 1 deletion addon/components/polaris-unstyled-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,6 @@ export default class PolarisUnstyledLinkComponent extends Component {
@action
handleClick(event) {
event.stopPropagation();
this.onClick(event);
this.onClick();
}
}
14 changes: 7 additions & 7 deletions addon/templates/components/polaris-button.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#with
{{#let
(component
"polaris-button/content"
text=@text
Expand Down Expand Up @@ -40,10 +40,10 @@
@external={{@external}}
@download={{@download}}
@ariaLabel={{@accessibilityLabel}}
@onClick={{action this.onClick}}
@onFocus={{action this.onFocus}}
@onBlur={{action this.onBlur}}
@onMouseUp={{action this.handleMouseUpByBlurring}}
@onClick={{this.handleClick}}
@onFocus={{this.onFocus}}
@onBlur={{this.onBlur}}
@onMouseUp={{this.handleMouseUpByBlurring}}
...attributes
>
{{#if hasBlock}}
Expand All @@ -68,7 +68,7 @@
aria-controls={{@ariaControls}}
aria-expanded={{if @ariaExpanded "true"}}
aria-pressed={{if @ariaPressed "true"}}
{{on "click" this.onClick}}
{{on "click" this.handleClick}}
{{on "focus" this.onFocus}}
{{on "blur" this.onBlur}}
{{on "keydown" this.onKeyDown}}
Expand All @@ -86,4 +86,4 @@
{{/if}}
</button>
{{/if}}
{{/with}}
{{/let}}
4 changes: 2 additions & 2 deletions addon/templates/components/polaris-link.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class={{this.linkClass}}
@url={{@url}}
@external={{@external}}
@onClick={{fn (or @onClick this.onClick)}}
@onClick={{this.handleClick}}
>
{{#if hasBlock}}
{{yield}}
Expand All @@ -15,7 +15,7 @@
<button
type="button"
class={{this.linkClass}}
{{on "click" (stop-propagation (or @onClick this.onClick))}}
{{on "click" (stop-propagation this.handleClick)}}
>
{{#if hasBlock}}
{{yield}}
Expand Down
4 changes: 2 additions & 2 deletions addon/templates/components/polaris-page/action.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{! TODO: support url }}
<button
type="button"
class={{this.classes}}
aria-label={{@accessibilityLabel}}
disabled={{@disabled}}
type={{this.type}}
{{on "click" this.onAction}}
{{on "click" this.handleClick}}
{{on "mouseup" this.handleMouseUpByBlurring}}
>
{{#if (or @icon @disclosure)}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{{!-- template-lint-disable no-invalid-interactive --}}
<div
{{on "click" (fn (or @onToggleAll this.onToggleAll))}}
class="Polaris-ResourceList-CheckableButton
{{if this.plain "Polaris-ResourceList-CheckableButton__CheckableButton--plain"}}
{{if this.shouldApplySelectModeClass "Polaris-ResourceList-CheckableButton__CheckableButton--selectMode"}}
{{if this.shouldApplySelectedClass "Polaris-ResourceList-CheckableButton__CheckableButton--selected"}}
{{if this.shouldApplyMeasuringClass "Polaris-ResourceList-CheckableButton__CheckableButton--measuring"}}
"
class={{this.classes}}
{{on "click" this.toggleAll}}
...attributes
>
<div class="Polaris-ResourceList-CheckableButton__Checkbox">
Expand Down
24 changes: 7 additions & 17 deletions tests/integration/components/polaris-button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,31 +235,21 @@ module('Integration | Component | polaris-button', function(hooks) {
module('onClick()', function() {
test('is called when the button is clicked', async function(assert) {
this.set('handleClick', (_myArg, event) => {
assert.ok(true, 'triggers onClick handler');
assert.ok(
event instanceof MouseEvent,
'receives the MouseEvent as last argument'
);
assert.ok(true, 'triggers @onClick handler');
assert.notOk(event, 'does not curry click event to @onClick');
});

await render(
hbs`{{polaris-button onClick=(action handleClick "myArg")}}`
);
await render(hbs`{{polaris-button onClick=this.handleClick}}`);
await click('button');
});

test('is called when the link is clicked', async function(assert) {
this.set('handleClick', (_myArg, event) => {
assert.ok(true, 'triggers onClick handler');
assert.ok(
event instanceof MouseEvent,
'receives the MouseEvent as last argument'
);
this.set('handleClick', (event) => {
assert.ok(true, 'triggers @onClick handler');
assert.notOk(event, 'does not curry click event to @onClick');
});

await render(
hbs`{{polaris-button onClick=(action handleClick "myArg") url="#"}}`
);
await render(hbs`{{polaris-button onClick=this.handleClick url="#"}}`);
await click('[data-polaris-unstyled]');
});
});
Expand Down
Loading