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

Commit 1fb9b6f

Browse files
author
Nick Litwin
committed
Add form field for file input plus button and label
1 parent c5b402f commit 1fb9b6f

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('tcUIComponents').directive('fileFormField', fileFormField);
5+
6+
function fileFormField() {
7+
return {
8+
restrict: 'E',
9+
templateUrl: 'directives/file-form-field/file-form-field.html',
10+
scope: {
11+
labelText: '@',
12+
fieldId: '@',
13+
placeholder: '@',
14+
fileType: '@',
15+
mandatory: '=',
16+
buttonText: '@',
17+
setFileReference: '&'
18+
},
19+
link: function(scope, element, attrs) {
20+
scope.selectFile = selectFile;
21+
22+
// fieldId is not set on element at this point, so grabbing with class .none
23+
// which exists on the element right away
24+
var fileInput = $(element[0]).find('.none');
25+
var fileNameInput = $(element[0]).find('input[type=text]');
26+
27+
fileInput.bind('change', function() {
28+
var file = fileInput[0].files[0];
29+
30+
// Pass file object up through callback into controller
31+
scope.setFileReference({file: file, fieldId: scope.fieldId});
32+
33+
// Set the file name as the value of the disabled input
34+
fileNameInput[0].value = file.name;
35+
});
36+
37+
function selectFile() {
38+
fileInput.click();
39+
}
40+
}
41+
}
42+
}
43+
})();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.labelwrapper
2+
label.tc-label {{labelText}}
3+
span.lowercase(ng-if="fileType") {{fileType | addBeginningSpace}}
4+
5+
span.tc-label__mandatory(ng-if="mandatory") #[span *]mandatory
6+
7+
.changenamelater
8+
input(type="text", placeholder="{{placeholder}}", disabled)
9+
10+
button.tc-btn(ng-click="selectFile()") {{buttonText}}
11+
12+
input.none(type="file", id="{{fieldId}}")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* jshint -W117, -W030 */
2+
describe('File Form Field Directive', function() {
3+
var scope;
4+
5+
// USE AS TEMPLATE FOR DIRECTIVES
6+
7+
beforeEach(function() {
8+
bard.appModule('tcUIComponents');
9+
bard.inject(this, '$compile', '$rootScope');
10+
});
11+
12+
bard.verifyNoOutstandingHttpRequests();
13+
14+
xdescribe('', function() {
15+
beforeEach(function() {});
16+
17+
it('', function() {});
18+
});
19+
});

0 commit comments

Comments
 (0)