Skip to content

Commit dd2f0a0

Browse files
committed
address comments
1 parent dbb98c4 commit dd2f0a0

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

src/lib/chips/chip-input.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,8 @@ export interface MdChipInputEvent {
3434
}
3535

3636
/**
37-
* A material design chips input component working with chip list component.
38-
* To be placed inside <md-form-field>. Can be placed inside <md-chip-list> or outside
39-
* <md-chip-list>.
40-
*
41-
* Example:
42-
* <md-form-field>
43-
* <md-chip-list #chipList>
44-
* <md-chip>Chip 1<md-chip>
45-
* <md-chip>Chip 2<md-chip>
46-
* </md-chip-list>
47-
* <input mdChipInputFor="chipList">
48-
* </md-form-field>
49-
*
50-
* <md-form-field>
51-
* <md-chip-list #chipList>
52-
* <md-chip>Chip 1<md-chip>
53-
* <md-chip>Chip 2<md-chip>
54-
* <input mdChipInputFor="chipList">
55-
* </md-chip-list>
56-
* </md-form-field>
37+
* Directive that adds chip-specific behaviors to an input element inside <md-form-field>.
38+
* May be placed inside or outside of an <md-chip-list>.
5739
*/
5840
@Directive({
5941
selector: 'input[mdChipInputFor], input[matChipInputFor]',

src/lib/chips/chip-list.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class MdChipListChange {
7171
'[attr.aria-required]': 'required.toString()',
7272
'[attr.aria-disabled]': 'disabled.toString()',
7373
'[attr.aria-invalid]': 'errorState',
74+
'[attr.aria-multiselectable]': 'true',
7475
'[class.mat-chip-list-disabled]': 'disabled',
7576
'[class.mat-chip-list-invalid]': 'errorState',
7677
'[class.mat-chip-list-required]': 'required',
@@ -164,6 +165,7 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
164165
/** Comparison function to specify which option is displayed. Defaults to object equality. */
165166
private _compareWith = (o1: any, o2: any) => o1 === o2;
166167

168+
/** The array of selected chips inside chip list. */
167169
get selected(): MdChip[] {
168170
return this._selectionModel.selected;
169171
}
@@ -219,22 +221,23 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
219221
return this._chipInput ? this._chipInput.placeholder : this._placeholder;
220222
}
221223

222-
/** For FormFieldControl. If any of the chips has focus, or the chip input has focus */
224+
/** Whether any chips or the mdChipInput inside of this chip-list has focus. */
223225
get focused() {
224226
return this.chips.some(chip => chip._hasFocus) ||
225227
(this._chipInput && this._chipInput.focused);
226228
}
227229

228-
/** For FormFieldControl. The chip list is empty if there's no chip and there's no input */
230+
/** Whether this chip-list contains no chips and no mdChipInput. */
229231
get empty(): boolean {
230232
return (!this._chipInput || this._chipInput.empty) && this.chips.length === 0;
231233
}
232234

233-
/** For FormFieldControl. The disabled is not depend on chip input */
235+
/** Whether this chip-list is disabled. */
234236
@Input()
235237
get disabled() { return this.ngControl ? this.ngControl.disabled : this._disabled; }
236238
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
237239

240+
/** Whether the chip list is in an error state. */
238241
get errorState(): boolean {
239242
const isInvalid = this.ngControl && this.ngControl.invalid;
240243
const isTouched = this.ngControl && this.ngControl.touched;
@@ -584,22 +587,28 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
584587
this._changeDetectorRef.markForCheck();
585588
}
586589

590+
/** When blurred, mark the field as touched when focus moved outside the chip list. */
587591
_blur() {
588592
if (!this.disabled) {
589593
if (this._chipInput) {
590-
// Check if focus moved to chip input. If not, do real on blur
594+
// If there's a chip input, we should check whether the focus moved to chip input.
595+
// If the focus is not moved to chip input, mark the field as touched. If the focus moved
596+
// to chip input, do nothing.
597+
// Timeout is needed to wait for the focus() event trigger on chip input.
591598
setTimeout(() => {
592599
if (!this.focused) {
593-
this._onBlur();
600+
this._markAsTouched();
594601
}
595602
});
596603
} else {
597-
this._onBlur();
604+
// If there's no chip input, then mark the field as touched.
605+
this._markAsTouched();
598606
}
599607
}
600608
}
601609

602-
_onBlur() {
610+
/** Mark the field as touched */
611+
_markAsTouched() {
603612
this._onTouched();
604613
this._changeDetectorRef.markForCheck();
605614
this.stateChanges.next();

src/lib/chips/chips.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,38 @@ _Hint: `<md-basic-chip>` receives the `mat-basic-chip` CSS class in addition to
2222
Chips can be selected via the `selected` property. Selection can be disabled by setting
2323
`selectable` to `false` on the `<md-chip-list>`.
2424

25-
Selection emits the `(select)` output while deselecting emits the `(deselect)` output. Both outputs
26-
receive a ChipEvent object with a structure of `{ chip: alteredChip }`.
25+
Selection and deselecting emit the `(onSelectionChange)` output. The output receive a
26+
ChipSelectionChange object with a structure of `{ source: alteredChip, isUserInput: boolean }`.
2727

2828
### Disabled chips
2929
Individual chips may be disabled by applying the `disabled` attribute to the chip. When disabled,
3030
chips are neither selectable nor focusable. Currently, disabled chips receive no special styling.
3131

32+
### Chip input
33+
Chip input can work with chip list to add new chips to the chip list. It implements chip-specified
34+
behaviors to an input element inside `<md-form-field>`. Chip input may be placed inside or outside of
35+
an `<md-chip-list>`.
36+
37+
```html
38+
<md-form-field>
39+
<md-chip-list #chipList>
40+
<md-chip>Chip 1<md-chip>
41+
<md-chip>Chip 2<md-chip>
42+
</md-chip-list>
43+
<input mdChipInputFor="chipList">
44+
</md-form-field>
45+
```
46+
47+
```html
48+
<md-form-field>
49+
<md-chip-list #chipList>
50+
<md-chip>Chip 1<md-chip>
51+
<md-chip>Chip 2<md-chip>
52+
<input mdChipInputFor="chipList">
53+
</md-chip-list>
54+
</md-form-field>
55+
```
56+
3257
### Keyboard interaction
3358
Users can move through the chips using the arrow keys and select/deselect them with the space. Chips
3459
also gain focus when clicked, ensuring keyboard navigation starts at the appropriate chip.

0 commit comments

Comments
 (0)