Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(button-toggle): add option for vertical toggle groups #1936

Merged
merged 2 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions src/demo-app/button-toggle/button-toggle-demo.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<p>
<md-checkbox (change)="isVertical = $event.checked">Show Button Toggles Vertical</md-checkbox>
</p>

<h1>Exclusive Selection</h1>

<section class="demo-section">
<md-button-toggle-group name="alignment">
<md-button-toggle-group name="alignment" [vertical]="isVertical">
<md-button-toggle value="left"><md-icon>format_align_left</md-icon></md-button-toggle>
<md-button-toggle value="center"><md-icon>format_align_center</md-icon></md-button-toggle>
<md-button-toggle value="right"><md-icon>format_align_right</md-icon></md-button-toggle>
Expand All @@ -12,7 +16,7 @@ <h1>Exclusive Selection</h1>
<h1>Disabled Group</h1>

<section class="demo-section">
<md-button-toggle-group name="checkbox" disabled>
<md-button-toggle-group name="checkbox" [vertical]="isVertical" disabled>
<md-button-toggle value="bold">
<md-icon class="md-24">format_bold</md-icon>
</md-button-toggle>
Expand All @@ -27,7 +31,7 @@ <h1>Disabled Group</h1>

<h1>Multiple Selection</h1>
<section class="demo-section">
<md-button-toggle-group multiple>
<md-button-toggle-group multiple [vertical]="isVertical">
<md-button-toggle>Flour</md-button-toggle>
<md-button-toggle>Eggs</md-button-toggle>
<md-button-toggle>Sugar</md-button-toggle>
Expand All @@ -40,7 +44,7 @@ <h1>Single Toggle</h1>

<h1>Dynamic Exclusive Selection</h1>
<section class="demo-section">
<md-button-toggle-group name="pies" [(ngModel)]="favoritePie">
<md-button-toggle-group name="pies" [(ngModel)]="favoritePie" [vertical]="isVertical">
<md-button-toggle *ngFor="let pie of pieOptions" [value]="pie">
{{pie}}
</md-button-toggle>
Expand Down
1 change: 1 addition & 0 deletions src/demo-app/button-toggle/button-toggle-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {MdUniqueSelectionDispatcher} from '@angular/material';
providers: [MdUniqueSelectionDispatcher],
})
export class ButtonToggleDemo {
isVertical = false;
favoritePie = 'Apple';
pieOptions = [
'Apple',
Expand Down
1 change: 1 addition & 0 deletions src/lib/button-toggle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Output:
| `disabled` | boolean | Optional, default = `false`. |
| `value` | any | The current value for the group. Will be set to the value of the selected toggle or a list of values from the selected toggles. |
| `selected` | `mdButtonToggle` | The current selected toggle or a list of the selected toggles. |
| `vertical` | boolean | Whether the group should show the toggles vertically. Default is `false`. |

### Attributes

Expand Down
14 changes: 14 additions & 0 deletions src/lib/button-toggle/button-toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ md-button-toggle-group {
@include md-elevation(2);
position: relative;
display: inline-flex;
flex-direction: row;

border-radius: $md-button-toggle-border-radius;

cursor: pointer;
white-space: nowrap;
}

.md-button-toggle-vertical {
flex-direction: column;

.md-button-toggle-label-content {
// Vertical button toggles shouldn't be an inline-block, because the toggles should
// fill the available width in the group.
display: block;
}
}


.md-button-toggle-disabled .md-button-toggle-label-content {
cursor: default;
}
Expand Down
24 changes: 22 additions & 2 deletions src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ describe('MdButtonToggle', () => {
expect(groupInstance.value);
});

it('should change the vertical state', () => {
expect(groupNativeElement.classList).not.toContain('md-button-toggle-vertical');

groupInstance.vertical = true;
fixture.detectChanges();

expect(groupNativeElement.classList).toContain('md-button-toggle-vertical');
});

it('should emit a change event from button toggles', fakeAsync(() => {
expect(buttonToggleInstances[0].checked).toBe(false);

Expand Down Expand Up @@ -412,6 +421,15 @@ describe('MdButtonToggle', () => {
expect(buttonToggleInstances[0].checked).toBe(true);
});

it('should change the vertical state', () => {
expect(groupNativeElement.classList).not.toContain('md-button-toggle-vertical');

groupInstance.vertical = true;
fixture.detectChanges();

expect(groupNativeElement.classList).toContain('md-button-toggle-vertical');
});

it('should deselect a button toggle when selected twice', () => {
buttonToggleNativeElements[0].click();
fixture.detectChanges();
Expand Down Expand Up @@ -506,7 +524,7 @@ describe('MdButtonToggle', () => {

@Component({
template: `
<md-button-toggle-group [disabled]="isGroupDisabled" [value]="groupValue">
<md-button-toggle-group [disabled]="isGroupDisabled" [vertical]="isVertical" [value]="groupValue">
<md-button-toggle value="test1">Test1</md-button-toggle>
<md-button-toggle value="test2">Test2</md-button-toggle>
<md-button-toggle value="test3">Test3</md-button-toggle>
Expand All @@ -515,6 +533,7 @@ describe('MdButtonToggle', () => {
})
class ButtonTogglesInsideButtonToggleGroup {
isGroupDisabled: boolean = false;
isVertical: boolean = false;
groupValue: string = null;
}

Expand All @@ -539,7 +558,7 @@ class ButtonToggleGroupWithNgModel {

@Component({
template: `
<md-button-toggle-group [disabled]="isGroupDisabled" multiple>
<md-button-toggle-group [disabled]="isGroupDisabled" [vertical]="isVertical" multiple>
<md-button-toggle value="eggs">Eggs</md-button-toggle>
<md-button-toggle value="flour">Flour</md-button-toggle>
<md-button-toggle value="sugar">Sugar</md-button-toggle>
Expand All @@ -548,6 +567,7 @@ class ButtonToggleGroupWithNgModel {
})
class ButtonTogglesInsideButtonToggleGroupMultiple {
isGroupDisabled: boolean = false;
isVertical: boolean = false;
}

@Component({
Expand Down
29 changes: 29 additions & 0 deletions src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class MdButtonToggleChange {
providers: [MD_BUTTON_TOGGLE_GROUP_VALUE_ACCESSOR],
host: {
'role': 'radiogroup',
'[class.md-button-toggle-vertical]': 'vertical'
},
exportAs: 'mdButtonToggleGroup',
})
Expand All @@ -60,6 +61,9 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
/** Disables all toggles in the group. */
private _disabled: boolean = null;

/** Whether the button toggle group should be vertical. */
private _vertical: boolean = false;

/** The currently selected button toggle, should match the value. */
private _selected: MdButtonToggle = null;

Expand Down Expand Up @@ -109,6 +113,15 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
this._disabled = coerceBooleanProperty(value);
}

@Input()
get vertical(): boolean {
return this._vertical;
}

set vertical(value) {
this._vertical = coerceBooleanProperty(value);
}

@Input()
get value(): any {
return this._value;
Expand Down Expand Up @@ -207,11 +220,17 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
@Directive({
selector: 'md-button-toggle-group[multiple]',
exportAs: 'mdButtonToggleGroup',
host: {
'[class.md-button-toggle-vertical]': 'vertical'
}
})
export class MdButtonToggleGroupMultiple {
/** Disables all toggles in the group. */
private _disabled: boolean = null;

/** Whether the button toggle group should be vertical. */
private _vertical: boolean = false;

@Input()
get disabled(): boolean {
return this._disabled;
Expand All @@ -220,6 +239,16 @@ export class MdButtonToggleGroupMultiple {
set disabled(value) {
this._disabled = (value != null && value !== false) ? true : null;
}

@Input()
get vertical(): boolean {
return this._vertical;
}

set vertical(value) {
this._vertical = coerceBooleanProperty(value);
}

}

@Component({
Expand Down