forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chips): Add remove functionality/styling.
Add events, styling and keyboard controls to allow removable chips. - Add basic styling for a user-provided remove icon. - Add keyboard controls for backspace/delete. - Add `(remove)` event which is emitted when the remove icon or one of the delete keys is pressed. - Add `md-chip-remove` directive which can be applied to `<md-icon>` (or others) to inform the chip of the remove request. Add new directive `mdChipInput` for controlling: - `(chipAdded)` - Event fired when a chip should be added. - `[separatorKeys]` - The list of keycodes that will fire the `(chipAdded)` event. - `[addOnBlur]` - Whether or not to fire the `(chipAdded)` event when the input is blurred. Additionally, fix some issues with dark theme and add styling/support for usage inside the `md-input-container` and add styling for focused chips. BREAKING CHANGE - The `selectable` property of the `md-chip-list` has now been moved to `md-chip` to maintain consistency with the new `removable` option. If you used the following code, ```html <md-chip-list [selectable]="selectable"> <md-chip>My Chip</md-chip> </md-chip-list> ``` you should switch it to ```html <md-chip-list> <md-chip [selectable]="selectable">My Chip</md-chip> </md-chip-list> ``` References angular#120. Closes angular#3143. Hat tip to @willshowell for suggestion to use secondary-text for focus color :-)
- Loading branch information
1 parent
ce0e933
commit 2a5819f
Showing
15 changed files
with
1,173 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,8 @@ | |
.mat-basic-chip { | ||
margin: auto 10px; | ||
} | ||
|
||
md-chip-list input { | ||
width: 150px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
Oops, something went wrong.