1
- import { AuLabel } from ' @appuniversum/ember-appuniversum' ;
2
1
import {
3
2
formatDate ,
4
3
isIsoDateString ,
@@ -11,29 +10,42 @@ import { guidFor } from '@ember/object/internals';
11
10
import Component from ' @glimmer/component' ;
12
11
import { tracked } from ' @glimmer/tracking' ;
13
12
import { modifier } from ' ember-modifier' ;
13
+ import AuLabel from ' ./au-label' ;
14
+ import type { DuetDatePickerChangeEvent } from ' @duetds/date-picker/dist/types/components/duet-date-picker/duet-date-picker' ;
15
+ import type { DuetLocalizedText } from ' @duetds/date-picker/dist/types/components/duet-date-picker/date-localization' ;
16
+ import type { DuetDateAdapter } from ' @duetds/date-picker/dist/types/components/duet-date-picker/date-adapter' ;
14
17
15
- const props = modifier (
16
- function props (element , positional , properties ) {
17
- for (let propertyName in properties) {
18
- element[propertyName] = properties[propertyName];
19
- }
20
- },
21
- { eager: false },
22
- );
18
+ type IsoDate = string ;
19
+ type Adapter = DuetDateAdapter ;
20
+ type Localization = DuetLocalizedText ;
21
+ type DayOfWeek = 0 | 1 | 2 | 3 | 4 | 5 | 6 ; // Based on this enum: https://github.com/duetds/date-picker/blob/a89499198d6e5555073bb0dec3a3dab9a5b3648b/src/components/duet-date-picker/date-utils.ts#L3
23
22
24
- const DEFAULT_ADAPTER = {
25
- parse : function (value = ' ' , createDate ) {
26
- let dateRegex = / ^ (\d {1,2} )-(\d {1,2} )-(\d {4} )$ / ;
23
+ export interface AuDatePickerSignature {
24
+ Args: {
25
+ alignment? : ' top' ;
26
+ adapter? : Adapter ;
27
+ buttonLabel? : string ;
28
+ disabled? : boolean ;
29
+ error? : boolean ;
30
+ ' first-day' ? : DayOfWeek ;
31
+ firstDay? : DayOfWeek ;
32
+ id? : string ;
33
+ label? : string ;
34
+ localization? : Localization ;
35
+ max? : IsoDate | Date ;
36
+ min? : IsoDate | Date ;
37
+ value? : IsoDate | Date ;
38
+ warning? : boolean ;
39
+ onChange? : (isoDate : IsoDate | null , date : Date | null ) => void ;
40
+ };
41
+ Element: HTMLDuetDatePickerElement ;
42
+ }
27
43
28
- const matches = value .match (dateRegex);
29
- if (matches) {
30
- return createDate (matches[3 ], matches[2 ], matches[1 ]);
31
- }
32
- },
33
- format: formatDate,
34
- };
44
+ type DayNames = [string , string , string , string , string , string , string ];
45
+ // prettier-ignore
46
+ type MonthNames = [string , string , string , string , string , string , string , string , string , string , string , string ];
35
47
36
- const DEFAULT_LOCALIZATION = {
48
+ const DEFAULT_LOCALIZATION: Localization = {
37
49
dayNames: getLocalizedDays (),
38
50
monthNames: getLocalizedMonths (),
39
51
monthNamesShort: getLocalizedMonths (' short' ),
@@ -45,18 +57,40 @@ const DEFAULT_LOCALIZATION = {
45
57
monthSelectLabel: ' Maand' ,
46
58
yearSelectLabel: ' Jaar' ,
47
59
closeLabel: ' Sluit venster' ,
48
- keyboardInstruction: ' Gebruik de pijltjestoetsen om te navigeren' ,
49
60
calendarHeading: ' Kies een datum' ,
61
+ locale: ' nl-BE' ,
50
62
};
51
63
52
- export default class AuDatePicker extends Component {
53
- @asIsoDate value;
54
- @asIsoDate min;
55
- @asIsoDate max;
64
+ const DEFAULT_ADAPTER: Adapter = {
65
+ parse : function (
66
+ value = ' ' ,
67
+ createDate : (day : string , month : string , year : string ) => Date ,
68
+ ) {
69
+ const dateRegex = / ^ (\d {1,2} )-(\d {1,2} )-(\d {4} )$ / ;
70
+
71
+ const matches = value .match (dateRegex );
72
+ if (matches ) {
73
+ return createDate (
74
+ matches [3 ] as string ,
75
+ matches [2 ] as string ,
76
+ matches [1 ] as string ,
77
+ );
78
+ }
79
+ },
80
+ format: formatDate ,
81
+ };
82
+
83
+ export default class AuDatePicker extends Component <AuDatePickerSignature > {
84
+ // @ts-expect-error TODO: Something is wrong with the decorator types, but I'm not sure how to fix it.
85
+ @asIsoDate declare value: IsoDate ;
86
+ // @ts-expect-error TODO: Something is wrong with the decorator types, but I'm not sure how to fix it.
87
+ @asIsoDate declare min: IsoDate ;
88
+ // @ts-expect-error TODO: Something is wrong with the decorator types, but I'm not sure how to fix it.
89
+ @asIsoDate declare max: IsoDate ;
56
90
@tracked isInitialized = false ;
57
91
58
- constructor () {
59
- super (... arguments );
92
+ constructor (owner : unknown , args : AuDatePickerSignature [ ' Args ' ] ) {
93
+ super (owner , args );
60
94
this .registerDuetDatePicker ();
61
95
}
62
96
@@ -101,9 +135,13 @@ export default class AuDatePicker extends Component {
101
135
else return ' ' ;
102
136
}
103
137
138
+ get firstDayOfWeek() {
139
+ return this .args .firstDay || this .args [' first-day' ];
140
+ }
141
+
104
142
@action
105
- handleDuetDateChange (event ) {
106
- let wasDatePickerCleared = ! event .detail .value ;
143
+ handleDuetDateChange(event : CustomEvent < DuetDatePickerChangeEvent > ) {
144
+ const wasDatePickerCleared = ! event .detail .value ;
107
145
if (wasDatePickerCleared ) {
108
146
this .args .onChange ?.(null , null );
109
147
} else {
@@ -112,7 +150,7 @@ export default class AuDatePicker extends Component {
112
150
}
113
151
114
152
async registerDuetDatePicker() {
115
- if (typeof FastBoot === ' undefined' ) {
153
+ if (typeof globalThis . FastBoot === ' undefined' ) {
116
154
const { defineCustomElements : registerDuetDatePicker } = await import (
117
155
' @duetds/date-picker/custom-element'
118
156
);
@@ -141,8 +179,9 @@ export default class AuDatePicker extends Component {
141
179
value ={{this .value }}
142
180
min ={{this .min }}
143
181
max ={{this .max }}
144
- first-day-of-week ={{@ first-day }}
182
+ first-day-of-week ={{this .firstDayOfWeek }}
145
183
data-test-au-date-picker-component
184
+ {{! @glint-expect-error duetChange is a custom event but the types expect Event instead}}
146
185
{{on " duetChange" this . handleDuetDateChange}}
147
186
{{props localization =this . localization dateAdapter =this . adapter}}
148
187
...attributes
@@ -152,7 +191,7 @@ export default class AuDatePicker extends Component {
152
191
</template >
153
192
}
154
193
155
- function validateAdapter (adapterArg ) {
194
+ function validateAdapter(adapterArg ? : Adapter ) {
156
195
assert (
157
196
` The @adapter argument needs to be an object but it is a "${typeof adapterArg }" ` ,
158
197
Boolean (adapterArg ) && typeof adapterArg === ' object' ,
@@ -166,7 +205,7 @@ function validateAdapter(adapterArg) {
166
205
});
167
206
}
168
207
169
- function validateLocalization (localizationArg ) {
208
+ function validateLocalization(localizationArg ? : Localization ) {
170
209
assert (
171
210
` The @localization argument needs to be an object but it is a "${typeof localizationArg }" ` ,
172
211
Boolean (localizationArg ) && typeof localizationArg === ' object' ,
@@ -180,10 +219,10 @@ function validateLocalization(localizationArg) {
180
219
});
181
220
}
182
221
183
- function asIsoDate (target , key /* , descriptor */ ) {
222
+ function asIsoDate(target : unknown , key : string /* , descriptor */ ) {
184
223
return {
185
- get () {
186
- let argValue = this .args [key];
224
+ get(this : AuDatePicker ) : string | undefined {
225
+ const argValue = ( this .args as { [ key : string ] : unknown }) [key ];
187
226
188
227
if (! argValue ) {
189
228
return ;
@@ -207,23 +246,34 @@ function asIsoDate(target, key /*, descriptor */) {
207
246
};
208
247
}
209
248
210
- function getLocalizedMonths (monthFormat = ' long' ) {
211
- let someYear = 2021 ;
249
+ function getLocalizedMonths(monthFormat = ' long' ): MonthNames {
250
+ const someYear = 2021 ;
212
251
return [... Array (12 ).keys ()].map ((monthIndex ) => {
213
- let date = new Date (someYear, monthIndex);
252
+ const date = new Date (someYear , monthIndex );
214
253
return intl ({ month: monthFormat }).format (date );
215
- });
254
+ }) as MonthNames ;
216
255
}
217
256
218
- function getLocalizedDays (weekdayFormat = ' long' ) {
219
- let someSunday = new Date (' 2021-01-03' );
257
+ function getLocalizedDays(weekdayFormat = ' long' ): DayNames {
258
+ const someSunday = new Date (' 2021-01-03' );
220
259
return [... Array (7 ).keys ()].map ((index ) => {
221
- let weekday = new Date (someSunday .getTime ());
260
+ const weekday = new Date (someSunday .getTime ());
222
261
weekday .setDate (someSunday .getDate () + index );
223
262
return intl ({ weekday: weekdayFormat }).format (weekday );
224
- });
263
+ }) as DayNames ;
225
264
}
226
265
227
- function intl (options ) {
266
+ function intl(options : object ) {
228
267
return new Intl .DateTimeFormat (' nl-BE' , options );
229
268
}
269
+
270
+ const props = modifier (function props(
271
+ element : HTMLElement ,
272
+ positional ,
273
+ properties : { [key : string ]: unknown },
274
+ ) {
275
+ for (const propertyName in properties ) {
276
+ (element as unknown as { [key : string ]: unknown })[propertyName ] =
277
+ properties [propertyName ];
278
+ }
279
+ });
0 commit comments