Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 1447314

Browse files
committed
Merge pull request #17 from kentcdodds/master
Adds the ability to conditionally hide fields.
2 parents 5d4c225 + fab5804 commit 1447314

File tree

9 files changed

+74
-18
lines changed

9 files changed

+74
-18
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
Version numbers correspond to `bower.json` version
22

3+
# 0.0.10
4+
5+
## Features
6+
7+
Added `hide` property on fields. Allows devs to conditionally show fields. Also adding `hideExpression` property which is an expression that will be evaluated on the result object (using $parse) to make the api simpler to use.
8+
9+
## Bug Fixes
10+
11+
## Breaking Changes
12+
313
# 0.0.1
414

515
## Features

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Example data as it would be set in the controller
5757
label: 'Password',
5858
required: true,
5959
disabled: false, //default: false
60+
hideExpression: '!username' // hide when username is blank
6061
}
6162

6263
];
@@ -139,6 +140,20 @@ When constructing fields use the options below to customize each field object. Y
139140
###### Default
140141
>`undefined`
141142
143+
---
144+
##### hideExpression (expression string)
145+
>`hideExpression` is used to conditionally show the input. Evaluates on the `result` and uses the `hide` property on the field.
146+
147+
###### Default
148+
>`undefined`
149+
150+
---
151+
##### hide (boolean)
152+
>`hide` is used to conditionally show the input. When true, the input is hidden (meant to be used with a watch).
153+
154+
###### Default
155+
>`undefined`
156+
142157
---
143158
##### disabled (boolean)
144159
>`disabled` is used to add the disabled attribute to a form field.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-formly",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"authors": [
55
"Grant Helton <[email protected]>"
66
],

dist/formly.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ angular.module('formly.render').directive('formlyField', [
4949
transclude: true,
5050
scope: {
5151
optionsData: '&options',
52-
formId: '@formId',
53-
index: '@index',
52+
formId: '=formId',
53+
index: '=index',
5454
value: '=formValue'
5555
},
5656
link: function fieldLink($scope, $element, $attr) {
57-
var templateUrl = getTemplateUrl($scope.options.type);
57+
var templateUrl = $scope.options.templateUrl || getTemplateUrl($scope.options.type);
5858
if (templateUrl) {
5959
$http.get(templateUrl, { cache: $templateCache }).success(function (data) {
6060
//template data returned
@@ -69,7 +69,7 @@ angular.module('formly.render').directive('formlyField', [
6969
'$scope',
7070
function fieldController($scope) {
7171
$scope.options = $scope.optionsData();
72-
if ($scope.options.default) {
72+
if (typeof $scope.options.default !== 'undefined') {
7373
$scope.value = $scope.options.default;
7474
}
7575
// set field id to link labels and fields
@@ -86,18 +86,11 @@ angular.module('formly.render').directive('formlyForm', function formlyForm() {
8686
templateUrl: 'directives/formly-form.html',
8787
replace: true,
8888
scope: {
89-
formId: '@formId',
9089
fields: '=fields',
9190
options: '=options',
9291
result: '=result',
9392
formOnParentScope: '=name'
9493
},
95-
controller: [
96-
'$scope',
97-
'$element',
98-
function formController($scope, $element) {
99-
}
100-
],
10194
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
10295
return {
10396
post: function (scope, ele, attr, controller) {
@@ -106,7 +99,22 @@ angular.module('formly.render').directive('formlyForm', function formlyForm() {
10699
scope.formOnParentScope = scope[attr.name];
107100
}
108101
};
109-
}
102+
},
103+
controller: [
104+
'$scope',
105+
'$element',
106+
'$parse',
107+
function ($scope, $element, $parse) {
108+
$scope.$watch('result', function (newValue) {
109+
angular.forEach($scope.fields, function (field, index) {
110+
if (field.hideExpression) {
111+
var getter = $parse(field.hideExpression);
112+
field.hide = getter($scope.result);
113+
}
114+
});
115+
}, true);
116+
}
117+
]
110118
};
111119
});
112120
angular.module('formly.render').run([
@@ -123,6 +131,6 @@ angular.module('formly.render').run([
123131
$templateCache.put('directives/formly-field-text.html', '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><input class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>');
124132
$templateCache.put('directives/formly-field-textarea.html', '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><textarea type=text class=form-control id={{id}} rows={{options.lines}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value>\n' + '\t</textarea></div>');
125133
$templateCache.put('directives/formly-field.html', '');
126-
$templateCache.put('directives/formly-form.html', '<form class=formly role=form><formly-field ng-repeat="field in fields" options=field form-value=result[field.key||$index] class=formly-field form-id={{options.uniqueFormId}} index={{$index}}></formly-field><button type=submit ng-hide=options.hideSubmit>{{options.submitCopy || "Submit"}}</button></form>');
134+
$templateCache.put('directives/formly-form.html', '<form class=formly role=form><formly-field ng-repeat="field in fields" options=field form-value=result[field.key||$index] class=formly-field form-id=options.uniqueFormId index=$index ng-hide=field.hide></formly-field><button type=submit ng-hide=options.hideSubmit>{{options.submitCopy || "Submit"}}</button></form>');
127135
}
128136
]);

dist/formly.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/formly.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/formly-form.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
form-value="result[field.key||$index]"
55
class="formly-field"
66
form-id="options.uniqueFormId"
7-
index="$index">
7+
index="$index"
8+
ng-hide="field.hide">
89
</formly-field>
910
<button type="submit"
1011
ng-hide="options.hideSubmit">

0 commit comments

Comments
 (0)