Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit cf3b664

Browse files
fix(base): Add EDITING and EDITABLE states to the chip.
PiperOrigin-RevId: 314367772
1 parent ba6f7c2 commit cf3b664

File tree

5 files changed

+240
-22
lines changed

5 files changed

+240
-22
lines changed

packages/mdc-chips/chip/adapter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ export interface MDCChipAdapter {
9393
*/
9494
notifyNavigation(key: string, source: EventSource): void;
9595

96+
/**
97+
* Emits when editing starts.
98+
*/
99+
notifyEditStart(): void;
100+
101+
/**
102+
* Emits when editing finishes.
103+
*/
104+
notifyEditFinish(): void;
105+
96106
/**
97107
* @return The computed property value of the given style property on the root element.
98108
*/

packages/mdc-chips/chip/component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ export class MDCChip extends MDCComponent<MDCChipFoundation> implements MDCRippl
267267
this.emit<MDCChipInteractionEventDetail>(
268268
strings.TRAILING_ICON_INTERACTION_EVENT, {chipId: this.id},
269269
true /* shouldBubble */),
270+
notifyEditStart: () => { /* Not Implemented. */ },
271+
notifyEditFinish: () => { /* Not Implemented. */ },
270272
removeClass: (className) => this.root.classList.remove(className),
271273
removeClassFromLeadingIcon: (className) => {
272274
if (this.leadingIcon_) {

packages/mdc-chips/chip/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export const cssClasses = {
6969
CHECKMARK: 'mdc-chip__checkmark',
7070
CHIP_EXIT: 'mdc-chip--exit',
7171
DELETABLE: 'mdc-chip--deletable',
72+
EDITABLE: 'mdc-chip--editable',
73+
EDITING: 'mdc-chip--editing',
7274
HIDDEN_LEADING_ICON: 'mdc-chip__icon--leading-hidden',
7375
LEADING_ICON: 'mdc-chip__icon--leading',
7476
PRIMARY_ACTION: 'mdc-chip__primary-action',

packages/mdc-chips/chip/foundation.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
6666
hasLeadingIcon: () => false,
6767
isRTL: () => false,
6868
isTrailingActionNavigable: () => false,
69+
notifyEditFinish: () => undefined,
70+
notifyEditStart: () => undefined,
6971
notifyInteraction: () => undefined,
7072
notifyNavigation: () => undefined,
7173
notifyRemoval: () => undefined,
@@ -97,6 +99,14 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
9799
return this.adapter.hasClass(cssClasses.SELECTED);
98100
}
99101

102+
isEditable() {
103+
return this.adapter.hasClass(cssClasses.EDITABLE);
104+
}
105+
106+
isEditing() {
107+
return this.adapter.hasClass(cssClasses.EDITING);
108+
}
109+
100110
setSelected(selected: boolean) {
101111
this.setSelected_(selected);
102112
this.notifySelection_(selected);
@@ -162,6 +172,12 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
162172
this.setPrimaryActionFocusable_(this.getFocusBehavior_());
163173
}
164174

175+
handleDoubleClick() {
176+
if (this.isEditable()) {
177+
this.startEditing();
178+
}
179+
}
180+
165181
/**
166182
* Handles a transition end event on the root element.
167183
*/
@@ -239,6 +255,10 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
239255
return;
240256
}
241257

258+
if (this.isEditing()) {
259+
this.finishEditing();
260+
}
261+
242262
this.adapter.removeClass(cssClasses.PRIMARY_ACTION_FOCUSED);
243263
}
244264

@@ -255,6 +275,23 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
255275
* Handles a keydown event from the root element.
256276
*/
257277
handleKeydown(evt: KeyboardEvent) {
278+
if (this.isEditing()) {
279+
if (this.shouldFinishEditing(evt)) {
280+
evt.preventDefault();
281+
this.finishEditing();
282+
}
283+
// When editing, the foundation should only handle key events that finish
284+
// the editing process.
285+
return;
286+
}
287+
288+
if (this.isEditable()) {
289+
if (this.shouldStartEditing(evt)) {
290+
evt.preventDefault();
291+
this.startEditing();
292+
}
293+
}
294+
258295
if (this.shouldNotifyInteraction_(evt)) {
259296
this.adapter.notifyInteraction();
260297
this.setPrimaryActionFocusable_(this.getFocusBehavior_());
@@ -366,6 +403,14 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
366403
}
367404
}
368405

406+
private shouldStartEditing(evt: KeyboardEvent): boolean {
407+
return this.eventFromPrimaryAction_(evt) && evt.key === strings.ENTER_KEY;
408+
}
409+
410+
private shouldFinishEditing(evt: KeyboardEvent): boolean {
411+
return evt.key === strings.ENTER_KEY;
412+
}
413+
369414
private shouldNotifyInteraction_(evt: KeyboardEvent): boolean {
370415
return evt.key === strings.ENTER_KEY || evt.key === strings.SPACEBAR_KEY;
371416
}
@@ -399,6 +444,16 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
399444
return this.adapter.eventTargetHasClass(
400445
evt.target, cssClasses.PRIMARY_ACTION);
401446
}
447+
448+
private startEditing() {
449+
this.adapter.addClass(cssClasses.EDITING);
450+
this.adapter.notifyEditStart();
451+
}
452+
453+
private finishEditing() {
454+
this.adapter.removeClass(cssClasses.EDITING);
455+
this.adapter.notifyEditFinish();
456+
}
402457
}
403458

404459
// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.

0 commit comments

Comments
 (0)