Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 64cefc8

Browse files
devversionThomasBurleson
authored andcommitted
feat(chips): md-max-chips to specify a maximum of chips that can be added through user input
Current UX provides no indication of when max chips has been reached. * use ngMessages to show information at max limit condition. Fixes #6864. Closes #6897. # Conflicts: # src/components/chips/chips.spec.js # src/components/chips/js/chipsController.js
1 parent ed4b20f commit 64cefc8

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/components/chips/demoBasicUsage/index.html

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
<md-content class="md-padding" layout="column">
44
<h2 class="md-title">Use a custom chip template.</h2>
55

6-
<md-chips ng-model="ctrl.roFruitNames" readonly="ctrl.readonly">
7-
<md-chip-template>
8-
<strong>{{$chip}}</strong>
9-
<em>(fruit)</em>
10-
</md-chip-template>
11-
</md-chips>
6+
<form name="fruitForm">
7+
<md-chips ng-model="ctrl.roFruitNames" name="fruitName" readonly="ctrl.readonly" md-max-chips="5">
8+
<md-chip-template>
9+
<strong>{{$chip}}</strong>
10+
<em>(fruit)</em>
11+
</md-chip-template>
12+
</md-chips>
13+
14+
<div class="errors" ng-messages="fruitForm.fruitName.$error">
15+
<div ng-message="md-max-chips">The maxmium of chips is reached.</div>
16+
</div>
17+
</form>
18+
1219

1320
<br/>
1421
<h2 class="md-title">Use the default chip template.</h2>

src/components/chips/demoBasicUsage/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function () {
22
'use strict';
33
angular
4-
.module('chipsDemo', ['ngMaterial'])
4+
.module('chipsDemo', ['ngMaterial', 'ngMessages'])
55
.controller('BasicDemoCtrl', DemoCtrl);
66

77
function DemoCtrl ($timeout, $q) {

src/components/chips/demoBasicUsage/style.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
.errors {
2+
font-size: 12px;
3+
color: rgb(221,44,0);
4+
margin-top: 10px;
5+
}
6+
17
.custom-chips {
28
.md-chip {
39
position: relative;

src/components/chips/js/chipsDirective.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
* displayed when there is at least one item in the list
6868
* @param {boolean=} readonly Disables list manipulation (deleting or adding list items), hiding
6969
* the input and delete buttons
70+
* @param {number=} md-max-chips The maximum number of chips allowed to add through user input.
71+
* <br/><br/>The validation property `md-max-chips` can be used when the max chips
72+
* amount is reached.
7073
* @param {expression} md-transform-chip An expression of form `myFunction($chip)` that when called
7174
* expects one of the following return values:
7275
* - an object representing the `$chip` input string
@@ -94,6 +97,23 @@
9497
* </md-chips>
9598
* </hljs>
9699
*
100+
* <h3>Validation</h3>
101+
* When using [ngMessages](https://docs.angularjs.org/api/ngMessages), you can show errors based
102+
* on our custom validators.
103+
* <hljs lang="html">
104+
* <form name="userForm">
105+
* <md-chips
106+
* name="fruits"
107+
* ng-model="myItems"
108+
* placeholder="Add an item"
109+
* md-max-chips="5">
110+
* </md-chips>
111+
* <div ng-messages="userForm.fruits.$error" ng-if="userForm.$dirty">
112+
* <div ng-message="md-max-chips">You reached the maximum amount of chips</div>
113+
* </div>
114+
* </form>
115+
* </hljs>
116+
*
97117
*/
98118

99119

@@ -177,6 +197,7 @@
177197
readonly: '=readonly',
178198
placeholder: '@',
179199
secondaryPlaceholder: '@',
200+
maxChips: '@mdMaxChips',
180201
transformChip: '&mdTransformChip',
181202
onAppend: '&mdOnAppend',
182203
onAdd: '&mdOnAdd',

0 commit comments

Comments
 (0)