-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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 removal functionality/styling. (#4912)
* 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 #120. Closes #3143.
- Loading branch information
1 parent
243b97d
commit c82aca9
Showing
14 changed files
with
1,087 additions
and
265 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
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,120 @@ | ||
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 {Directionality} from '../core'; | ||
import {createKeyboardEvent} from '@angular/cdk/testing'; | ||
|
||
import {ENTER} from '../core/keyboard/keycodes'; | ||
|
||
const COMMA = 188; | ||
|
||
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: Directionality, 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 (chipEnd) on enter keyup', () => { | ||
let ENTER_EVENT = createKeyboardEvent('keydown', ENTER, inputNativeElement) as any; | ||
|
||
spyOn(testChipInput, 'add'); | ||
|
||
chipInputDirective._keydown(ENTER_EVENT); | ||
expect(testChipInput.add).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('[addOnBlur]', () => { | ||
it('allows (chipEnd) when true', () => { | ||
spyOn(testChipInput, 'add'); | ||
|
||
testChipInput.addOnBlur = true; | ||
fixture.detectChanges(); | ||
|
||
chipInputDirective._blur(); | ||
expect(testChipInput.add).toHaveBeenCalled(); | ||
}); | ||
|
||
it('disallows (chipEnd) when false', () => { | ||
spyOn(testChipInput, 'add'); | ||
|
||
testChipInput.addOnBlur = false; | ||
fixture.detectChanges(); | ||
|
||
chipInputDirective._blur(); | ||
expect(testChipInput.add).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('[separatorKeysCodes]', () => { | ||
it('does not emit (chipEnd) when a non-separator key is pressed', () => { | ||
let ENTER_EVENT = createKeyboardEvent('keydown', ENTER, inputNativeElement) as any; | ||
spyOn(testChipInput, 'add'); | ||
|
||
testChipInput.separatorKeys = [COMMA]; | ||
fixture.detectChanges(); | ||
|
||
chipInputDirective._keydown(ENTER_EVENT); | ||
expect(testChipInput.add).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('emits (chipEnd) when a custom separator keys is pressed', () => { | ||
let COMMA_EVENT = createKeyboardEvent('keydown', 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 #chipList> | ||
</md-chip-list> | ||
<input mdInput [mdChipInputFor]="chipList" | ||
[mdChipInputAddOnBlur]="addOnBlur" | ||
[mdChipInputSeparatorKeyCodes]="separatorKeys" | ||
(mdChipInputTokenEnd)="add($event)" /> | ||
` | ||
}) | ||
class TestChipInput { | ||
addOnBlur: boolean = false; | ||
separatorKeys: number[] = [ENTER]; | ||
|
||
add(_: MdChipInputEvent) { | ||
} | ||
} |
Oops, something went wrong.