mtrl 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/demo/build.ts +349 -0
  2. package/demo/index.html +110 -0
  3. package/demo/main.js +448 -0
  4. package/demo/styles.css +239 -0
  5. package/index.ts +18 -0
  6. package/package.json +14 -3
  7. package/server.ts +86 -0
  8. package/src/components/badge/api.ts +70 -63
  9. package/src/components/badge/badge.ts +16 -2
  10. package/src/components/badge/config.ts +66 -13
  11. package/src/components/badge/features.ts +51 -42
  12. package/src/components/badge/index.ts +27 -2
  13. package/src/components/badge/types.ts +62 -30
  14. package/src/components/bottom-app-bar/bottom-app-bar.ts +154 -0
  15. package/src/components/bottom-app-bar/config.ts +29 -0
  16. package/src/components/bottom-app-bar/index.ts +17 -0
  17. package/src/components/bottom-app-bar/types.ts +114 -0
  18. package/src/components/button/api.ts +5 -0
  19. package/src/components/button/button.ts +0 -1
  20. package/src/components/button/config.ts +6 -2
  21. package/src/components/button/index.ts +10 -2
  22. package/src/components/button/types.ts +20 -2
  23. package/src/components/card/card.ts +13 -25
  24. package/src/components/card/config.ts +83 -30
  25. package/src/components/card/content.ts +8 -10
  26. package/src/components/card/features.ts +4 -3
  27. package/src/components/card/index.ts +29 -2
  28. package/src/components/card/types.ts +33 -22
  29. package/src/components/checkbox/config.ts +3 -4
  30. package/src/components/checkbox/index.ts +1 -2
  31. package/src/components/checkbox/types.ts +12 -3
  32. package/src/components/chip/api.ts +170 -221
  33. package/src/components/chip/chip.ts +34 -302
  34. package/src/components/chip/config.ts +1 -2
  35. package/src/components/chip/index.ts +10 -2
  36. package/src/components/chip/types.ts +224 -35
  37. package/src/components/datepicker/api.ts +265 -0
  38. package/src/components/datepicker/config.ts +141 -0
  39. package/src/components/datepicker/datepicker.ts +341 -0
  40. package/src/components/datepicker/index.ts +12 -0
  41. package/src/components/datepicker/render.ts +450 -0
  42. package/src/components/datepicker/types.ts +397 -0
  43. package/src/components/datepicker/utils.ts +289 -0
  44. package/src/components/dialog/api.ts +55 -21
  45. package/src/components/dialog/config.ts +12 -9
  46. package/src/components/dialog/dialog.ts +6 -3
  47. package/src/components/dialog/features.ts +345 -151
  48. package/src/components/dialog/index.ts +38 -8
  49. package/src/components/dialog/types.ts +40 -14
  50. package/src/components/divider/config.ts +81 -0
  51. package/src/components/divider/divider.ts +37 -0
  52. package/src/components/divider/features.ts +207 -0
  53. package/src/components/divider/index.ts +9 -0
  54. package/src/components/divider/types.ts +55 -0
  55. package/src/components/extended-fab/api.ts +141 -0
  56. package/src/components/extended-fab/config.ts +112 -0
  57. package/src/components/extended-fab/extended-fab.ts +125 -0
  58. package/src/components/extended-fab/index.ts +9 -0
  59. package/src/components/extended-fab/types.ts +304 -0
  60. package/src/components/fab/api.ts +97 -0
  61. package/src/components/fab/config.ts +93 -0
  62. package/src/components/fab/fab.ts +67 -0
  63. package/src/components/fab/index.ts +9 -0
  64. package/src/components/fab/types.ts +251 -0
  65. package/src/components/list/config.ts +4 -5
  66. package/src/components/list/features.ts +6 -7
  67. package/src/components/list/index.ts +7 -9
  68. package/src/components/list/list-item.ts +12 -13
  69. package/src/components/list/types.ts +50 -5
  70. package/src/components/list/utils.ts +30 -3
  71. package/src/components/menu/features/items-manager.ts +9 -9
  72. package/src/components/menu/features/positioning.ts +7 -7
  73. package/src/components/menu/features/visibility.ts +7 -7
  74. package/src/components/menu/index.ts +7 -9
  75. package/src/components/menu/menu-item.ts +6 -6
  76. package/src/components/menu/menu.ts +22 -0
  77. package/src/components/menu/types.ts +29 -10
  78. package/src/components/menu/utils.ts +67 -0
  79. package/src/components/navigation/api.ts +78 -50
  80. package/src/components/navigation/config.ts +22 -10
  81. package/src/components/navigation/features/items.ts +284 -0
  82. package/src/components/navigation/index.ts +0 -6
  83. package/src/components/navigation/nav-item.ts +70 -33
  84. package/src/components/navigation/navigation.ts +53 -3
  85. package/src/components/navigation/types.ts +117 -70
  86. package/src/components/progress/api.ts +2 -3
  87. package/src/components/progress/config.ts +2 -3
  88. package/src/components/progress/index.ts +0 -1
  89. package/src/components/progress/progress.ts +1 -2
  90. package/src/components/progress/types.ts +186 -33
  91. package/src/components/radios/config.ts +1 -1
  92. package/src/components/radios/index.ts +0 -1
  93. package/src/components/radios/types.ts +0 -7
  94. package/src/components/search/api.ts +203 -0
  95. package/src/components/search/config.ts +86 -0
  96. package/src/components/search/features/index.ts +4 -0
  97. package/src/components/search/features/search.ts +717 -0
  98. package/src/components/search/features/states.ts +169 -0
  99. package/src/components/search/features/structure.ts +197 -0
  100. package/src/components/search/index.ts +7 -0
  101. package/src/components/search/search.ts +52 -0
  102. package/src/components/search/types.ts +175 -0
  103. package/src/components/segmented-button/config.ts +80 -0
  104. package/src/components/segmented-button/index.ts +4 -0
  105. package/src/components/segmented-button/segment.ts +154 -0
  106. package/src/components/segmented-button/segmented-button.ts +249 -0
  107. package/src/components/segmented-button/types.ts +254 -0
  108. package/src/components/slider/accessibility.md +5 -5
  109. package/src/components/slider/api.ts +41 -120
  110. package/src/components/slider/config.ts +51 -47
  111. package/src/components/slider/features/handlers.ts +495 -0
  112. package/src/components/slider/features/index.ts +1 -2
  113. package/src/components/slider/features/slider.ts +66 -84
  114. package/src/components/slider/features/states.ts +195 -0
  115. package/src/components/slider/features/structure.ts +136 -206
  116. package/src/components/slider/features/ui.ts +145 -206
  117. package/src/components/slider/index.ts +2 -11
  118. package/src/components/slider/slider.ts +9 -12
  119. package/src/components/slider/types.ts +67 -26
  120. package/src/components/snackbar/config.ts +2 -3
  121. package/src/components/snackbar/constants.ts +0 -32
  122. package/src/components/snackbar/index.ts +0 -1
  123. package/src/components/snackbar/position.ts +9 -1
  124. package/src/components/snackbar/types.ts +122 -46
  125. package/src/components/switch/config.ts +2 -3
  126. package/src/components/switch/index.ts +0 -1
  127. package/src/components/switch/types.ts +3 -2
  128. package/src/components/tabs/config.ts +3 -4
  129. package/src/components/tabs/features.ts +4 -2
  130. package/src/components/tabs/index.ts +0 -15
  131. package/src/components/tabs/indicator.ts +73 -13
  132. package/src/components/tabs/tab-api.ts +12 -4
  133. package/src/components/tabs/tab.ts +18 -6
  134. package/src/components/tabs/types.ts +23 -5
  135. package/src/components/textfield/config.ts +2 -3
  136. package/src/components/textfield/index.ts +0 -1
  137. package/src/components/textfield/types.ts +17 -3
  138. package/src/components/timepicker/README.md +277 -0
  139. package/src/components/timepicker/api.ts +632 -0
  140. package/src/components/timepicker/clockdial.ts +482 -0
  141. package/src/components/timepicker/config.ts +228 -0
  142. package/src/components/timepicker/index.ts +3 -0
  143. package/src/components/timepicker/render.ts +613 -0
  144. package/src/components/timepicker/timepicker.ts +117 -0
  145. package/src/components/timepicker/types.ts +336 -0
  146. package/src/components/timepicker/utils.ts +241 -0
  147. package/src/components/tooltip/api.ts +1 -1
  148. package/src/components/tooltip/config.ts +27 -6
  149. package/src/components/tooltip/index.ts +0 -1
  150. package/src/components/tooltip/types.ts +13 -3
  151. package/src/components/top-app-bar/config.ts +83 -0
  152. package/src/components/top-app-bar/index.ts +11 -0
  153. package/src/components/top-app-bar/top-app-bar.ts +316 -0
  154. package/src/components/top-app-bar/types.ts +140 -0
  155. package/src/core/build/_ripple.scss +6 -6
  156. package/src/core/build/ripple.ts +72 -95
  157. package/src/core/compose/features/icon.ts +3 -1
  158. package/src/core/compose/features/ripple.ts +4 -1
  159. package/src/core/compose/features/textlabel.ts +23 -2
  160. package/src/core/dom/create.ts +5 -0
  161. package/src/index.ts +9 -0
  162. package/src/styles/abstract/_theme.scss +9 -1
  163. package/src/styles/components/_badge.scss +182 -0
  164. package/src/styles/components/_bottom-app-bar.scss +103 -0
  165. package/src/{components/button/_styles.scss → styles/components/_button.scss} +0 -10
  166. package/src/{components/checkbox/_styles.scss → styles/components/_checkbox.scss} +0 -2
  167. package/src/styles/components/_datepicker.scss +358 -0
  168. package/src/styles/components/_dialog.scss +259 -0
  169. package/src/styles/components/_divider.scss +57 -0
  170. package/src/styles/components/_extended-fab.scss +267 -0
  171. package/src/styles/components/_fab.scss +225 -0
  172. package/src/{components/navigation/_styles.scss → styles/components/_navigation.scss} +1 -0
  173. package/src/styles/components/_search.scss +306 -0
  174. package/src/styles/components/_segmented-button.scss +117 -0
  175. package/src/{components/slider/_styles.scss → styles/components/_slider.scss} +83 -24
  176. package/src/{components/switch/_styles.scss → styles/components/_switch.scss} +0 -2
  177. package/src/{components/tabs/_styles.scss → styles/components/_tabs.scss} +95 -33
  178. package/src/{components/textfield/_styles.scss → styles/components/_textfield.scss} +70 -67
  179. package/src/styles/components/_timepicker.scss +451 -0
  180. package/src/styles/components/_top-app-bar.scss +225 -0
  181. package/src/styles/main.scss +98 -49
  182. package/src/styles/themes/_autumn.scss +21 -0
  183. package/src/styles/themes/_base-theme.scss +61 -0
  184. package/src/styles/themes/_baseline.scss +58 -0
  185. package/src/styles/themes/_bluekhaki.scss +125 -0
  186. package/src/styles/themes/_brownbeige.scss +125 -0
  187. package/src/styles/themes/_browngreen.scss +125 -0
  188. package/src/styles/themes/_forest.scss +6 -0
  189. package/src/styles/themes/_greenbeige.scss +125 -0
  190. package/src/styles/themes/_material.scss +125 -0
  191. package/src/styles/themes/_ocean.scss +6 -0
  192. package/src/styles/themes/_sageivory.scss +125 -0
  193. package/src/styles/themes/_spring.scss +6 -0
  194. package/src/styles/themes/_summer.scss +5 -0
  195. package/src/styles/themes/_sunset.scss +5 -0
  196. package/src/styles/themes/_tealcaramel.scss +125 -0
  197. package/src/styles/themes/_winter.scss +6 -0
  198. package/src/components/badge/_styles.scss +0 -174
  199. package/src/components/badge/constants.ts +0 -30
  200. package/src/components/button/constants.ts +0 -11
  201. package/src/components/card/constants.ts +0 -84
  202. package/src/components/dialog/_styles.scss +0 -213
  203. package/src/components/dialog/constants.ts +0 -32
  204. package/src/components/menu/constants.ts +0 -154
  205. package/src/components/navigation/constants.ts +0 -200
  206. package/src/components/navigation/features/items.js +0 -192
  207. package/src/components/progress/constants.ts +0 -29
  208. package/src/components/slider/features/appearance.ts +0 -94
  209. package/src/components/slider/features/disabled.ts +0 -68
  210. package/src/components/slider/features/events.ts +0 -164
  211. package/src/components/slider/features/interactions.ts +0 -396
  212. package/src/components/slider/features/keyboard.ts +0 -233
  213. package/src/components/switch/constants.ts +0 -80
  214. package/src/components/tabs/constants.ts +0 -89
  215. package/src/core/collection/adapters/mongodb.js +0 -232
  216. /package/src/{components/card/_styles.scss → styles/components/_card.scss} +0 -0
  217. /package/src/{components/carousel/_styles.scss → styles/components/_carousel.scss} +0 -0
  218. /package/src/{components/chip/_styles.scss → styles/components/_chip.scss} +0 -0
  219. /package/src/{components/list/_styles.scss → styles/components/_list.scss} +0 -0
  220. /package/src/{components/menu/_styles.scss → styles/components/_menu.scss} +0 -0
  221. /package/src/{components/progress/_styles.scss → styles/components/_progress.scss} +0 -0
  222. /package/src/{components/radios/_styles.scss → styles/components/_radios.scss} +0 -0
  223. /package/src/{components/sheet/_styles.scss → styles/components/_sheet.scss} +0 -0
  224. /package/src/{components/snackbar/_styles.scss → styles/components/_snackbar.scss} +0 -0
  225. /package/src/{components/tooltip/_styles.scss → styles/components/_tooltip.scss} +0 -0
  226. /package/src/styles/utilities/{_color.scss → _colors.scss} +0 -0
@@ -0,0 +1,397 @@
1
+ // src/components/datepicker/types.ts
2
+
3
+ /**
4
+ * DatePicker variant types
5
+ * @category Components
6
+ */
7
+ export type DatePickerVariant = 'docked' | 'modal' | 'modal-input';
8
+
9
+ /**
10
+ * DatePicker view types
11
+ * @category Components
12
+ */
13
+ export type DatePickerView = 'day' | 'month' | 'year';
14
+
15
+ /**
16
+ * DatePicker selection mode types
17
+ * @category Components
18
+ */
19
+ export type DatePickerSelectionMode = 'single' | 'range';
20
+
21
+ /**
22
+ * Day names for the calendar
23
+ * @internal
24
+ */
25
+ export const DAY_NAMES = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
26
+
27
+ /**
28
+ * Month names for the calendar
29
+ * @internal
30
+ */
31
+ export const MONTH_NAMES = [
32
+ 'January', 'February', 'March', 'April',
33
+ 'May', 'June', 'July', 'August',
34
+ 'September', 'October', 'November', 'December'
35
+ ];
36
+
37
+ /**
38
+ * Month names abbreviated
39
+ * @internal
40
+ */
41
+ export const MONTH_NAMES_SHORT = [
42
+ 'Jan', 'Feb', 'Mar', 'Apr',
43
+ 'May', 'Jun', 'Jul', 'Aug',
44
+ 'Sep', 'Oct', 'Nov', 'Dec'
45
+ ];
46
+
47
+ /**
48
+ * Default format for displaying dates
49
+ * @internal
50
+ */
51
+ export const DEFAULT_DATE_FORMAT = 'MM/DD/YYYY';
52
+
53
+ /**
54
+ * CSS class name for today's date
55
+ * @internal
56
+ */
57
+ export const TODAY_CLASS = 'today';
58
+
59
+ /**
60
+ * CSS class name for selected date
61
+ * @internal
62
+ */
63
+ export const SELECTED_CLASS = 'selected';
64
+
65
+ /**
66
+ * CSS class name for dates outside the current month
67
+ * @internal
68
+ */
69
+ export const OUTSIDE_MONTH_CLASS = 'outside-month';
70
+
71
+ /**
72
+ * CSS class for the first date in a range
73
+ * @internal
74
+ */
75
+ export const RANGE_START_CLASS = 'range-start';
76
+
77
+ /**
78
+ * CSS class for the last date in a range
79
+ * @internal
80
+ */
81
+ export const RANGE_END_CLASS = 'range-end';
82
+
83
+ /**
84
+ * CSS class for dates between start and end in a range
85
+ * @internal
86
+ */
87
+ export const RANGE_MIDDLE_CLASS = 'range-middle';
88
+
89
+ /**
90
+ * Configuration interface for the DatePicker component
91
+ * @category Components
92
+ */
93
+ export interface DatePickerConfig {
94
+ /**
95
+ * DatePicker variant that determines display style
96
+ * @default 'docked'
97
+ */
98
+ variant?: DatePickerVariant | string;
99
+
100
+ /**
101
+ * Whether the datepicker is initially disabled
102
+ * @default false
103
+ */
104
+ disabled?: boolean;
105
+
106
+ /**
107
+ * Initial view to display (day, month, year)
108
+ * @default 'day'
109
+ */
110
+ initialView?: DatePickerView | string;
111
+
112
+ /**
113
+ * Selection mode for the datepicker (single or range)
114
+ * @default 'single'
115
+ */
116
+ selectionMode?: DatePickerSelectionMode | string;
117
+
118
+ /**
119
+ * Initial selected date(s)
120
+ * Accepts a Date object, Date string, or two dates for range selection
121
+ */
122
+ value?: Date | string | [Date | string, Date | string];
123
+
124
+ /**
125
+ * Minimum selectable date
126
+ */
127
+ minDate?: Date | string;
128
+
129
+ /**
130
+ * Maximum selectable date
131
+ */
132
+ maxDate?: Date | string;
133
+
134
+ /**
135
+ * Format for displaying dates
136
+ * @default 'MM/DD/YYYY'
137
+ */
138
+ dateFormat?: string;
139
+
140
+ /**
141
+ * Label text for the datepicker
142
+ * @example 'Select Date'
143
+ */
144
+ label?: string;
145
+
146
+ /**
147
+ * Placeholder text for the input field
148
+ * @example 'MM/DD/YYYY'
149
+ */
150
+ placeholder?: string;
151
+
152
+ /**
153
+ * Additional CSS classes to add to the datepicker
154
+ * @example 'form-field event-date'
155
+ */
156
+ class?: string;
157
+
158
+ /**
159
+ * Component prefix for class names
160
+ * @default 'mtrl'
161
+ */
162
+ prefix?: string;
163
+
164
+ /**
165
+ * Component name used in class generation
166
+ */
167
+ componentName?: string;
168
+
169
+ /**
170
+ * Whether to enable animation effects
171
+ * @default true
172
+ */
173
+ animate?: boolean;
174
+
175
+ /**
176
+ * Whether to close the picker when a date is selected
177
+ * @default true for modal variants, false for docked
178
+ */
179
+ closeOnSelect?: boolean;
180
+
181
+ /**
182
+ * Array of dates to highlight or disable
183
+ */
184
+ specialDates?: Array<{
185
+ date: Date | string;
186
+ highlight?: boolean;
187
+ disabled?: boolean;
188
+ tooltip?: string;
189
+ }>;
190
+ }
191
+
192
+ /**
193
+ * Date object with additional properties for rendering
194
+ * @internal
195
+ */
196
+ export interface CalendarDate {
197
+ /** JavaScript Date object */
198
+ date: Date;
199
+
200
+ /** Day of the month (1-31) */
201
+ day: number;
202
+
203
+ /** Whether the date is in the current month being displayed */
204
+ isCurrentMonth: boolean;
205
+
206
+ /** Whether the date is today */
207
+ isToday: boolean;
208
+
209
+ /** Whether the date is selected */
210
+ isSelected: boolean;
211
+
212
+ /** Whether the date is disabled */
213
+ isDisabled: boolean;
214
+
215
+ /** Whether the date is the first date in a range */
216
+ isRangeStart?: boolean;
217
+
218
+ /** Whether the date is the last date in a range */
219
+ isRangeEnd?: boolean;
220
+
221
+ /** Whether the date is between start and end in a range */
222
+ isRangeMiddle?: boolean;
223
+ }
224
+
225
+ /**
226
+ * Calendar API interface for managing calendar navigation
227
+ * @category Components
228
+ */
229
+ export interface CalendarAPI {
230
+ /** Updates the calendar view to display a specific month/year */
231
+ goToDate: (date: Date) => void;
232
+
233
+ /** Moves to the next month */
234
+ nextMonth: () => void;
235
+
236
+ /** Moves to the previous month */
237
+ prevMonth: () => void;
238
+
239
+ /** Moves to the next year */
240
+ nextYear: () => void;
241
+
242
+ /** Moves to the previous year */
243
+ prevYear: () => void;
244
+
245
+ /** Switches to day selection view */
246
+ showDayView: () => void;
247
+
248
+ /** Switches to month selection view */
249
+ showMonthView: () => void;
250
+
251
+ /** Switches to year selection view */
252
+ showYearView: () => void;
253
+
254
+ /** Gets the current calendar view */
255
+ getCurrentView: () => string;
256
+ }
257
+
258
+ /**
259
+ * DatePicker component interface
260
+ * @category Components
261
+ */
262
+ export interface DatePickerComponent {
263
+ /** The datepicker's main DOM element */
264
+ element: HTMLElement;
265
+
266
+ /** The input field DOM element */
267
+ input: HTMLInputElement;
268
+
269
+ /** API for managing calendar and selected dates */
270
+ calendar: CalendarAPI;
271
+
272
+ /** API for managing disabled state */
273
+ disabled: {
274
+ /** Enables the datepicker */
275
+ enable: () => void;
276
+ /** Disables the datepicker */
277
+ disable: () => void;
278
+ /** Checks if the datepicker is disabled */
279
+ isDisabled: () => boolean;
280
+ };
281
+
282
+ /** API for managing component lifecycle */
283
+ lifecycle: {
284
+ /** Destroys the component and cleans up resources */
285
+ destroy: () => void;
286
+ };
287
+
288
+ /**
289
+ * Gets a class name with the component's prefix
290
+ * @param name - Base class name
291
+ * @returns Prefixed class name
292
+ */
293
+ getClass: (name: string) => string;
294
+
295
+ /**
296
+ * Opens the datepicker dropdown/modal
297
+ * @returns The datepicker component for chaining
298
+ */
299
+ open: () => DatePickerComponent;
300
+
301
+ /**
302
+ * Closes the datepicker dropdown/modal
303
+ * @returns The datepicker component for chaining
304
+ */
305
+ close: () => DatePickerComponent;
306
+
307
+ /**
308
+ * Gets the selected date(s)
309
+ * @returns Date object, array of two Date objects for range, or null if none selected
310
+ */
311
+ getValue: () => Date | [Date, Date] | null;
312
+
313
+ /**
314
+ * Sets the selected date(s)
315
+ * @param value - Date, string, or array of dates for range selection
316
+ * @returns The datepicker component for chaining
317
+ */
318
+ setValue: (value: Date | string | [Date | string, Date | string]) => DatePickerComponent;
319
+
320
+ /**
321
+ * Gets the formatted date string based on the selected date(s)
322
+ * @returns Formatted date string or empty string if none selected
323
+ */
324
+ getFormattedValue: () => string;
325
+
326
+ /**
327
+ * Clears the selected date(s)
328
+ * @returns The datepicker component for chaining
329
+ */
330
+ clear: () => DatePickerComponent;
331
+
332
+ /**
333
+ * Enables the datepicker
334
+ * @returns The datepicker component for chaining
335
+ */
336
+ enable: () => DatePickerComponent;
337
+
338
+ /**
339
+ * Disables the datepicker
340
+ * @returns The datepicker component for chaining
341
+ */
342
+ disable: () => DatePickerComponent;
343
+
344
+ /**
345
+ * Sets the minimum selectable date
346
+ * @param date - Date object or string
347
+ * @returns The datepicker component for chaining
348
+ */
349
+ setMinDate: (date: Date | string) => DatePickerComponent;
350
+
351
+ /**
352
+ * Sets the maximum selectable date
353
+ * @param date - Date object or string
354
+ * @returns The datepicker component for chaining
355
+ */
356
+ setMaxDate: (date: Date | string) => DatePickerComponent;
357
+
358
+ /**
359
+ * Destroys the datepicker component and cleans up resources
360
+ */
361
+ destroy: () => void;
362
+
363
+ /**
364
+ * Adds an event listener to the datepicker
365
+ * @param event - Event name ('change', 'open', 'close', etc.)
366
+ * @param handler - Event handler function
367
+ * @returns The datepicker component for chaining
368
+ */
369
+ on: (event: string, handler: Function) => DatePickerComponent;
370
+
371
+ /**
372
+ * Removes an event listener from the datepicker
373
+ * @param event - Event name
374
+ * @param handler - Event handler function
375
+ * @returns The datepicker component for chaining
376
+ */
377
+ off: (event: string, handler: Function) => DatePickerComponent;
378
+ }
379
+
380
+ /**
381
+ * API options interface for withAPI function
382
+ * @internal
383
+ */
384
+ export interface ApiOptions {
385
+ disabled: {
386
+ enable: () => void;
387
+ disable: () => void;
388
+ };
389
+ lifecycle: {
390
+ destroy: () => void;
391
+ };
392
+ events: {
393
+ on: (event: string, handler: Function) => any;
394
+ off: (event: string, handler: Function) => any;
395
+ emit: (event: string, data: any) => any;
396
+ };
397
+ }
@@ -0,0 +1,289 @@
1
+ // src/components/datepicker/utils.ts
2
+ import { CalendarDate, MONTH_NAMES, MONTH_NAMES_SHORT } from './types';
3
+
4
+ /**
5
+ * Parses a date from various input types
6
+ * @param date - Date string, Date object, or null
7
+ * @returns Valid Date object or null if invalid
8
+ */
9
+ export const parseDate = (date: Date | string | null): Date | null => {
10
+ if (!date) return null;
11
+
12
+ // Already a Date object
13
+ if (date instanceof Date) {
14
+ return isNaN(date.getTime()) ? null : date;
15
+ }
16
+
17
+ // String date
18
+ if (typeof date === 'string') {
19
+ // Try to parse the string
20
+ const parsedDate = new Date(date);
21
+ return isNaN(parsedDate.getTime()) ? null : parsedDate;
22
+ }
23
+
24
+ return null;
25
+ };
26
+
27
+ /**
28
+ * Formats a date according to the specified format using a parser-based approach
29
+ * This avoids string replacement issues by building the output string from scratch
30
+ *
31
+ * @param date - Date to format
32
+ * @param format - Format string (MM/DD/YYYY, etc.)
33
+ * @returns Formatted date string
34
+ */
35
+ export const formatDate = (date: Date | null, format: string = 'MM/DD/YYYY'): string => {
36
+ if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
37
+ return '';
38
+ }
39
+
40
+ let result = '';
41
+ let i = 0;
42
+
43
+ while (i < format.length) {
44
+ // Check for month name patterns
45
+ if (format.substring(i, i+4) === 'MMMM') {
46
+ // Full month name
47
+ result += MONTH_NAMES[date.getMonth()];
48
+ i += 4;
49
+ }
50
+ else if (format.substring(i, i+3) === 'MMM') {
51
+ // Abbreviated month name
52
+ result += MONTH_NAMES_SHORT[date.getMonth()];
53
+ i += 3;
54
+ }
55
+ else if (format.substring(i, i+2) === 'MM') {
56
+ // Two-digit month
57
+ result += (date.getMonth() + 1).toString().padStart(2, '0');
58
+ i += 2;
59
+ }
60
+ else if (format.substring(i, i+1) === 'M') {
61
+ // Single-digit month
62
+ result += (date.getMonth() + 1);
63
+ i += 1;
64
+ }
65
+ else if (format.substring(i, i+4) === 'YYYY') {
66
+ // 4-digit year
67
+ result += date.getFullYear();
68
+ i += 4;
69
+ }
70
+ else if (format.substring(i, i+2) === 'YY') {
71
+ // 2-digit year
72
+ result += date.getFullYear().toString().slice(-2);
73
+ i += 2;
74
+ }
75
+ else if (format.substring(i, i+2) === 'DD') {
76
+ // Two-digit day
77
+ result += date.getDate().toString().padStart(2, '0');
78
+ i += 2;
79
+ }
80
+ else if (format.substring(i, i+1) === 'D') {
81
+ // Single-digit day
82
+ result += date.getDate();
83
+ i += 1;
84
+ }
85
+ else {
86
+ // Any other character is copied as-is
87
+ result += format[i];
88
+ i += 1;
89
+ }
90
+ }
91
+
92
+ return result;
93
+ };
94
+
95
+ /**
96
+ * Gets the days in a month
97
+ * @param year - Year
98
+ * @param month - Month (0-11)
99
+ * @returns Number of days in the month
100
+ */
101
+ export const getDaysInMonth = (year: number, month: number): number => {
102
+ return new Date(year, month + 1, 0).getDate();
103
+ };
104
+
105
+ /**
106
+ * Gets the first day of the month
107
+ * @param year - Year
108
+ * @param month - Month (0-11)
109
+ * @returns Day of the week (0-6, where 0 is Sunday)
110
+ */
111
+ export const getFirstDayOfMonth = (year: number, month: number): number => {
112
+ return new Date(year, month, 1).getDay();
113
+ };
114
+
115
+ /**
116
+ * Checks if two dates are the same day
117
+ * @param date1 - First date
118
+ * @param date2 - Second date
119
+ * @returns True if same day, false otherwise
120
+ */
121
+ export const isSameDay = (date1: Date, date2: Date): boolean => {
122
+ return (
123
+ date1.getDate() === date2.getDate() &&
124
+ date1.getMonth() === date2.getMonth() &&
125
+ date1.getFullYear() === date2.getFullYear()
126
+ );
127
+ };
128
+
129
+ /**
130
+ * Checks if a date is between two other dates (inclusive)
131
+ * @param date - Date to check
132
+ * @param startDate - Start date
133
+ * @param endDate - End date
134
+ * @returns True if date is between start and end, false otherwise
135
+ */
136
+ export const isDateInRange = (date: Date, startDate: Date, endDate: Date): boolean => {
137
+ const timestamp = date.getTime();
138
+ return timestamp >= startDate.getTime() && timestamp <= endDate.getTime();
139
+ };
140
+
141
+ /**
142
+ * Generates calendar dates for a month
143
+ * @param year - Year
144
+ * @param month - Month (0-11)
145
+ * @param selectedDate - Currently selected date
146
+ * @param rangeEndDate - End date if range selection
147
+ * @param minDate - Minimum selectable date
148
+ * @param maxDate - Maximum selectable date
149
+ * @returns Array of calendar dates
150
+ */
151
+ export const generateCalendarDates = (
152
+ year: number,
153
+ month: number,
154
+ selectedDate: Date | null = null,
155
+ rangeEndDate: Date | null = null,
156
+ minDate: Date | null = null,
157
+ maxDate: Date | null = null
158
+ ): CalendarDate[] => {
159
+ const today = new Date();
160
+ today.setHours(0, 0, 0, 0);
161
+
162
+ const result: CalendarDate[] = [];
163
+
164
+ // Calculate days needed from previous month
165
+ const firstDay = getFirstDayOfMonth(year, month);
166
+ const daysInPrevMonth = getDaysInMonth(year, month - 1);
167
+
168
+ // Add days from previous month
169
+ for (let i = firstDay - 1; i >= 0; i--) {
170
+ const date = new Date(year, month - 1, daysInPrevMonth - i);
171
+ const isDisabled =
172
+ (minDate && date < minDate) ||
173
+ (maxDate && date > maxDate);
174
+
175
+ result.push({
176
+ date,
177
+ day: date.getDate(),
178
+ isCurrentMonth: false,
179
+ isToday: isSameDay(date, today),
180
+ isSelected: selectedDate ? isSameDay(date, selectedDate) : false,
181
+ isDisabled
182
+ });
183
+ }
184
+
185
+ // Add days from current month
186
+ const daysInMonth = getDaysInMonth(year, month);
187
+ for (let i = 1; i <= daysInMonth; i++) {
188
+ const date = new Date(year, month, i);
189
+ const isSelected = selectedDate ? isSameDay(date, selectedDate) : false;
190
+ const isDisabled =
191
+ (minDate && date < minDate) ||
192
+ (maxDate && date > maxDate);
193
+
194
+ const calendarDate: CalendarDate = {
195
+ date,
196
+ day: i,
197
+ isCurrentMonth: true,
198
+ isToday: isSameDay(date, today),
199
+ isSelected,
200
+ isDisabled
201
+ };
202
+
203
+ // Handle range selection
204
+ if (selectedDate && rangeEndDate) {
205
+ calendarDate.isRangeStart = isSameDay(date, selectedDate);
206
+ calendarDate.isRangeEnd = isSameDay(date, rangeEndDate);
207
+ calendarDate.isRangeMiddle = isDateInRange(date, selectedDate, rangeEndDate) &&
208
+ !calendarDate.isRangeStart && !calendarDate.isRangeEnd;
209
+ }
210
+
211
+ result.push(calendarDate);
212
+ }
213
+
214
+ // Add days from next month to complete the calendar grid (6 rows × 7 columns)
215
+ const totalDaysNeeded = 42; // 6 rows × 7 columns
216
+ const remainingDays = totalDaysNeeded - result.length;
217
+
218
+ for (let i = 1; i <= remainingDays; i++) {
219
+ const date = new Date(year, month + 1, i);
220
+ const isDisabled =
221
+ (minDate && date < minDate) ||
222
+ (maxDate && date > maxDate);
223
+
224
+ result.push({
225
+ date,
226
+ day: i,
227
+ isCurrentMonth: false,
228
+ isToday: isSameDay(date, today),
229
+ isSelected: selectedDate ? isSameDay(date, selectedDate) : false,
230
+ isDisabled
231
+ });
232
+ }
233
+
234
+ return result;
235
+ };
236
+
237
+ /**
238
+ * Generates an array of years for year selection
239
+ * @param currentYear - Center year for the range
240
+ * @param range - Number of years before and after current year
241
+ * @returns Array of years
242
+ */
243
+ export const generateYearRange = (currentYear: number, range: number = 10): number[] => {
244
+ const years: number[] = [];
245
+ const startYear = currentYear - range;
246
+ const endYear = currentYear + range;
247
+
248
+ for (let year = startYear; year <= endYear; year++) {
249
+ years.push(year);
250
+ }
251
+
252
+ return years;
253
+ };
254
+
255
+ /**
256
+ * Adds days to a date
257
+ * @param date - Base date
258
+ * @param days - Number of days to add
259
+ * @returns New date with days added
260
+ */
261
+ export const addDays = (date: Date, days: number): Date => {
262
+ const result = new Date(date);
263
+ result.setDate(result.getDate() + days);
264
+ return result;
265
+ };
266
+
267
+ /**
268
+ * Adds months to a date
269
+ * @param date - Base date
270
+ * @param months - Number of months to add
271
+ * @returns New date with months added
272
+ */
273
+ export const addMonths = (date: Date, months: number): Date => {
274
+ const result = new Date(date);
275
+ result.setMonth(result.getMonth() + months);
276
+ return result;
277
+ };
278
+
279
+ /**
280
+ * Adds years to a date
281
+ * @param date - Base date
282
+ * @param years - Number of years to add
283
+ * @returns New date with years added
284
+ */
285
+ export const addYears = (date: Date, years: number): Date => {
286
+ const result = new Date(date);
287
+ result.setFullYear(result.getFullYear() + years);
288
+ return result;
289
+ };