|
4 | 4 | --> |
5 | 5 |
|
6 | 6 | <template> |
| 7 | + <!-- |
| 8 | + `format` is specified as a workaround for https://github.com/nextcloud/calendar/issues/8307 |
| 9 | + until the issue is fixed in `@nextcloud/vue`. |
| 10 | +
|
| 11 | + This works because `@vuepic/vue-datepicker` can use it to parse text inputs from the user |
| 12 | + when providing a format as pattern string (and not a one-way function). |
| 13 | + --> |
7 | 14 | <DateTimePicker |
8 | 15 | id="date-time-picker-input" |
9 | 16 | :min="minimumDate" |
|
12 | 19 | :type="type" |
13 | 20 | :hideLabel="true" |
14 | 21 | class="date-time-picker" |
| 22 | + :format="formatStr" |
15 | 23 | @blur="onBlur" |
16 | 24 | @update:modelValue="onInput" /> |
17 | 25 | </template> |
18 | 26 |
|
19 | 27 | <script> |
| 28 | +import { getCanonicalLocale } from '@nextcloud/l10n' |
20 | 29 | import { |
21 | 30 | NcDateTimePicker as DateTimePicker, |
22 | 31 | } from '@nextcloud/vue' |
| 32 | +import { getDateLocalePattern, getDateTimeLocalePattern, getTimeLocalePattern } from 'datetime-locale-patterns' |
23 | 33 | import { mapStores } from 'pinia' |
24 | 34 | import useDavRestrictionsStore from '../../store/davRestrictions.js' |
25 | 35 |
|
| 36 | +const canonicalLocale = getCanonicalLocale() |
| 37 | +
|
26 | 38 | export default { |
27 | 39 | name: 'DatePicker', |
28 | 40 | components: { |
@@ -91,6 +103,28 @@ export default { |
91 | 103 |
|
92 | 104 | return this.max || new Date(this.davRestrictionsStore.maximumDate) |
93 | 105 | }, |
| 106 | +
|
| 107 | + /** |
| 108 | + * Returns the date format pattern for the current picker type. |
| 109 | + * |
| 110 | + * See https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table |
| 111 | + * |
| 112 | + * @return {string | undefined} A format pattern or `undefined` to use the default `NcDateTimePicker` formatting. |
| 113 | + */ |
| 114 | + formatStr() { |
| 115 | + // Match logic from https://github.com/nextcloud-libraries/nextcloud-vue/blob/v9.8.0/src/components/NcDateTimePicker/NcDateTimePicker.vue#L549 |
| 116 | + if (this.type === 'date' || this.type === 'date-range') { |
| 117 | + return getDateLocalePattern(canonicalLocale, { dateStyle: 'medium' }) |
| 118 | + } else if (this.type === 'time' || this.type === 'time-range') { |
| 119 | + return getTimeLocalePattern(canonicalLocale, { timeStyle: 'short' }) |
| 120 | + } else if (this.type === 'datetime' || this.type === 'datetime-range') { |
| 121 | + return getDateTimeLocalePattern(canonicalLocale, { dateStyle: 'medium', timeStyle: 'short' }) |
| 122 | + } else { |
| 123 | + // 'datetime-locale-patterns' does not support `month` and `year`. |
| 124 | + // So fall back to default formatting. |
| 125 | + return undefined |
| 126 | + } |
| 127 | + }, |
94 | 128 | }, |
95 | 129 |
|
96 | 130 | methods: { |
|
0 commit comments