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(chips): Add remove functionality/styling. #2476

Closed
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
39 changes: 31 additions & 8 deletions src/demo-app/chips/chips-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ <h4>Advanced</h4>

<md-chip-list selectable="false">
<md-chip color="accent" selected="true">Selected/Colored</md-chip>

<md-chip color="warn" selected="true" *ngIf="visible"
(destroy)="alert('chip destroyed')" (click)="toggleVisible()">
(destroy)="alert('chip destroyed')" (remove)="toggleVisible()">
<md-icon md-chip-remove>cancel</md-icon>
Copy link
Member

Choose a reason for hiding this comment

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

Using the md-icon element directly here feels a little weird to me. Couple of thoughts:

  • I bet we can draw an x with css and avoid the need for the icon entirely.
  • md-icon has its own associated role and aria attributes, which may interfere with the chip's option-ness
  • I originally thought this should be a button element (since it's an action), but now I'm thinking that's probably not the case, since a screen-reader user will probably want to see the chip as an atomic option.

So, what do you think of changing this so that there's two directives:

  1. <md-chip-remove>, which has its own x icon (potentially drawn with css)
  2. [mdChipRemove] as an attribute that you apply when you want to do your own thing (no styles, only behavior)
    ?

With Events
</md-chip>
</md-chip-list>
Expand All @@ -37,16 +39,37 @@ <h4>Advanced</h4>
<md-card-content>
<h4>Input Container</h4>

<md-chip-list>
<md-chip *ngFor="let person of people" [color]="color">
{{person.name}}
</md-chip>
</md-chip-list>
<p>
You can easily put the the <code>&lt;md-chip-list&gt;</code> inside of an
<code>&lt;md-input-container&gt;</code>.
</p>

<md-input-container [floatPlaceholder]="people.length > 0 ? 'always' : 'auto'">
<md-chip-list>
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
[removable]="removable" (remove)="remove(person)">
{{person.name}}
<md-icon mdChipRemove>cancel</md-icon>
</md-chip>

<md-input-container>
<input mdInput #input (keyup.enter)="add(input)" placeholder="New Contributor..."/>
<input mdInput placeholder="New Contributor..."
mdChipInput (chipAdded)="add($event)"
[separatorKeys]="separatorKeys" [addOnBlur]="addOnBlur" />
</md-chip-list>
</md-input-container>

<p>
The example above has overridden the <code>[separatorKeys]</code> input to allow for
<code>ENTER</code>, <code>COMMA</code> and <code>SEMI COLON</code> keys.
</p>

<h4>Options</h4>
<p>
<md-checkbox name="selectable" [(ngModel)]="selectable">Selectable</md-checkbox>
<md-checkbox name="removable" [(ngModel)]="removable">Removable</md-checkbox>
<md-checkbox name="addOnBlur" [(ngModel)]="addOnBlur">Add on Blur</md-checkbox>
</p>

<h4>Stacked Chips</h4>

<p>
Expand Down
4 changes: 4 additions & 0 deletions src/demo-app/chips/chips-demo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
.mat-basic-chip {
margin: auto 10px;
}

md-chip-list input {
width: 150px;
}
}
33 changes: 28 additions & 5 deletions src/demo-app/chips/chips-demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Component, ElementRef} from '@angular/core';
import {Component} from '@angular/core';
import {MdChipInputEvent, ENTER, COMMA} from '@angular/material';

export interface Person {
name: string;
Expand All @@ -18,6 +19,12 @@ export interface DemoColor {
export class ChipsDemo {
visible: boolean = true;
color: string = '';
selectable: boolean = true;
removable: boolean = true;
addOnBlur: boolean = true;

// Enter, comma, semi-colon
separatorKeys = [ENTER, COMMA, 186];

people: Person[] = [
{ name: 'Kara' },
Expand All @@ -39,10 +46,26 @@ export class ChipsDemo {
alert(message);
}

add(input: ElementRef): void {
if (input.nativeElement.value && input.nativeElement.value.trim() != '') {
this.people.push({ name: input.nativeElement.value.trim() });
input.nativeElement.value = '';
add(event: MdChipInputEvent): void {
let input = event.input;
let value = event.value;

// Add our person
if (value && value.trim() != '') {
this.people.push({ name: value.trim() });
}

// Reset the input value
if (input) {
input.value = '';
}
}

remove(person: Person): void {
let index = this.people.indexOf(person);

if (index >= 0) {
this.people.splice(index, 1);
}
}

Expand Down
58 changes: 57 additions & 1 deletion src/lib/chips/_chips-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,90 @@

// The spec only provides guidance for light-themed chips. When inside of a dark theme, fall back
// to standard background and foreground colors.
$unselected-background: if($is-dark-theme, mat-color($background, card), #e0e0e0);
$unselected-background: if($is-dark-theme, #656565, #e0e0e0);
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment for the origin of #656565?

$unselected-foreground: if($is-dark-theme, mat-color($foreground, text), $light-foreground);

$selected-background: if($is-dark-theme, mat-color($background, app-bar), #808080);
$selected-foreground: if($is-dark-theme, mat-color($foreground, text), $light-selected-foreground);

$focus-color: mat-color($foreground, secondary-text);

.mat-chip:not(.mat-basic-chip) {
background-color: $unselected-background;
color: $unselected-foreground;

.mat-chip-focus-border {
pointer-events: none;
}

&:focus {
outline: none;
border: 2px solid $focus-color;
}

.mat-chip-remove {
color: $unselected-foreground;
opacity: 0.3;

&:hover {
opacity: 0.54;
}
Copy link
Member

Choose a reason for hiding this comment

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

Any way to generally reduce the nesting in this file?

}
}

.mat-chip.mat-chip-selected:not(.mat-basic-chip) {
background-color: $selected-background;
color: $selected-foreground;

.mat-chip-remove {
color: $selected-foreground;
opacity: 0.4;

&:hover {
opacity: 0.54;
}
}

&.mat-primary {
background-color: mat-color($primary, 500);
color: mat-contrast($primary, 500);

.mat-chip-remove {
color: mat-contrast($primary, 500);
opacity: 0.4;

&:hover {
opacity: 0.54;
}
}
}

&.mat-accent {
background-color: mat-color($accent, 500);
color: mat-contrast($accent, 500);

.mat-chip-remove {
color: mat-contrast($accent, 500);
opacity: 0.4;

&:hover {
opacity: 0.54;
}
}
}

&.mat-warn {
background-color: mat-color($warn, 500);
color: mat-contrast($warn, 500);

.mat-chip-remove {
color: mat-contrast($warn, 500);
opacity: 0.4;

&:hover {
opacity: 0.54;
}
}
}
}
}
115 changes: 115 additions & 0 deletions src/lib/chips/chip-input.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import {async, TestBed, ComponentFixture} from '@angular/core/testing';
import {MdChipsModule} from './index';
import {Component, DebugElement} from '@angular/core';
import {MdChipInput, MdChipInputEvent} from './chip-input';
import {By} from '@angular/platform-browser';
import {Dir} from '../core/rtl/dir';
import {FakeKeyboardEvent} from './chip-list.spec';
import {ENTER, COMMA} from '../core/keyboard/keycodes';

describe('MdChipInput', () => {
let fixture: ComponentFixture<any>;
let testChipInput: TestChipInput;
let inputDebugElement: DebugElement;
let inputNativeElement: HTMLElement;
let chipInputDirective: MdChipInput;

let dir = 'ltr';

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdChipsModule],
declarations: [TestChipInput],
providers: [{
provide: Dir, useFactory: () => {
return {value: dir.toLowerCase()};
}
}]
});

TestBed.compileComponents();
}));

beforeEach(async(() => {
fixture = TestBed.createComponent(TestChipInput);
testChipInput = fixture.debugElement.componentInstance;
fixture.detectChanges();

inputDebugElement = fixture.debugElement.query(By.directive(MdChipInput));
chipInputDirective = inputDebugElement.injector.get(MdChipInput) as MdChipInput;
inputNativeElement = inputDebugElement.nativeElement;
}));

describe('basic behavior', () => {
it('emits the (chipAdded) on enter keyup', () => {
let ENTER_EVENT = new FakeKeyboardEvent(ENTER, inputNativeElement) as any;

spyOn(testChipInput, 'add');

chipInputDirective._keydown(ENTER_EVENT);
expect(testChipInput.add).toHaveBeenCalled();
});
});

describe('[addOnBlur]', () => {
it('allows (chipAdded) when true', () => {
spyOn(testChipInput, 'add');

testChipInput.addOnBlur = true;
fixture.detectChanges();

chipInputDirective._blur();
expect(testChipInput.add).toHaveBeenCalled();
});

it('disallows (chipAdded) when false', () => {
spyOn(testChipInput, 'add');

testChipInput.addOnBlur = false;
fixture.detectChanges();

chipInputDirective._blur();
expect(testChipInput.add).not.toHaveBeenCalled();
});
});

describe('[separatorKeys]', () => {
it('does not emit (chipAdded) when a non-separator key is pressed', () => {
let ENTER_EVENT = new FakeKeyboardEvent(ENTER, inputNativeElement) as any;
spyOn(testChipInput, 'add');

testChipInput.separatorKeys = [COMMA];
fixture.detectChanges();

chipInputDirective._keydown(ENTER_EVENT);
expect(testChipInput.add).not.toHaveBeenCalled();
});

it('emits (chipAdded) when a custom separator keys is pressed', () => {
let COMMA_EVENT = new FakeKeyboardEvent(COMMA, inputNativeElement) as any;
spyOn(testChipInput, 'add');

testChipInput.separatorKeys = [COMMA];
fixture.detectChanges();

chipInputDirective._keydown(COMMA_EVENT);
expect(testChipInput.add).toHaveBeenCalled();
});
});
});

@Component({
template: `
<md-chip-list>
<input mdChipInput [addOnBlur]="addOnBlur" [separatorKeys]="separatorKeys"
(chipAdded)="add($event)" />
</md-chip-list>
`
})
class TestChipInput {
addOnBlur: boolean = false;
separatorKeys: number[] = [ENTER];

add(event: MdChipInputEvent) {
}
}
Loading