1
- const classNames = {
2
- wrapper : 'NativeDatepicker' ,
3
- input : 'NativeDatepicker__input' ,
4
- } ;
1
+ /* global define */
2
+ ( function nativeDatepickerFactory1 ( factory ) {
3
+ if ( typeof exports === 'object' && typeof module === 'object' ) {
4
+ module . exports = factory ( ) ;
5
+ } else if ( typeof define === 'function' && define . amd ) {
6
+ define ( factory ) ;
7
+ } else {
8
+ window [ 'NativeDatepicker' ] = factory ( ) ;
9
+ }
10
+ } ) ( function nativeDatepickerFactory2 ( ) {
11
+ var classNames = {
12
+ wrapper : 'NativeDatepicker' ,
13
+ input : 'NativeDatepicker__input' ,
14
+ } ;
5
15
6
- const dateRegex = / \d { 4 } - \d { 2 } - \d { 2 } / ;
16
+ var dateRegex = / \d { 4 } - \d { 2 } - \d { 2 } / ;
7
17
8
- class NativeDatepicker {
9
- constructor ( options ) {
18
+ function NativeDatepicker ( options ) {
10
19
this . options = Object . assign (
11
20
{
12
21
win : typeof window !== 'undefined' ? window : undefined ,
13
22
existingElement : null ,
14
- onChange : ( ) => { } ,
23
+ onChange : function defaultOnChange ( ) { } ,
24
+ initialValue : '' ,
15
25
} ,
16
26
options
17
27
) ;
18
28
19
29
this . addStylesheet ( ) ;
20
30
this . buildDom ( ) ;
31
+ this . setValue ( this . options . initialValue ) ;
21
32
}
22
33
23
- setValue ( fullString ) {
24
- const match = fullString . match ( dateRegex ) ;
34
+ NativeDatepicker . prototype . setValue = function setValue ( fullString ) {
35
+ var match = fullString . match ( dateRegex ) ;
25
36
if ( match ) {
26
37
this . fullValue = fullString ;
27
38
this . dateValue = match [ 0 ] ;
28
39
this . dateInputElement . value = match [ 0 ] ;
29
40
}
30
- }
41
+ } ;
31
42
32
- buildDom ( ) {
43
+ NativeDatepicker . prototype . buildDom = function buildDom ( ) {
33
44
// DOM structure:
34
- // <span class="datepicker-toggle">
35
- // <!-- optional DOM nodes from user -->
36
- // <input type="date" class="datepicker-input">
45
+ // <span class="NativeDatepicker">
46
+ // <input type="date" class="NativeDatepicker__input">
37
47
// </span>
38
48
39
- const element =
49
+ var element =
40
50
this . options . existingElement ||
41
51
this . options . win . document . createElement ( 'span' ) ;
42
52
element . classList . add ( classNames . wrapper ) ;
@@ -48,63 +58,76 @@ class NativeDatepicker {
48
58
element . style . display = 'none' ;
49
59
}
50
60
51
- const dateInputElement = this . options . win . document . createElement ( 'input' ) ;
61
+ var dateInputElement = this . options . win . document . createElement ( 'input' ) ;
52
62
dateInputElement . type = 'date' ;
53
63
dateInputElement . classList . add ( classNames . input ) ;
54
64
element . appendChild ( dateInputElement ) ;
55
65
this . dateInputElement = dateInputElement ;
56
66
57
- dateInputElement . addEventListener ( 'change' , ( ) => {
58
- let newValue = this . fullValue . replace ( dateRegex , dateInputElement . value ) ;
59
- // Regex didn't match, fallback to setting the entire value
60
- if ( ! newValue . includes ( dateInputElement . value ) ) {
61
- newValue = dateInputElement . value ;
67
+ var self = this ;
68
+ dateInputElement . addEventListener (
69
+ 'change' ,
70
+ function onNativeDatepickerChange ( ) {
71
+ var newValue = self . fullValue . replace (
72
+ dateRegex ,
73
+ dateInputElement . value
74
+ ) ;
75
+ // Regex didn't match, fallback to setting the entire value
76
+ if ( ! newValue . includes ( dateInputElement . value ) ) {
77
+ newValue = dateInputElement . value ;
78
+ }
79
+ dateInputElement . value = self . dateValue ;
80
+ self . options . onChange ( newValue ) ;
62
81
}
63
- dateInputElement . value = this . dateValue ;
64
- this . options . onChange ( newValue ) ;
65
- } ) ;
66
- }
82
+ ) ;
83
+ } ;
67
84
68
- addStylesheet ( ) {
69
- if ( ! this . options . win . document . querySelector ( 'style#nativeDatepicker' ) ) {
70
- const style = this . options . win . document . createElement ( 'style' ) ;
71
- style . id = 'nativeDatepicker' ;
72
- style . textContent = `
73
- .${ classNames . wrapper } {
74
- display: inline-block;
75
- position: relative;
76
- }
77
- .${ classNames . input } {
78
- position: absolute;
79
- left: 0;
80
- top: 0;
81
- width: 100%;
82
- height: 100%;
83
- opacity: 0;
84
- cursor: pointer;
85
- box-sizing: border-box;
86
- }
87
- .${ classNames . input } ::-webkit-calendar-picker-indicator {
88
- position: absolute;
89
- left: 0;
90
- top: 0;
91
- width: 100%;
92
- height: 100%;
93
- margin: 0;
94
- padding: 0;
95
- cursor: pointer;
96
- }
97
- ` ;
85
+ NativeDatepicker . prototype . addStylesheet = function addStylesheet ( ) {
86
+ var styleId = 'NativeDatepickerStyles' ;
87
+ if ( ! this . options . win . document . querySelector ( 'style#' + styleId ) ) {
88
+ var style = this . options . win . document . createElement ( 'style' ) ;
89
+ style . id = styleId ;
90
+ style . textContent =
91
+ '.' +
92
+ classNames . wrapper +
93
+ ' {' +
94
+ ' display: inline-block;' +
95
+ ' position: relative;' +
96
+ '}' +
97
+ '.' +
98
+ classNames . input +
99
+ ' {' +
100
+ ' position: absolute;' +
101
+ ' left: 0;' +
102
+ ' top: 0;' +
103
+ ' width: 100%;' +
104
+ ' height: 100%;' +
105
+ ' opacity: 0;' +
106
+ ' cursor: pointer;' +
107
+ ' box-sizing: border-box;' +
108
+ '}' +
109
+ '.' +
110
+ classNames . input +
111
+ '::-webkit-calendar-picker-indicator {' +
112
+ ' position: absolute;' +
113
+ ' left: 0;' +
114
+ ' top: 0;' +
115
+ ' width: 100%;' +
116
+ ' height: 100%;' +
117
+ ' margin: 0;' +
118
+ ' padding: 0;' +
119
+ ' cursor: pointer;' +
120
+ '}' ;
98
121
this . options . win . document . head . appendChild ( style ) ;
99
122
}
100
- }
123
+ } ;
101
124
102
- isSupported ( ) {
103
- const input = this . options . win . document . createElement ( 'input' ) ;
125
+ NativeDatepicker . prototype . isSupported = function isSupported ( ) {
126
+ var input = this . options . win . document . createElement ( 'input' ) ;
104
127
input . type = 'date' ;
105
128
input . value = 'invalid date value' ;
106
129
return input . value !== 'invalid date value' ;
107
- }
108
- }
130
+ } ;
109
131
110
- module . exports = NativeDatepicker ;
132
+ return NativeDatepicker ;
133
+ } ) ;
0 commit comments