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

Commit dbfc0a7

Browse files
author
Kamil Kisiela
committed
refactor(datepicker): move minDate, maxDate and filterDate to ngModelAttrs and bound them
1 parent f39d17e commit dbfc0a7

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

src/types/datepicker/datepicker.js

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ export default (formlyConfigProvider) => {
77
wrapper: ['messages'],
88
defaultOptions: {
99
ngModelAttrs: {
10-
placeholder: {attribute: 'md-placeholder'}
10+
placeholder: {
11+
attribute: 'md-placeholder'
12+
},
13+
minDate: {
14+
bound: 'md-min-date'
15+
},
16+
maxDate: {
17+
bound: 'md-max-date'
18+
},
19+
filterDate: {
20+
bound: 'md-date-filter'
21+
}
1122
}
1223
},
1324
apiCheck: (check) => ({
@@ -19,37 +30,4 @@ export default (formlyConfigProvider) => {
1930
}
2031
})
2132
});
22-
23-
formlyConfigProvider.templateManipulators.preWrapper.push((template, options) => {
24-
if (angular.isDefined(options.templateOptions.minDate)
25-
|| angular.isDefined(options.templateOptions.maxDate)
26-
|| angular.isDefined(options.templateOptions.filterDate)) {
27-
28-
const dateConfig = {
29-
min: options.templateOptions.minDate || undefined,
30-
max: options.templateOptions.maxDate || undefined,
31-
filter: options.templateOptions.filterDate || undefined
32-
};
33-
const node = document.createElement('div');
34-
35-
node.innerHTML = template;
36-
const datepickerNode = node.querySelector('md-datepicker');
37-
38-
if (datepickerNode) {
39-
if (dateConfig.min) {
40-
datepickerNode.setAttribute('md-min-date', 'to.minDate');
41-
}
42-
if (dateConfig.max) {
43-
datepickerNode.setAttribute('md-max-date', 'to.maxDate');
44-
}
45-
if (dateConfig.filter) {
46-
datepickerNode.setAttribute('md-date-filter', 'to.filterDate');
47-
}
48-
}
49-
return node.innerHTML;
50-
}
51-
52-
53-
return template;
54-
});
5533
}

tests/types/datepicker-spec.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ describe("formlyMaterial - datepicker type", () => {
1212
let form;
1313
let element;
1414
let field;
15+
const minDate = new Date(2015, 10, 19);
16+
const maxDate = new Date(2015, 11, 20);
17+
const filterDate = () => true;
18+
1519
//
1620
// helpers
1721
//
@@ -24,11 +28,9 @@ describe("formlyMaterial - datepicker type", () => {
2428
templateOptions: {
2529
label: 'test field',
2630
placeholder: "Pick a date",
27-
minDate: new Date(2015, 11, 19),
28-
maxDate: new Date(2015, 12, 20),
29-
filterDate: (date) => {
30-
return true;
31-
}
31+
minDate: minDate,
32+
maxDate: maxDate,
33+
filterDate: filterDate
3234
}
3335
}, options)];
3436

@@ -63,15 +65,24 @@ describe("formlyMaterial - datepicker type", () => {
6365
});
6466

6567
it('should have min date', () => {
66-
expect(element.attr('md-min-date')).toBe('to.minDate');
68+
const scope = angular.element(element).scope();
69+
70+
expect(element.attr('md-min-date')).toBe("options.templateOptions['minDate']");
71+
expect(scope.options.templateOptions.minDate.toDateString()).toBe(minDate.toDateString());
6772
});
6873

6974
it('should have max date', () => {
70-
expect(element.attr('md-max-date')).toBe('to.maxDate');
75+
const scope = angular.element(element).scope();
76+
77+
expect(element.attr('md-max-date')).toBe("options.templateOptions['maxDate']");
78+
expect(scope.options.templateOptions.maxDate.toDateString()).toBe(maxDate.toDateString());
7179
});
7280

7381
it('should have date filter', () => {
74-
expect(element.attr('md-date-filter')).toBe('to.filterDate');
82+
const scope = angular.element(element).scope();
83+
84+
expect(element.attr('md-date-filter')).toBe("options.templateOptions['filterDate']");
85+
expect(scope.options.templateOptions.filterDate).toBe(filterDate);
7586
});
7687

7788
});

0 commit comments

Comments
 (0)