Skip to content

Commit a1f9028

Browse files
authored
feat(checkbox, radio): change align to labelPosition (inverted) (#2289)
1 parent 28691ca commit a1f9028

File tree

7 files changed

+71
-26
lines changed

7 files changed

+71
-26
lines changed

src/lib/checkbox/checkbox.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ md-checkbox {
278278
transform: scaleX(0) rotate(0deg);
279279
}
280280

281-
.md-checkbox-align-end {
281+
.md-checkbox-label-before {
282282
.md-checkbox-inner-container {
283283
order: 1;
284284
margin: {

src/lib/checkbox/checkbox.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ describe('MdCheckbox', () => {
213213
expect(inputElement.tabIndex).toBe(0);
214214
});
215215

216-
it('should add a css class to end-align the checkbox', () => {
217-
testComponent.alignment = 'end';
216+
it('should add a css class to position the label before the checkbox', () => {
217+
testComponent.labelPos = 'before';
218218
fixture.detectChanges();
219219

220-
expect(checkboxNativeElement.classList).toContain('md-checkbox-align-end');
220+
expect(checkboxNativeElement.classList).toContain('md-checkbox-label-before');
221221
});
222222

223223
it('should not trigger the click event multiple times', () => {
@@ -626,7 +626,7 @@ describe('MdCheckbox', () => {
626626
<md-checkbox
627627
id="simple-check"
628628
[required]="isRequired"
629-
[align]="alignment"
629+
[labelPosition]="labelPos"
630630
[checked]="isChecked"
631631
[indeterminate]="isIndeterminate"
632632
[disabled]="isDisabled"
@@ -639,7 +639,7 @@ describe('MdCheckbox', () => {
639639
</div>`
640640
})
641641
class SingleCheckbox {
642-
alignment: string = 'start';
642+
labelPos: 'before' | 'after' = 'after';
643643
isChecked: boolean = false;
644644
isRequired: boolean = false;
645645
isIndeterminate: boolean = false;

src/lib/checkbox/checkbox.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class MdCheckboxChange {
7272
'[class.md-checkbox-indeterminate]': 'indeterminate',
7373
'[class.md-checkbox-checked]': 'checked',
7474
'[class.md-checkbox-disabled]': 'disabled',
75-
'[class.md-checkbox-align-end]': 'align == "end"',
75+
'[class.md-checkbox-label-before]': 'labelPosition == "before"',
7676
'[class.md-checkbox-focused]': '_hasFocus',
7777
},
7878
providers: [MD_CHECKBOX_CONTROL_VALUE_ACCESSOR],
@@ -114,8 +114,23 @@ export class MdCheckbox implements ControlValueAccessor {
114114
get required(): boolean { return this._required; }
115115
set required(value) { this._required = coerceBooleanProperty(value); }
116116

117-
/** Whether or not the checkbox should come before or after the label. */
118-
@Input() align: 'start' | 'end' = 'start';
117+
/**
118+
* Whether or not the checkbox should appear before or after the label.
119+
* @deprecated
120+
*/
121+
@Input()
122+
get align(): 'start' | 'end' {
123+
// align refers to the checkbox relative to the label, while labelPosition refers to the
124+
// label relative to the checkbox. As such, they are inverted.
125+
return this.labelPosition == 'after' ? 'start' : 'end';
126+
}
127+
128+
set align(v) {
129+
this.labelPosition = (v == 'start') ? 'after' : 'before';
130+
}
131+
132+
/** Whether the label should appear after or before the checkbox. Defaults to 'after' */
133+
@Input() labelPosition: 'before' | 'after' = 'after';
119134

120135
private _disabled: boolean = false;
121136

src/lib/radio/radio.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
(click)="_onInputClick($event)">
2626

2727
<!-- The label content for radio control. -->
28-
<div class="md-radio-label-content" [class.md-radio-align-end]="align == 'after'">
28+
<div class="md-radio-label-content" [class.md-radio-label-before]="labelPosition == 'before'">
2929
<ng-content></ng-content>
3030
</div>
3131
</label>

src/lib/radio/radio.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ md-radio-button {
7474
}
7575

7676
// Alignment.
77-
.md-radio-label-content.md-radio-align-end {
77+
.md-radio-label-content.md-radio-label-before {
7878
order: -1;
7979
padding-left: 0;
8080
padding-right: $md-toggle-padding;

src/lib/radio/radio.spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ describe('MdRadio', () => {
7070
expect(radioInstances[0].checked).toBe(false);
7171
});
7272

73-
it('should set alignment based on the group alignment', () => {
74-
testComponent.alignment = 'end';
73+
it('should set label position based on the group labelPosition', () => {
74+
testComponent.labelPos = 'before';
7575
fixture.detectChanges();
7676

7777
for (let radio of radioInstances) {
78-
expect(radio.align).toBe('end');
78+
expect(radio.labelPosition).toBe('before');
7979
}
8080

81-
testComponent.alignment = 'start';
81+
testComponent.labelPos = 'after';
8282
fixture.detectChanges();
8383

8484
for (let radio of radioInstances) {
85-
expect(radio.align).toBe('start');
85+
expect(radio.labelPosition).toBe('after');
8686
}
8787
});
8888

@@ -586,7 +586,7 @@ describe('MdRadio', () => {
586586
@Component({
587587
template: `
588588
<md-radio-group [disabled]="isGroupDisabled"
589-
[align]="alignment"
589+
[labelPosition]="labelPos"
590590
[value]="groupValue"
591591
name="test-name">
592592
<md-radio-button value="fire" [disableRipple]="disableRipple">Charmander</md-radio-button>
@@ -596,7 +596,7 @@ describe('MdRadio', () => {
596596
`
597597
})
598598
class RadiosInsideRadioGroup {
599-
alignment: string;
599+
labelPos: 'before' | 'after';
600600
isGroupDisabled: boolean = false;
601601
groupValue: string = null;
602602
disableRipple: boolean = false;

src/lib/radio/radio.ts

+38-8
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,23 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
111111
this._updateRadioButtonNames();
112112
}
113113

114-
/** Alignment of the radio-buttons relative to their labels. Can be 'before' or 'after'. */
115-
@Input() align: 'before' | 'after';
114+
/**
115+
* Alignment of the radio-buttons relative to their labels. Can be 'before' or 'after'.
116+
* @deprecated
117+
*/
118+
@Input()
119+
get align(): 'start' | 'end' {
120+
// align refers to the checkbox relative to the label, while labelPosition refers to the
121+
// label relative to the checkbox. As such, they are inverted.
122+
return this.labelPosition == 'after' ? 'start' : 'end';
123+
}
124+
125+
set align(v) {
126+
this.labelPosition = (v == 'start') ? 'after' : 'before';
127+
}
128+
129+
/** Whether the labels should appear after or before the radio-buttons. Defaults to 'after' */
130+
@Input() labelPosition: 'before' | 'after' = 'after';
116131

117132
@Input()
118133
get disabled(): boolean {
@@ -363,16 +378,31 @@ export class MdRadioButton implements OnInit {
363378
}
364379
}
365380

366-
private _align: 'before' | 'after';
381+
/**
382+
* Whether or not the radio-button should appear before or after the label.
383+
* @deprecated
384+
*/
385+
@Input()
386+
get align(): 'start' | 'end' {
387+
// align refers to the checkbox relative to the label, while labelPosition refers to the
388+
// label relative to the checkbox. As such, they are inverted.
389+
return this.labelPosition == 'after' ? 'start' : 'end';
390+
}
391+
392+
set align(v) {
393+
this.labelPosition = (v == 'start') ? 'after' : 'before';
394+
}
395+
396+
private _labelPosition: 'before' | 'after';
367397

368-
/** Alignment of the radio-button relative to their labels. Can be 'before' or 'after'. */
398+
/** Whether the label should appear after or before the radio button. Defaults to 'after' */
369399
@Input()
370-
get align(): 'before' | 'after' {
371-
return this._align || (this.radioGroup != null && this.radioGroup.align) || 'before';
400+
get labelPosition(): 'before' | 'after' {
401+
return this._labelPosition || (this.radioGroup && this.radioGroup.labelPosition) || 'after';
372402
}
373403

374-
set align(value: 'before' | 'after') {
375-
this._align = value;
404+
set labelPosition(value) {
405+
this._labelPosition = value;
376406
}
377407

378408
/** Whether the radio button is disabled. */

0 commit comments

Comments
 (0)