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

Commit 921690d

Browse files
committed
Merge pull request #29 from kentcdodds/master
Closes #27 and #28
2 parents eab1f1a + 8e9fbc1 commit 921690d

File tree

10 files changed

+109
-15
lines changed

10 files changed

+109
-15
lines changed

CHANGELOG.md

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

3+
# 0.0.12
4+
5+
## Features
6+
7+
Adding the `formlyOptionsProvider` which allows developers to set default form options.
8+
9+
Adding `submitButtonTemplate` to the configurable `formlyOptions`. If it has a value, then the button in the `formly-form` directive will be replaced with the compiled (on the scope) version of that value. Also adding twitter classes to the submit button so hopefully this template will be unecessary in some cases.
10+
11+
## Bug Fixes
12+
13+
## Breaking Changes
14+
315
# 0.0.11
416

517
## Features

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Example data as it would be set in the controller
8181
};
8282
```
8383
### Creating Forms
84-
Forms can be customized with the options below.
84+
Forms can be customized with the options below. Note, you can configure this on a form-by-form basis as shown in the example, or globally using the [`formlyOptionsProvider`](#global-config).
8585

8686
#### uniqueFormId (string, required)
8787
>`uniqueFormId` is used to identify the form.
@@ -92,6 +92,9 @@ Forms can be customized with the options below.
9292
#### submitCopy (string, optional)
9393
>`submitCopy` customizes the submit button copy. Defaults to 'Submit'.
9494
95+
#### submitButtonTemplate (string, optional)
96+
>`submitButtonTemplate` customizes the template used for the submit button. Compiled on the scope, so you have access to all other options (and any custom options) in your custom template.
97+
9598
### Creating Form Fields
9699
When constructing fields use the options below to customize each field object. You must set at least a `type`, `template`, or `templateUrl`.
97100

@@ -400,9 +403,11 @@ _Example password field_
400403

401404
### Global Config
402405

406+
#### formlyTemplateProvider
407+
403408
You can configure formly to use custom templates for specified types (your own "text" template) by injecting the `formlyTemplateProvider` in your app's `config` function. The `formlyTemplateProvider` has the following functions:
404409

405-
#### setTemplateUrl
410+
##### setTemplateUrl
406411

407412
Allows you to set a template
408413

@@ -418,7 +423,7 @@ formlyTemplateProvider.setTemplate({
418423
});
419424
```
420425

421-
#### getTemplateUrl
426+
##### getTemplateUrl
422427

423428
Allows you to get the template
424429

@@ -427,6 +432,30 @@ formlyTemplateProvider.setTemplateUrl('radio', 'views/custom-formly-radio.html')
427432
formlyTemplateProvider.getTemplateUrl('radio') === 'views/custom-formly-radio.html'; // true
428433
```
429434

435+
#### formlyOptionsProvider
436+
437+
You can configure default options for all forms using the `formlyOptionsProvider` in your app's `config` function. This has the following api:
438+
439+
##### setOption
440+
441+
Allows you to set an option
442+
443+
```javascript
444+
formlyOptionsProvider.setOption('uniqueFormId', 'probablyDontWantToDoThis');
445+
formlyOptionsProvider.setOption('submitCopy', 'Save');
446+
447+
// the same can be accomplished with
448+
449+
formlyOptionsProvider.setOption({
450+
submitCopy: 'Save',
451+
uniqueFormId: 'probablyDontWantToDoThis'
452+
});
453+
```
454+
455+
##### getOptions
456+
457+
Returns a copy of the current options. This is used internally.
458+
430459
## Roadmap
431460

432461
## Release Notes

src/app.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var app = angular.module('app', ['ng',
88

99
app.constant('usingCustomTypeTemplates', window.localStorage.getItem('useCustomTypeTemplates') === 'true');
1010

11-
app.config(function($stateProvider, $urlRouterProvider, $locationProvider, formlyTemplateProvider, usingCustomTypeTemplates) {
11+
app.config(function($stateProvider, $urlRouterProvider, $locationProvider, formlyTemplateProvider, formlyOptionsProvider, usingCustomTypeTemplates) {
1212
$locationProvider.html5Mode(false);
1313
$locationProvider.hashPrefix('!');
1414

@@ -28,6 +28,18 @@ app.config(function($stateProvider, $urlRouterProvider, $locationProvider, forml
2828
checkbox: 'views/custom-field-checkbox.html'
2929
});
3030
}
31+
32+
formlyOptionsProvider.setOption('uniqueFormId', 'whyConfigureUniqueId');
33+
// or
34+
formlyOptionsProvider.setOption({
35+
submitCopy: 'Configured Submit',
36+
hideSubmit: true,
37+
submitButtonTemplate: [
38+
'<button type="submit" class="btn btn-primary" ng-hide="options.hideSubmit">',
39+
'{{options.submitCopy || "Submit"}} boo yeah!',
40+
'</button>'
41+
].join('')
42+
});
3143
});
3244

3345
app.run(function($rootScope, $state, $stateParams, $window) {

src/directives/formly-form.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
ng-hide="field.hide">
99
</formly-field>
1010
<button type="submit"
11+
class="btn btn-primary"
1112
ng-hide="options.hideSubmit">
1213
{{options.submitCopy || "Submit"}}
1314
</button>

src/directives/formly-form.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
'use strict';
22
angular.module('formly.render')
3-
.directive('formlyForm', function formlyForm() {
3+
.directive('formlyForm', function formlyForm(formlyOptions, $compile) {
44
return {
55
restrict: 'AE',
66
templateUrl: 'directives/formly-form.html',
77
replace: true,
88
scope: {
9-
fields: '=fields',
10-
options: '=options',
11-
result: '=result',
9+
fields: '=',
10+
options: '=?',
11+
result: '=',
1212
formOnParentScope: '=name'
1313
},
1414
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
1515
return {
1616
post: function (scope, ele, attr, controller) {
17+
scope.options = angular.extend(formlyOptions.getOptions(), scope.options);
18+
if (scope.options.submitButtonTemplate) {
19+
ele.find('button').replaceWith($compile(scope.options.submitButtonTemplate)(scope));
20+
}
1721
//Post gets called after angular has created the FormController
1822
//Now pass the FormController back up to the parent scope
1923
scope.formOnParentScope = scope[attr.name];

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
<script src="directives/formly-form.js"></script>
4242
<script src="directives/formly-field.js"></script>
4343
<script src="providers/formly-template.js"></script>
44+
<script src="providers/formly-options.js"></script>
4445
</body>
4546
</html>

src/providers/formly-options.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
angular.module('formly.render')
3+
.provider('formlyOptions', function() {
4+
5+
var options = {
6+
uniqueFormId: null,
7+
submitCopy: "Submit",
8+
hideSubmit: false,
9+
submitButtonTemplate: null
10+
};
11+
12+
function setOption(name, value) {
13+
if (typeof name === 'string') {
14+
options[name] = value;
15+
} else {
16+
angular.forEach(name, function(val, name) {
17+
setOption(name, val);
18+
});
19+
}
20+
}
21+
22+
function getOptions() {
23+
// copy to avoid third-parties manipulating the options outside of the api.
24+
return angular.copy(options);
25+
}
26+
27+
this.setOption = setOption;
28+
this.getOptions = getOptions;
29+
this.$get = function formlyOptions() {
30+
return this;
31+
}
32+
33+
});

src/providers/formly-template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ angular.module('formly.render')
1919
templateMap[name] = templateUrl;
2020
} else {
2121
angular.forEach(name, function(templateUrl, name) {
22-
setTemplateUrl(name, templateUrl)
22+
setTemplateUrl(name, templateUrl);
2323
});
2424
}
2525
}

src/views/home.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ <h3>Form State</h3>
106106
</tbody>
107107

108108
</table>
109+
110+
<h3>Configured Default Options</h3>
111+
<div hljs source="preConfiguredOptions|json"></div>
109112
</div>
110113
</div>
111114
</div>

src/views/home.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
app.controller('home', function($scope, $parse, $rootScope, $window, usingCustomTypeTemplates) {
2+
app.controller('home', function($scope, $parse, formlyOptions, $window, usingCustomTypeTemplates) {
33
// Public Methods
44
$scope.onSubmit = function onSubmit() {
55
$scope.submittedData = $scope.formData;
@@ -29,8 +29,6 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
2929
$scope.formFields = $parse(newValue)({});
3030
$scope.formFieldsError = false;
3131
} catch (e) {
32-
// eat $parse error
33-
// console.log('Formly Demo App Error: error parsing data, changes not applied');
3432
$scope.formFieldsError = true;
3533
}
3634
});
@@ -39,8 +37,6 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
3937
$scope.formOptions = $parse(newValue)({});
4038
$scope.formOptionsError = false;
4139
} catch (e) {
42-
// eat $parse error
43-
// console.log('Formly Demo App Error: error parsing data, changes not applied');
4440
$scope.formOptionsError = true;
4541
}
4642
});
@@ -52,6 +48,8 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
5248
$scope.typeTemplatesButton = 'Use Custom Type Templates';
5349
}
5450

51+
$scope.preConfiguredOptions = formlyOptions.getOptions();
52+
5553
$scope.formFields = [{
5654
key: 'firstName',
5755
type: 'text',
@@ -179,7 +177,8 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
179177

180178
$scope.formOptions = {
181179
uniqueFormId: 'formly',
182-
submitCopy: 'Save'
180+
submitCopy: 'Save',
181+
hideSubmit: false
183182
};
184183
$scope.submittedData = null;
185184
$scope.formData = {};

0 commit comments

Comments
 (0)