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

Commit

Permalink
feat(chips): md-max-chips to specify a maximum of chips that can be a…
Browse files Browse the repository at this point in the history
…dded 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
  • Loading branch information
devversion authored and ThomasBurleson committed Apr 1, 2016
1 parent ed4b20f commit 64cefc8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/components/chips/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
<md-content class="md-padding" layout="column">
<h2 class="md-title">Use a custom chip template.</h2>

<md-chips ng-model="ctrl.roFruitNames" readonly="ctrl.readonly">
<md-chip-template>
<strong>{{$chip}}</strong>
<em>(fruit)</em>
</md-chip-template>
</md-chips>
<form name="fruitForm">
<md-chips ng-model="ctrl.roFruitNames" name="fruitName" readonly="ctrl.readonly" md-max-chips="5">
<md-chip-template>
<strong>{{$chip}}</strong>
<em>(fruit)</em>
</md-chip-template>
</md-chips>

<div class="errors" ng-messages="fruitForm.fruitName.$error">
<div ng-message="md-max-chips">The maxmium of chips is reached.</div>
</div>
</form>


<br/>
<h2 class="md-title">Use the default chip template.</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/components/chips/demoBasicUsage/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
'use strict';
angular
.module('chipsDemo', ['ngMaterial'])
.module('chipsDemo', ['ngMaterial', 'ngMessages'])
.controller('BasicDemoCtrl', DemoCtrl);

function DemoCtrl ($timeout, $q) {
Expand Down
6 changes: 6 additions & 0 deletions src/components/chips/demoBasicUsage/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.errors {
font-size: 12px;
color: rgb(221,44,0);
margin-top: 10px;
}

.custom-chips {
.md-chip {
position: relative;
Expand Down
21 changes: 21 additions & 0 deletions src/components/chips/js/chipsDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
* displayed when there is at least one item in the list
* @param {boolean=} readonly Disables list manipulation (deleting or adding list items), hiding
* the input and delete buttons
* @param {number=} md-max-chips The maximum number of chips allowed to add through user input.
* <br/><br/>The validation property `md-max-chips` can be used when the max chips
* amount is reached.
* @param {expression} md-transform-chip An expression of form `myFunction($chip)` that when called
* expects one of the following return values:
* - an object representing the `$chip` input string
Expand Down Expand Up @@ -94,6 +97,23 @@
* </md-chips>
* </hljs>
*
* <h3>Validation</h3>
* When using [ngMessages](https://docs.angularjs.org/api/ngMessages), you can show errors based
* on our custom validators.
* <hljs lang="html">
* <form name="userForm">
* <md-chips
* name="fruits"
* ng-model="myItems"
* placeholder="Add an item"
* md-max-chips="5">
* </md-chips>
* <div ng-messages="userForm.fruits.$error" ng-if="userForm.$dirty">
* <div ng-message="md-max-chips">You reached the maximum amount of chips</div>
* </div>
* </form>
* </hljs>
*
*/


Expand Down Expand Up @@ -177,6 +197,7 @@
readonly: '=readonly',
placeholder: '@',
secondaryPlaceholder: '@',
maxChips: '@mdMaxChips',
transformChip: '&mdTransformChip',
onAppend: '&mdOnAppend',
onAdd: '&mdOnAdd',
Expand Down

0 comments on commit 64cefc8

Please sign in to comment.