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

Commit f073f2a

Browse files
author
Andy Tang
committed
Added textarea support for mdTextFloat
``` <md-text-float rows="4" cols="50" type="textarea" label="{{labels.intro}}" ng-model="user.intro"> ```
1 parent bd78f49 commit f073f2a

File tree

3 files changed

+67
-174
lines changed

3 files changed

+67
-174
lines changed

src/components/textField/demoBasicUsage/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<md-text-float label="Postal Code" ng-model="user.postalCode"></md-text-float>
2323
</div>
2424
<md-text-float label="Country" ng-model="user.country" disabled></md-text-float>
25-
<md-textarea-float label="Intro" ng-model="user.intro" rows="4" cols="50"></md-textarea-float>
25+
<md-text-float label="Intro" ng-model="user.intro" rows="4" cols="50" type="textarea"></md-text-float>
2626
</form>
2727
</md-content>
2828

src/components/textField/textField.js

Lines changed: 60 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
angular.module('material.components.textField', ['material.core', 'material.services.theming'])
88
.directive('mdInputGroup', [ mdInputGroupDirective ])
99
.directive('mdInput', ['$mdUtil', mdInputDirective ])
10-
.directive('mdTextarea', ['$mdUtil', mdTextareaDirective])
11-
.directive('mdTextFloat', [ '$mdTheming', '$mdUtil', mdTextFloatDirective ])
12-
.directive('mdTextareaFloat', [ '$mdTheming', '$mdUtil', mdTextareaFloatDirective ]);
10+
.directive('mdTextFloat', [ '$mdTheming', '$mdUtil', mdTextFloatDirective ]);
1311

1412

1513
/**
@@ -36,6 +34,9 @@ angular.module('material.components.textField', ['material.core', 'material.serv
3634
*
3735
* <!-- Specify an input type if desired. -->
3836
* <md-text-float label="eMail" ng-model="user.email" type="email" ></md-text-float>
37+
*
38+
* <!-- Specify textarea as an input type if desired. -->
39+
* <md-text-float label="intro" ng-model="user.intro" type="textarea" ></md-text-float>
3940
* </hljs>
4041
*/
4142
function mdTextFloatDirective($mdTheming, $mdUtil) {
@@ -78,70 +79,6 @@ function mdTextFloatDirective($mdTheming, $mdUtil) {
7879
};
7980
}
8081

81-
/**
82-
* @ngdoc directive
83-
* @name mdTextareaFloat
84-
* @module material.components.textField
85-
*
86-
* @restrict E
87-
*
88-
* @description
89-
* Use the `<md-textarea-float>` directive to quickly construct `Floating Label` textarea fields
90-
*
91-
* @param {string} fid Attribute used for accessibility link pairing between the Label and Input elements
92-
* @param {string=} rows Optional value to define the amount of rows.
93-
* @param {string=} cols Optional value to define the amount of cols.
94-
* @param {string} label Attribute to specify the input text field hint.
95-
* @param {string=} ng-model Optional value to assign as existing input text string
96-
*
97-
* @usage
98-
* <hljs lang="html">
99-
* <md-textarea-float label="Intro" ng-model="user.intro" > </md-textarea-float>
100-
*
101-
* <!-- Specify the amount of rows and cols. -->
102-
* <md-textarea-float label="Company" ng-model="user.company" rows="4" cols="50" > </md-textarea-float>
103-
* </hljs>
104-
*/
105-
function mdTextareaFloatDirective($mdTheming, $mdUtil) {
106-
return {
107-
restrict: 'E',
108-
replace: true,
109-
scope: {
110-
fid: '@?',
111-
label: '@?',
112-
rows: '@?',
113-
cols: '@?',
114-
value: '=ngModel'
115-
},
116-
compile: function (element, attr) {
117-
118-
if (angular.isUndefined(attr.fid)) {
119-
attr.fid = $mdUtil.nextUid();
120-
}
121-
122-
return {
123-
pre: function (scope, element, attrs) {
124-
// transpose `disabled` flag
125-
if (angular.isDefined(attrs.disabled)) {
126-
element.attr('disabled', true);
127-
scope.isDisabled = true;
128-
}
129-
130-
// transpose optional `class` settings
131-
element.attr('class', attrs.class);
132-
133-
},
134-
post: $mdTheming
135-
};
136-
},
137-
template: '<md-input-group ng-disabled="isDisabled" tabindex="-1">' +
138-
' <label for="{{fid}}" >{{label}}</label>' +
139-
' <md-textarea id="{{fid}}" ng-model="value" ' +
140-
' rows="{{rows}}" cols="{{cols}}"></md-textarea>' +
141-
'</md-input-group>'
142-
};
143-
}
144-
14582
/*
14683
* @private
14784
*
@@ -199,113 +136,67 @@ function mdInputDirective($mdUtil) {
199136
return {
200137
restrict: 'E',
201138
replace: true,
202-
template: '<input >',
139+
template: function createTemplate(element) {
140+
var isTextarea = element.parent().attr('type') === 'textarea';
141+
if (isTextarea) {
142+
return '<textarea >';
143+
} else {
144+
return '<input >';
145+
}
146+
},
203147
require: ['^?mdInputGroup', '?ngModel'],
204148
link: function mdInputDirectiveLink(scope, element, attr, ctrls) {
205-
linkBehaviours(scope, element, ctrls, $mdUtil);
206-
207149
element.attr('type', attr.type || element.parent().attr('type') || "text");
208-
}
209-
};
210-
}
211-
212-
/*
213-
* @private
214-
*
215-
* @ngdoc directive
216-
* @name mdTextarea
217-
* @module material.components.textField
218-
*
219-
* @restrict E
220-
*
221-
* @description
222-
* Use the `<md-textarea>` directive as elements within a `<md-input-group>` container
223-
*
224-
* @usage
225-
* <hljs lang="html">
226-
* <md-input-group ng-disabled="user.isLocked">
227-
* <label for="i1">Intro</label>
228-
* <md-textarea id="i1" ng-model="user.intro"></md-textarea>
229-
* </md-input-group>
230-
* </hljs>
231-
*/
232-
function mdTextareaDirective($mdUtil) {
233-
return {
234-
restrict: 'E',
235-
replace: true,
236-
template: '<textarea >',
237-
require: ['^?mdInputGroup', '?ngModel'],
238-
link: function mdInputDirectiveLink(scope, element, attr, ctrls) {
239-
linkBehaviours(scope, element, ctrls, $mdUtil);
150+
element.attr('rows', attr.rows || element.parent().attr('rows'));
151+
element.attr('cols', attr.cols || element.parent().attr('cols'));
240152

241-
element.attr('rows', attr.rows || element.parent().attr('rows') || "8");
242-
element.attr('cols', attr.cols || element.parent().attr('cols') || "100");
243-
}
244-
};
245-
}
246-
247-
/*
248-
* @private
249-
*
250-
* @ngdoc directive
251-
* @name mdTextarea
252-
* @module material.components.textField
253-
* @function
254-
*
255-
* @description
256-
* The link function used in the mdInput and mdTextarea
257-
*
258-
*/
259-
function linkBehaviours(scope, element, ctrls, $mdUtil) {
260-
var inputGroupCtrl = ctrls[0];
261-
var ngModelCtrl = ctrls[1];
262-
if (!inputGroupCtrl) {
263-
return;
264-
}
265-
266-
// scan for disabled and transpose the `type` value to the <input> element
267-
var isDisabled = $mdUtil.isParentDisabled(element);
268-
269-
element.attr('tabindex', isDisabled ? -1 : 0);
270-
element.attr('aria-disabled', isDisabled ? 'true' : 'false');
271-
272-
// When the input value changes, check if it "has" a value, and
273-
// set the appropriate class on the input group
274-
if (ngModelCtrl) {
275-
//Add a $formatter so we don't use up the render function
276-
ngModelCtrl.$formatters.push(function (value) {
277-
inputGroupCtrl.setHasValue(isNotEmpty(value));
278-
return value;
279-
});
280-
}
281-
282-
element.on('input', function () {
283-
inputGroupCtrl.setHasValue(isNotEmpty());
284-
});
285-
286-
// When the input focuses, add the focused class to the group
287-
element.on('focus', function (e) {
288-
inputGroupCtrl.setFocused(true);
289-
});
290-
// When the input blurs, remove the focused class from the group
291-
element.on('blur', function (e) {
292-
inputGroupCtrl.setFocused(false);
293-
inputGroupCtrl.setHasValue(isNotEmpty());
294-
});
295-
296-
scope.$on('$destroy', function () {
297-
inputGroupCtrl.setFocused(false);
298-
inputGroupCtrl.setHasValue(false);
299-
});
300-
301-
302-
function isNotEmpty(value) {
303-
value = angular.isUndefined(value) ? element.val() : value;
304-
return (angular.isDefined(value) && (value !== null) &&
305-
(value.toString().trim() != ""));
306-
}
307-
}
153+
var inputGroupCtrl = ctrls[0];
154+
var ngModelCtrl = ctrls[1];
155+
if (!inputGroupCtrl) {
156+
return;
157+
}
308158

159+
// scan for disabled and transpose the `type` value to the <input> element
160+
var isDisabled = $mdUtil.isParentDisabled(element);
309161

162+
element.attr('tabindex', isDisabled ? -1 : 0);
163+
element.attr('aria-disabled', isDisabled ? 'true' : 'false');
310164

165+
// When the input value changes, check if it "has" a value, and
166+
// set the appropriate class on the input group
167+
if (ngModelCtrl) {
168+
//Add a $formatter so we don't use up the render function
169+
ngModelCtrl.$formatters.push(function (value) {
170+
inputGroupCtrl.setHasValue(isNotEmpty(value));
171+
return value;
172+
});
173+
}
311174

175+
element.on('input', function () {
176+
inputGroupCtrl.setHasValue(isNotEmpty());
177+
});
178+
179+
// When the input focuses, add the focused class to the group
180+
element.on('focus', function (e) {
181+
inputGroupCtrl.setFocused(true);
182+
});
183+
// When the input blurs, remove the focused class from the group
184+
element.on('blur', function (e) {
185+
inputGroupCtrl.setFocused(false);
186+
inputGroupCtrl.setHasValue(isNotEmpty());
187+
});
188+
189+
scope.$on('$destroy', function () {
190+
inputGroupCtrl.setFocused(false);
191+
inputGroupCtrl.setHasValue(false);
192+
});
193+
194+
195+
function isNotEmpty(value) {
196+
value = angular.isUndefined(value) ? element.val() : value;
197+
return (angular.isDefined(value) && (value !== null) &&
198+
(value.toString().trim() != ""));
199+
}
200+
}
201+
};
202+
}

src/components/textField/textField.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ describe('Text Field directives', function () {
246246
});
247247

248248
it('should configure the amount of collumn and rows', function configureColsAndRows() {
249-
var markup = '<md-textarea-float rows="4" cols="50"' +
249+
var markup = '<md-text-float rows="4" cols="50"' +
250+
' type="textarea"' +
250251
' label="{{labels.intro}}" ' +
251252
' ng-model="user.filledIntro" >' +
252-
'</md-textarea-float>';
253+
'</md-text-float>';
253254
var el = buildElement(markup, model);
254255
var textarea = el.find('textarea');
255256

@@ -258,10 +259,11 @@ describe('Text Field directives', function () {
258259
});
259260

260261
it('should configure the label', function configureLabel() {
261-
var markup = '<md-textarea-float rows="4" cols="50"' +
262+
var markup = '<md-text-float rows="4" cols="50"' +
263+
' type="textarea"' +
262264
' label="{{labels.intro}}" ' +
263265
' ng-model="user.filledIntro" >' +
264-
'</md-textarea-float>';
266+
'</md-text-float>';
265267
var el = buildElement(markup, model);
266268
var label = el.find('label');
267269

0 commit comments

Comments
 (0)