-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(material-experimental/mdc-chips): Make chips editable by connecting to the mdc web editing interface #19618
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e2b412b
Stop arrow key navigation through matChipRemove directives from delet…
5fc8cff
Make the MDC MatChipRow editable
7c3a7d5
Fixes to demo page for editable chips
ddf746b
Fix linter issues
c6cc7d7
Fix incorrect host input param
31be702
Fix chip grid test
8c5d3e1
Simplify chip grid test
b0cf17c
Merge branch 'master' into mdc-editable-chip
kowsen bbf240b
Change chip edit span to be a directive instead of a component.
9b716ca
Add tests for fallback MatChipEditInput.
f11ac3b
Merge pull request #2 from kowsen/mdc-editable-alternate
kowsen 8e81881
Fixes for comments on PR.
638a6c0
Fix formatting on new longer selector.
66c5502
Move edit input/output to MatChipRow.
98f370e
Move ngAcceptInputType property to MatChipRow.
4009d91
Fix style issues and tests from review.
eabea48
Vertically center avatar and remove icons to match existing behavior.
b7f068b
Fix selector for vertically centering chip contents.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
src/material-experimental/mdc-chips/chip-edit-input.spec.ts
This file contains hidden or 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,52 @@ | ||
import {Component, DebugElement} from '@angular/core'; | ||
import {async, TestBed, ComponentFixture} from '@angular/core/testing'; | ||
import {MatChipEditInput, MatChipsModule} from './index'; | ||
import {By} from '@angular/platform-browser'; | ||
|
||
|
||
describe('MDC-based MatChipEditInput', () => { | ||
const DEFAULT_INITIAL_VALUE = 'INITIAL_VALUE'; | ||
|
||
let fixture: ComponentFixture<any>; | ||
let inputDebugElement: DebugElement; | ||
let inputInstance: MatChipEditInput; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [MatChipsModule], | ||
declarations: [ | ||
ChipEditInputContainer, | ||
], | ||
}); | ||
|
||
TestBed.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(ChipEditInputContainer); | ||
inputDebugElement = fixture.debugElement.query(By.directive(MatChipEditInput))!; | ||
inputInstance = inputDebugElement.injector.get<MatChipEditInput>(MatChipEditInput); | ||
})); | ||
|
||
describe('on initialization', () => { | ||
it('should set the initial input text', () => { | ||
inputInstance.initialize(DEFAULT_INITIAL_VALUE); | ||
expect(inputInstance.getNativeElement().textContent).toEqual(DEFAULT_INITIAL_VALUE); | ||
}); | ||
|
||
it('should focus the input', () => { | ||
inputInstance.initialize(DEFAULT_INITIAL_VALUE); | ||
expect(document.activeElement).toEqual(inputInstance.getNativeElement()); | ||
}); | ||
}); | ||
|
||
it('should update the internal value as it is set', () => { | ||
inputInstance.initialize(DEFAULT_INITIAL_VALUE); | ||
const newValue = 'NEW_VALUE'; | ||
inputInstance.setValue(newValue); | ||
expect(inputInstance.getValue()).toEqual(newValue); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
template: `<span matChipEditInput></span>`, | ||
}) | ||
class ChipEditInputContainer {} |
This file contains hidden or 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,60 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { | ||
Directive, | ||
ElementRef, | ||
Inject, | ||
} from '@angular/core'; | ||
import {DOCUMENT} from '@angular/common'; | ||
|
||
/** | ||
* A directive that makes a span editable and exposes functions to modify and retrieve the | ||
* element's contents. | ||
*/ | ||
@Directive({ | ||
selector: 'span[matChipEditInput]', | ||
host: { | ||
'class': 'mdc-chip__primary-action mat-chip-edit-input', | ||
'role': 'textbox', | ||
'tabindex': '-1', | ||
'contenteditable': 'true', | ||
}, | ||
}) | ||
export class MatChipEditInput { | ||
constructor( | ||
private readonly _elementRef: ElementRef, | ||
@Inject(DOCUMENT) private readonly _document: any) {} | ||
|
||
initialize(initialValue: string) { | ||
this.getNativeElement().focus(); | ||
this.setValue(initialValue); | ||
} | ||
|
||
getNativeElement(): HTMLElement { | ||
return this._elementRef.nativeElement; | ||
} | ||
|
||
setValue(value: string) { | ||
this.getNativeElement().innerText = value; | ||
this._moveCursorToEndOfInput(); | ||
} | ||
|
||
getValue(): string { | ||
return this.getNativeElement().textContent || ''; | ||
} | ||
|
||
private _moveCursorToEndOfInput() { | ||
const range = this._document.createRange(); | ||
range.selectNodeContents(this.getNativeElement()); | ||
range.collapse(false); | ||
const sel = window.getSelection()!; | ||
sel.removeAllRanges(); | ||
sel.addRange(range); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,19 +1,32 @@ | ||
<span class="mdc-chip__ripple"></span> | ||
<ng-container *ngIf="!_isEditing()"> | ||
<span class="mdc-chip__ripple"></span> | ||
|
||
<span matRipple class="mat-mdc-chip-ripple" | ||
[matRippleAnimation]="_rippleAnimation" | ||
[matRippleDisabled]="_isRippleDisabled()" | ||
[matRippleCentered]="_isRippleCentered" | ||
[matRippleTrigger]="_elementRef.nativeElement"></span> | ||
<span matRipple class="mat-mdc-chip-ripple" | ||
[matRippleAnimation]="_rippleAnimation" | ||
[matRippleDisabled]="_isRippleDisabled()" | ||
[matRippleCentered]="_isRippleCentered" | ||
[matRippleTrigger]="_elementRef.nativeElement"></span> | ||
</ng-container> | ||
|
||
<div role="gridcell"> | ||
<div #chipContent tabindex="-1" | ||
class="mat-chip-row-focusable-text-content mat-mdc-focus-indicator"> | ||
<ng-content select="mat-chip-avatar, [matChipAvatar]"></ng-content> | ||
<span class="mdc-chip__text"><ng-content></ng-content></span> | ||
<ng-content select="mat-chip-trailing-icon,[matChipTrailingIcon]"></ng-content> | ||
<div class="mat-mdc-chip-content"> | ||
<div role="gridcell"> | ||
<div #chipContent tabindex="-1" | ||
class="mat-mdc-chip-row-focusable-text-content mat-mdc-focus-indicator mdc-chip__primary-action" | ||
[attr.role]="editable ? 'button' : null"> | ||
<ng-content select="mat-chip-avatar, [matChipAvatar]"></ng-content> | ||
<span class="mdc-chip__text"><ng-content></ng-content></span> | ||
<ng-content select="mat-chip-trailing-icon,[matChipTrailingIcon]"></ng-content> | ||
</div> | ||
</div> | ||
<div role="gridcell" *ngIf="removeIcon" class="mat-mdc-chip-remove-icon"> | ||
<ng-content select="[matChipRemove]"></ng-content> | ||
</div> | ||
</div> | ||
<div role="gridcell" *ngIf="removeIcon"> | ||
<ng-content select="[matChipRemove]"></ng-content> | ||
</div> | ||
|
||
<div *ngIf="_isEditing()" role="gridcell" class="mat-mdc-chip-edit-input-container"> | ||
<ng-content *ngIf="contentEditInput; else defaultMatChipEditInput" | ||
select="[matChipEditInput]"></ng-content> | ||
<ng-template #defaultMatChipEditInput> | ||
<span matChipEditInput></span> | ||
</ng-template> | ||
</div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jelbourn are you aware of any a11y issues with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really know the a11y nuances of
contenteditable
, so this would require some manual testing