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

Modernize polaris-radio-button component #504

Merged
merged 4 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 24 additions & 25 deletions addon/components/polaris-radio-button.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { tagName, layout as templateLayout } from '@ember-decorators/component';
import layout from '../templates/components/polaris-radio-button';

/**
* Polaris radio button component.
* See https://polaris.shopify.com/components/forms/radio-button
*/
export default Component.extend({
// Tagless component, renders a `polaris-choice` component internally.
tagName: '',

layout,

@tagName('')
@templateLayout(layout)
export default class PolarisRadioButton extends Component {
/**
* Label for the radio button
*
Expand All @@ -21,7 +19,7 @@ export default Component.extend({
* @default null
* @public
*/
label: null,
label = null;

/**
* Visually hide the label
Expand All @@ -31,7 +29,7 @@ export default Component.extend({
* @default false
* @public
*/
labelHidden: false,
labelHidden = false;

/**
* Radio button is selected
Expand All @@ -41,7 +39,7 @@ export default Component.extend({
* @default false
* @public
*/
checked: false,
checked = false;

/**
* Additional text to aid in use
Expand All @@ -51,7 +49,7 @@ export default Component.extend({
* @default null
* @public
*/
helpText: null,
helpText = null;

/**
* ID for form input
Expand All @@ -61,7 +59,7 @@ export default Component.extend({
* @default null
* @public
*/
inputId: null,
inputId = null;

/**
* Name for form input
Expand All @@ -71,7 +69,7 @@ export default Component.extend({
* @default null
* @public
*/
name: null,
name = null;

/**
* Value for form input
Expand All @@ -81,7 +79,7 @@ export default Component.extend({
* @default null
* @public
*/
value: null,
value = null;

/**
* Disable the radio button
Expand All @@ -91,7 +89,7 @@ export default Component.extend({
* @default false
* @public
*/
disabled: false,
disabled = false;

/**
* Callback when radio button is toggled
Expand All @@ -101,7 +99,7 @@ export default Component.extend({
* @default noop
* @public
*/
onChange() {},
onChange() {}

/**
* Callback when radio button is focussed
Expand All @@ -111,7 +109,7 @@ export default Component.extend({
* @default noop
* @public
*/
onFocus() {},
onFocus() {}

/**
* Callback when focus is removed
Expand All @@ -121,20 +119,21 @@ export default Component.extend({
* @default noop
* @public
*/
onBlur() {},
onBlur() {}

/**
* @private
*/
_id: computed('inputId', function() {
return this.get('inputId') || `polaris-radio-button-${guidFor(this)}`;
}).readOnly(),
@(computed('inputId').readOnly())
get _id() {
return this.inputId || `polaris-radio-button-${guidFor(this)}`;
}

/**
* @private
*/
describedBy: computed('helpText', '_id', function() {
const helpText = this.get('helpText');
return helpText ? `${this.get('_id')}HelpText` : null;
}).readOnly(),
});
@(computed('helpText', '_id').readOnly())
get describedBy() {
return this.helpText ? `${this._id}HelpText` : null;
}
}
40 changes: 20 additions & 20 deletions addon/templates/components/polaris-radio-button.hbs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{{#polaris-choice
inputId=_id
label=label
labelHidden=labelHidden
disabled=disabled
helpText=helpText
}}
<span data-test-radio-button class="Polaris-RadioButton">
<PolarisChoice
@inputId={{this._id}}
@label={{@label}}
@labelHidden={{@labelHidden}}
@disabled={{@disabled}}
@helpText={{@helpText}}
>
<span data-test-radio-button="true" class="Polaris-RadioButton">
<input
data-test-radio-button-input
type="radio"
class="Polaris-RadioButton__Input"
id={{_id}}
name={{name}}
value={{value}}
checked={{checked}}
disabled={{disabled}}
aria-describedby={{describedBy}}
onchange={{action onChange value="target.value"}}
onfocus={{action onFocus}}
onblur={{action onBlur}}
id={{this._id}}
name={{@name}}
value={{@value}}
checked={{@checked}}
disabled={{@disabled}}
aria-describedby={{this.describedBy}}
onchange={{action (or @onChange this.onChange) value="target.value"}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left the action helper here because the fn wasn't working as expected when passing value="target.value" 🤷‍♂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomnez can you try
<input ...... {{on "change" (or @onChange this.onChange)}} ....>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still passes the event as the first (and only) argument now rather than the expected value 🤷‍♂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really a problem, we can just handle in the local action right? as the example here

Screen Shot 2020-02-17 at 4 09 59 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can do that 👍

onfocus={{fn (or @onFocus this.onFocus)}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same...let's use the on modifier for these last 2

onblur={{fn (or @onBlur this.onBlur)}}
>

<span data-test-radio-button-backdrop class="Polaris-RadioButton__Backdrop"></span>
<span data-test-radio-button-icon class="Polaris-RadioButton__Icon"></span>
<span data-test-radio-button-backdrop="true" class="Polaris-RadioButton__Backdrop"></span>
<span data-test-radio-button-icon="true" class="Polaris-RadioButton__Icon"></span>
</span>
{{/polaris-choice}}
</PolarisChoice>