@zooui/nuxt 0.1.1 → 0.2.0
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.
- package/README.md +1 -1
- package/components/DateField.vue +163 -37
- package/package.json +1 -1
- package/utils/date-entry.ts +180 -6
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ plain Nuxt UI. Three rules:
|
|
|
62
62
|
| `ZThemeToggle` | Light / dark / system |
|
|
63
63
|
| `ZRepeatingList` | A generic list editor |
|
|
64
64
|
| `ZNumberInput` | One number input for money, percent, count and hours, with a calc mode |
|
|
65
|
-
| `ZDateField` | A calendar-day field on `Temporal.PlainDate` |
|
|
65
|
+
| `ZDateField` | A calendar-day field on `Temporal.PlainDate` or `CalendarDate`, with Nuxt UI's limit props |
|
|
66
66
|
|
|
67
67
|
## Docs
|
|
68
68
|
|
package/components/DateField.vue
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// A calendar-day field: type a date, press a shortcut key, or pick from a calendar. The model is a Temporal.PlainDate (
|
|
3
|
-
// column comes back as, so a form round-trips the server's value without ever touching a string or a
|
|
1
|
+
<script setup lang="ts" generic="T extends Temporal.PlainDate | CalendarDateLike | null">
|
|
2
|
+
// A calendar-day field: type a date, press a shortcut key, or pick from a calendar. The model is a Temporal.PlainDate (the
|
|
3
|
+
// same type a `date` column comes back as, so a form round-trips the server's value without ever touching a string or a
|
|
4
|
+
// time zone) or an @internationalized/date CalendarDate (the type Nuxt UI's UCalendar and UInputDate use), or null.
|
|
5
|
+
//
|
|
6
|
+
// THE CALLER'S TYPE. Whatever the model holds is what comes back, in `update:modelValue`, `dateUpdate` and `invalid`
|
|
7
|
+
// alike; a CalendarDate is built with the caller's own class. A model that starts out null hands back `modelType`'s
|
|
8
|
+
// type ('plain-date' by default), and one that held a day and was cleared keeps the type it had. Only a Gregorian
|
|
9
|
+
// CalendarDate is taken: one in another calendar throws (toPlainDate), since converting it by its fields would land
|
|
10
|
+
// on another day. Inside, everything is a PlainDate; the conversion is at the edges.
|
|
4
11
|
//
|
|
5
12
|
// WHY NO `Date`. A JavaScript Date is an instant, not a day. Every conversion between the two goes through a
|
|
6
13
|
// time zone, and every one of those is a chance for the 12th to become the 11th. So nothing here constructs one:
|
|
7
|
-
// the typed text is parsed by parseDateEntry (utils/date-entry.ts, pure Temporal), and
|
|
8
|
-
//
|
|
9
|
-
// "Today" comes from useToday().
|
|
14
|
+
// the typed text is parsed by parseDateEntry (utils/date-entry.ts, pure Temporal), and a CalendarDate is converted
|
|
15
|
+
// by its fields alone (year, month, day), never via toDate()/getLocalTimeZone(). "Today" comes from useToday().
|
|
10
16
|
//
|
|
11
17
|
// <ZDateField v-model="form.endDate" clearable class="w-full" />
|
|
12
18
|
// <ZDateField v-model="due" format="DD.MM.YYYY" locale="de" />
|
|
19
|
+
// <ZDateField v-model="due" :min-value="opened" :max-value="closes" :is-date-unavailable="isHoliday" />
|
|
13
20
|
//
|
|
14
21
|
// FORMAT AND LOCALE. `locale` names the calendar's language and week (Nuxt UI's locale when unset, `en` unless the
|
|
15
22
|
// app's <UApp :locale> says otherwise). `format` is how the box writes and reads a day — a pattern such as
|
|
@@ -22,6 +29,14 @@
|
|
|
22
29
|
// start/end of month, y/r start/end of year, + and - a day either way (_ too, so a format whose separator is "-"
|
|
23
30
|
// keeps a key for the day before).
|
|
24
31
|
//
|
|
32
|
+
// LIMITS. `minValue`, `maxValue` and `isDateUnavailable`, named as Nuxt UI's UCalendar names them, go to the calendar,
|
|
33
|
+
// which disables those days. A typed date they refuse is not committed: the field keeps the date it had and draws it
|
|
34
|
+
// back in the box (as with text that isn't a date, the box never shows a day the form isn't holding), and is marked
|
|
35
|
+
// invalid — aria-invalid, Nuxt UI's error color, a message for screen readers — until the next edit, pick, shortcut or
|
|
36
|
+
// clear. `invalid` carries the problem (why, the date, the limit, an English message) for the product to show, and
|
|
37
|
+
// null once it's gone; a refused Enter doesn't announce `dateUpdate`. The shortcuts stop at the limits and skip
|
|
38
|
+
// unavailable days (applyDateShortcut says how, `t` included).
|
|
39
|
+
//
|
|
25
40
|
// `dateUpdate` fires when a date is settled — picked, cleared, set by a shortcut, committed with Enter, or when
|
|
26
41
|
// focus leaves the field for somewhere other than its own calendar (Tab included, once) — which is the moment
|
|
27
42
|
// an inline editor (a table cell) should save and close. It always carries the date just settled, even when
|
|
@@ -30,15 +45,23 @@
|
|
|
30
45
|
//
|
|
31
46
|
// Also: a leading `icon` whose click emits `iconClick`, a `calendarIcon` in place of the calendar,
|
|
32
47
|
// and one-letter weekday headers (the locale's narrow weekday).
|
|
33
|
-
import type { CalendarDate as CalendarDay } from '@internationalized/date'
|
|
34
48
|
import { CalendarDate } from '@internationalized/date'
|
|
35
|
-
import { useLocale } from '@nuxt/ui/composables'
|
|
36
|
-
import { Temporal } from 'temporal-polyfill/implementation'
|
|
49
|
+
import { formFieldInjectionKey, useLocale } from '@nuxt/ui/composables'
|
|
50
|
+
import type { Temporal } from 'temporal-polyfill/implementation'
|
|
37
51
|
import { useToday } from '../composables/useToday'
|
|
38
|
-
import {
|
|
52
|
+
import {
|
|
53
|
+
applyDateShortcut, checkDateLimits, commitDateEntry, dateEntryFormat, dateEntryFormatFor, dateLimitMessage, dateModelTypeOf, formatDateEntry,
|
|
54
|
+
fromPlainDate, isDateShortcut, parseDateEntry, toPlainDate,
|
|
55
|
+
type CalendarDateLike, type DateLimitProblem, type DateLimits, type DateModelType,
|
|
56
|
+
} from '../utils/date-entry'
|
|
39
57
|
|
|
40
58
|
defineOptions({ inheritAttrs: false })
|
|
41
59
|
|
|
60
|
+
// A CalendarDate is typed by the fields read from it (CalendarDateLike), not its class: Vue's ref() unwraps a class
|
|
61
|
+
// with private fields into a type the class no longer accepts, so a `ref<CalendarDate | null>` would not type-check
|
|
62
|
+
// against the class itself.
|
|
63
|
+
type Day = NonNullable<T>
|
|
64
|
+
|
|
42
65
|
const props = withDefaults(
|
|
43
66
|
defineProps<{
|
|
44
67
|
clearable?: boolean
|
|
@@ -61,37 +84,108 @@ const props = withDefaults(
|
|
|
61
84
|
/** The two buttons' accessible names, for a product in another language. */
|
|
62
85
|
clearLabel?: string
|
|
63
86
|
chooseLabel?: string
|
|
87
|
+
/** The type to hand back while the model is null and has held no day: a CalendarDate v-model that starts out
|
|
88
|
+
* null needs `calendar-date`. */
|
|
89
|
+
modelType?: DateModelType
|
|
90
|
+
/** The first day the field takes, a PlainDate or a CalendarDate. */
|
|
91
|
+
minValue?: Temporal.PlainDate | CalendarDateLike
|
|
92
|
+
/** The last day the field takes, a PlainDate or a CalendarDate. */
|
|
93
|
+
maxValue?: Temporal.PlainDate | CalendarDateLike
|
|
94
|
+
/** Days the field refuses between the two; called with a day of the model's type. */
|
|
95
|
+
isDateUnavailable?: (date: Day) => boolean
|
|
64
96
|
}>(),
|
|
65
|
-
{ portal: true, calendarIcon: 'i-lucide-calendar', clearLabel: 'Clear date', chooseLabel: 'Choose a date' },
|
|
97
|
+
{ portal: true, calendarIcon: 'i-lucide-calendar', clearLabel: 'Clear date', chooseLabel: 'Choose a date', modelType: 'plain-date' },
|
|
66
98
|
)
|
|
67
99
|
const emit = defineEmits<{
|
|
68
|
-
dateUpdate: [value:
|
|
100
|
+
dateUpdate: [value: T | null]
|
|
101
|
+
// A typed day refused by the limits, and null once the field is no longer marked invalid.
|
|
102
|
+
invalid: [problem: (DateLimitProblem<Day> & { message: string }) | null]
|
|
69
103
|
clear: []
|
|
70
104
|
// A click on the leading `icon` (`iconClick`).
|
|
71
105
|
iconClick: [event: MouseEvent]
|
|
72
106
|
}>()
|
|
73
|
-
const model = defineModel<
|
|
107
|
+
const model = defineModel<T | null>({ default: null })
|
|
74
108
|
|
|
75
109
|
const { locale: uiLocale } = useLocale()
|
|
76
110
|
const localeCode = computed(() => props.locale ?? uiLocale.value.code)
|
|
77
111
|
const entryFormat = computed(() => (props.format ? dateEntryFormat(props.format) : dateEntryFormatFor(localeCode.value)))
|
|
78
112
|
const show = (d: Temporal.PlainDate | null) => formatDateEntry(d, entryFormat.value)
|
|
79
113
|
|
|
114
|
+
// The caller's type, kept through an empty spell, and the CalendarDate it last held (whose class builds the next).
|
|
115
|
+
let remembered: DateModelType | undefined
|
|
116
|
+
let like: CalendarDateLike | undefined
|
|
117
|
+
function remember(v: unknown) {
|
|
118
|
+
const type = dateModelTypeOf(v)
|
|
119
|
+
if (type) remembered = type
|
|
120
|
+
if (type === 'calendar-date') like = v as CalendarDateLike
|
|
121
|
+
}
|
|
122
|
+
remember(model.value)
|
|
123
|
+
const kind = () => dateModelTypeOf(model.value) ?? remembered ?? props.modelType
|
|
124
|
+
/** A day as the caller's type. */
|
|
125
|
+
const out = (d: Temporal.PlainDate | null) => fromPlainDate(d, kind(), like) as T | null
|
|
126
|
+
const outDay = (d: Temporal.PlainDate) => fromPlainDate(d, kind(), like) as Day
|
|
127
|
+
|
|
80
128
|
const open = ref(false)
|
|
81
|
-
const text = ref(show(model.value))
|
|
129
|
+
const text = ref(show(toPlainDate(model.value)))
|
|
82
130
|
|
|
83
131
|
const sameDay = (a: Temporal.PlainDate | null, b: Temporal.PlainDate | null) => a === b || (!!a && !!b && a.equals(b))
|
|
84
132
|
|
|
133
|
+
const limits = computed<DateLimits>(() => {
|
|
134
|
+
const unavailable = props.isDateUnavailable
|
|
135
|
+
return {
|
|
136
|
+
minValue: toPlainDate(props.minValue),
|
|
137
|
+
maxValue: toPlainDate(props.maxValue),
|
|
138
|
+
isDateUnavailable: unavailable && ((d: Temporal.PlainDate) => unavailable(outDay(d))),
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
// ── Invalid ───────────────────────────────────────────────────────────────────────────────────────────────────
|
|
143
|
+
const problem = ref<DateLimitProblem | null>(null)
|
|
144
|
+
const problemMessage = computed(() => (problem.value ? dateLimitMessage(problem.value, entryFormat.value) : ''))
|
|
145
|
+
function flag(p: DateLimitProblem | null) {
|
|
146
|
+
if (!p && !problem.value) return
|
|
147
|
+
problem.value = p
|
|
148
|
+
emit('invalid', p && { reason: p.reason, date: outDay(p.date), limit: p.limit && outDay(p.limit), message: problemMessage.value })
|
|
149
|
+
}
|
|
150
|
+
// In a UFormField, Nuxt UI's UInput takes aria-invalid, its error color and aria-describedby from the field, over
|
|
151
|
+
// anything passed to it: so while a day is refused the field the UInput sees carries the message as its error (unless
|
|
152
|
+
// it has one of its own, which the product may have set from `invalid`), and the message below takes the id that
|
|
153
|
+
// aria-describedby names. Outside one, they are passed to the UInput directly.
|
|
154
|
+
const formField = inject(formFieldInjectionKey, undefined)
|
|
155
|
+
if (formField) {
|
|
156
|
+
provide(formFieldInjectionKey, computed(() => {
|
|
157
|
+
const f = formField.value
|
|
158
|
+
return problem.value && f && !f.error ? { ...f, error: problemMessage.value } : f
|
|
159
|
+
}))
|
|
160
|
+
}
|
|
161
|
+
const panelId = useId()
|
|
162
|
+
const messageId = computed(() => {
|
|
163
|
+
if (!problem.value) return undefined
|
|
164
|
+
if (!formField?.value) return `${panelId}-invalid`
|
|
165
|
+
return formField.value.error ? undefined : `${formField.value.ariaId}-error`
|
|
166
|
+
})
|
|
167
|
+
const attrs = useAttrs()
|
|
168
|
+
const invalidAttrs = computed(() => {
|
|
169
|
+
if (!problem.value || formField?.value) return {}
|
|
170
|
+
const describedBy = [attrs['aria-describedby'], messageId.value].filter(Boolean).join(' ')
|
|
171
|
+
return { 'color': 'error', 'aria-invalid': true, 'aria-describedby': describedBy }
|
|
172
|
+
})
|
|
173
|
+
|
|
85
174
|
// The date this field last settled on, which is what `dateUpdate` reports. NEVER re-read `model.value` for
|
|
86
175
|
// that: when the parent owns the v-model, writing it only emits `update:modelValue`, and the prop comes back
|
|
87
176
|
// on the parent's next render — so in the same tick `model.value` is still the OLD date, and a blur that just
|
|
88
177
|
// committed "5/12" would announce null.
|
|
89
|
-
let settled = model.value
|
|
178
|
+
let settled = toPlainDate(model.value)
|
|
90
179
|
// The date `dateUpdate` last carried, boxed so an announced null is told apart from nothing announced yet.
|
|
91
180
|
let announced: { date: Temporal.PlainDate | null } | undefined
|
|
92
|
-
watch(model, (
|
|
181
|
+
watch(model, (v) => {
|
|
182
|
+
remember(v)
|
|
183
|
+
const d = toPlainDate(v)
|
|
93
184
|
// The parent set a different date, so whatever we announced before is no longer what's in the field.
|
|
94
|
-
if (!sameDay(d, settled))
|
|
185
|
+
if (!sameDay(d, settled)) {
|
|
186
|
+
announced = undefined
|
|
187
|
+
flag(null)
|
|
188
|
+
}
|
|
95
189
|
settled = d
|
|
96
190
|
text.value = show(d)
|
|
97
191
|
})
|
|
@@ -99,10 +193,9 @@ watch(model, (d) => {
|
|
|
99
193
|
watch(entryFormat, () => { text.value = show(settled) })
|
|
100
194
|
|
|
101
195
|
const inputCmp = useTemplateRef<{ inputRef: HTMLInputElement | null, $el: HTMLElement }>('inputCmp')
|
|
102
|
-
const panelId = useId()
|
|
103
196
|
|
|
104
197
|
function set(d: Temporal.PlainDate | null) {
|
|
105
|
-
if (!sameDay(d, settled)) model.value = d
|
|
198
|
+
if (!sameDay(d, settled)) model.value = out(d)
|
|
106
199
|
settled = d
|
|
107
200
|
text.value = show(d)
|
|
108
201
|
return d
|
|
@@ -110,9 +203,22 @@ function set(d: Temporal.PlainDate | null) {
|
|
|
110
203
|
|
|
111
204
|
// What's typed becomes the value — or nothing, when it isn't a date. Leaving "5/32" in the box would show one
|
|
112
205
|
// thing while the form saved another. Untouched text keeps the date as it is, so passing through the field
|
|
113
|
-
// can't blank a stored date it wouldn't read back (a year before 1000).
|
|
114
|
-
|
|
115
|
-
|
|
206
|
+
// can't blank a stored date it wouldn't read back (a year before 1000). A day the limits refuse keeps the date
|
|
207
|
+
// there was, drawn back in the box, and marks the field invalid: undefined, for nothing settled.
|
|
208
|
+
function commitText(): { date: Temporal.PlainDate | null } | undefined {
|
|
209
|
+
const next = commitDateEntry(text.value, settled, useToday(), entryFormat.value)
|
|
210
|
+
const refused = next && !sameDay(next, settled) ? checkDateLimits(next, limits.value) : null
|
|
211
|
+
if (refused) {
|
|
212
|
+
text.value = show(settled)
|
|
213
|
+
flag(refused)
|
|
214
|
+
return undefined
|
|
215
|
+
}
|
|
216
|
+
return { date: set(next) }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function onType(v: string | number | null | undefined) {
|
|
220
|
+
text.value = v == null ? '' : String(v)
|
|
221
|
+
flag(null)
|
|
116
222
|
}
|
|
117
223
|
|
|
118
224
|
function focusInput() {
|
|
@@ -133,7 +239,7 @@ function isOurs(target: EventTarget | null) {
|
|
|
133
239
|
|
|
134
240
|
function announce(d: Temporal.PlainDate | null) {
|
|
135
241
|
announced = { date: d }
|
|
136
|
-
emit('dateUpdate', d)
|
|
242
|
+
emit('dateUpdate', out(d))
|
|
137
243
|
}
|
|
138
244
|
|
|
139
245
|
// Leaving settles the field, but a date already announced (by Enter, say) isn't announced again: an inline
|
|
@@ -160,7 +266,10 @@ function onKeydown(e: KeyboardEvent) {
|
|
|
160
266
|
}
|
|
161
267
|
if (e.key === 'Enter') {
|
|
162
268
|
open.value = false
|
|
163
|
-
|
|
269
|
+
const committed = commitText()
|
|
270
|
+
if (committed) announce(committed.date)
|
|
271
|
+
// Refused: the date it kept is selected, so typing another replaces it.
|
|
272
|
+
else nextTick(() => inputCmp.value?.inputRef?.select())
|
|
164
273
|
return
|
|
165
274
|
}
|
|
166
275
|
// Tab commits here, so the text is settled before focus moves; the blur that follows announces it, once.
|
|
@@ -169,12 +278,14 @@ function onKeydown(e: KeyboardEvent) {
|
|
|
169
278
|
open.value = false
|
|
170
279
|
return
|
|
171
280
|
}
|
|
172
|
-
if (e.key.length !== 1) return
|
|
281
|
+
if (e.key.length !== 1 || !isDateShortcut(e.key, entryFormat.value)) return
|
|
282
|
+
e.preventDefault()
|
|
173
283
|
const today = useToday()
|
|
174
|
-
// Shortcuts step from what's on screen, even if it hasn't been committed yet.
|
|
175
|
-
|
|
284
|
+
// Shortcuts step from what's on screen, even if it hasn't been committed yet. With no allowed day to go to, the
|
|
285
|
+
// key does nothing.
|
|
286
|
+
const next = applyDateShortcut(e.key, parseDateEntry(text.value, today, entryFormat.value) ?? settled, today, entryFormat.value, limits.value)
|
|
176
287
|
if (!next) return
|
|
177
|
-
|
|
288
|
+
flag(null)
|
|
178
289
|
announce(set(next))
|
|
179
290
|
// Selected, so typing a date replaces the shortcut's rather than appending to it.
|
|
180
291
|
nextTick(() => inputCmp.value?.inputRef?.select())
|
|
@@ -182,32 +293,41 @@ function onKeydown(e: KeyboardEvent) {
|
|
|
182
293
|
|
|
183
294
|
function clear() {
|
|
184
295
|
const d = set(null)
|
|
296
|
+
flag(null)
|
|
185
297
|
emit('clear')
|
|
186
298
|
announce(d)
|
|
187
299
|
focusInput()
|
|
188
300
|
}
|
|
189
301
|
|
|
190
302
|
// ── Calendar ──────────────────────────────────────────────────────────────────────────────────────────────
|
|
191
|
-
// CalendarDate
|
|
303
|
+
// UCalendar takes Nuxt UI's CalendarDate, a bare year/month/day, so the conversion is by fields both ways.
|
|
192
304
|
const toCalendar = (d: Temporal.PlainDate) => new CalendarDate(d.year, d.month, d.day)
|
|
193
305
|
|
|
194
306
|
// The selection follows text typed but not yet committed, as the month and the shortcuts do, so the picker and the
|
|
195
307
|
// input never disagree; untouched or unreadable text falls back to the model.
|
|
196
308
|
const calendarValue = computed(() => {
|
|
197
|
-
const day = parseDateEntry(text.value, useToday(), entryFormat.value) ?? model.value
|
|
309
|
+
const day = parseDateEntry(text.value, useToday(), entryFormat.value) ?? toPlainDate(model.value)
|
|
198
310
|
return day ? toCalendar(day) : undefined
|
|
199
311
|
})
|
|
200
312
|
// The month the calendar shows: the date in the box (typed or committed), else this month. Only read while
|
|
201
313
|
// the popover is open, which is only ever in the browser.
|
|
202
314
|
const calendarMonth = computed(() =>
|
|
203
|
-
toCalendar(parseDateEntry(text.value, useToday(), entryFormat.value) ?? model.value ?? useToday()),
|
|
315
|
+
toCalendar(parseDateEntry(text.value, useToday(), entryFormat.value) ?? toPlainDate(model.value) ?? useToday()),
|
|
204
316
|
)
|
|
317
|
+
// The limits, for the calendar to disable those days.
|
|
318
|
+
const calendarMin = computed(() => (limits.value.minValue ? toCalendar(limits.value.minValue as Temporal.PlainDate) : undefined))
|
|
319
|
+
const calendarMax = computed(() => (limits.value.maxValue ? toCalendar(limits.value.maxValue as Temporal.PlainDate) : undefined))
|
|
320
|
+
const calendarUnavailable = computed(() => {
|
|
321
|
+
const unavailable = limits.value.isDateUnavailable
|
|
322
|
+
return unavailable ? (v: CalendarDateLike) => unavailable(toPlainDate(v)!) : undefined
|
|
323
|
+
})
|
|
205
324
|
|
|
206
325
|
function onPick(v: unknown) {
|
|
207
|
-
|
|
208
|
-
|
|
326
|
+
const type = dateModelTypeOf(v)
|
|
327
|
+
if (type !== 'calendar-date') return
|
|
209
328
|
open.value = false
|
|
210
|
-
|
|
329
|
+
flag(null)
|
|
330
|
+
announce(set(toPlainDate(v as CalendarDateLike)))
|
|
211
331
|
focusInput()
|
|
212
332
|
}
|
|
213
333
|
|
|
@@ -248,16 +368,17 @@ defineExpose({ focus: focusInput })
|
|
|
248
368
|
takes Nuxt UI's highlight ring, so it reads as the one being edited. -->
|
|
249
369
|
<UInput
|
|
250
370
|
ref="inputCmp"
|
|
251
|
-
|
|
371
|
+
:model-value="text"
|
|
252
372
|
type="text"
|
|
253
373
|
autocomplete="off"
|
|
254
|
-
v-bind="
|
|
374
|
+
v-bind="{ ...$attrs, ...invalidAttrs }"
|
|
255
375
|
:size="size"
|
|
256
376
|
:icon="icon"
|
|
257
377
|
:disabled="disabled"
|
|
258
378
|
:placeholder="placeholder ?? entryFormat.pattern"
|
|
259
|
-
:highlight="highlight || open"
|
|
379
|
+
:highlight="highlight || open || !!problem"
|
|
260
380
|
:ui="{ ...ui, root: `group ${ui?.root ?? ''}` }"
|
|
381
|
+
@update:model-value="onType"
|
|
261
382
|
@keydown="onKeydown"
|
|
262
383
|
@blur="onBlur"
|
|
263
384
|
>
|
|
@@ -265,6 +386,8 @@ defineExpose({ focus: focusInput })
|
|
|
265
386
|
<UIcon :name="icon" data-slot="leadingIcon" :class="inputUi.leadingIcon({ class: ui?.leadingIcon })" @click="emit('iconClick', $event)" />
|
|
266
387
|
</template>
|
|
267
388
|
<template #trailing="{ ui: inputUi }">
|
|
389
|
+
<!-- Why a typed day was refused, for screen readers: the input's aria-describedby names it. -->
|
|
390
|
+
<span v-if="messageId" :id="messageId" class="sr-only">{{ problemMessage }}</span>
|
|
268
391
|
<!-- Neither button takes focus (mousedown is prevented), so the input never blurs to them. Each icon is
|
|
269
392
|
`text-current`, over Nuxt UI's dimmed trailing icon, so the button's own color reaches it. -->
|
|
270
393
|
<button
|
|
@@ -302,6 +425,9 @@ defineExpose({ focus: focusInput })
|
|
|
302
425
|
:locale="localeCode"
|
|
303
426
|
:size="size"
|
|
304
427
|
:view-control="headingButton"
|
|
428
|
+
:min-value="calendarMin"
|
|
429
|
+
:max-value="calendarMax"
|
|
430
|
+
:is-date-unavailable="calendarUnavailable"
|
|
305
431
|
weekday-format="narrow"
|
|
306
432
|
prevent-deselect
|
|
307
433
|
class="p-2"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zooui/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Zoo UI for Nuxt: a theme-free Nuxt layer with Nuxt UI as it ships, Zoo UI's Z* components and colored-edge surfaces, and the mechanisms every product needs, all on Nuxt UI's tokens so they take the product's own colors. Add a theme layer such as @zooui/theme-forest for a ready-made look.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
package/utils/date-entry.ts
CHANGED
|
@@ -13,6 +13,12 @@
|
|
|
13
13
|
// server route, a domain package). Temporal is the polyfill's own `/implementation`, never the bare entry, which
|
|
14
14
|
// resolves to `globalThis.Temporal` where an engine has one: one class wherever this runs, the same one
|
|
15
15
|
// a database client or serializer in the product hands out.
|
|
16
|
+
//
|
|
17
|
+
// THE TWO DAY TYPES. Everything here works in PlainDate. A field may also hold an @internationalized/date
|
|
18
|
+
// CalendarDate, the type Nuxt UI's UCalendar and UInputDate use: toPlainDate and fromPlainDate convert by the fields
|
|
19
|
+
// alone (year, month, day), and only a Gregorian one is accepted, since another calendar's year and month are not the
|
|
20
|
+
// ISO ones and converting them by their fields would land on a different day.
|
|
21
|
+
import { CalendarDate } from '@internationalized/date'
|
|
16
22
|
import { Temporal } from 'temporal-polyfill/implementation'
|
|
17
23
|
|
|
18
24
|
type PlainDate = Temporal.PlainDate
|
|
@@ -209,14 +215,63 @@ export function commitDateEntry(text: string, current: PlainDate | null, today:
|
|
|
209
215
|
*
|
|
210
216
|
* Pressing m, h, y or r again keeps walking, so "hhh" is the end of the month after next. The walk stops at
|
|
211
217
|
* FIRST_ENTRY_DATE and LAST_ENTRY_DATE, so a shortcut never lands on a day the field can't read back.
|
|
218
|
+
*
|
|
219
|
+
* With `limits`, it also stops at `minValue` and `maxValue`, and skips the days `isDateUnavailable` refuses:
|
|
220
|
+
*
|
|
221
|
+
* - `m` and `y` land on the first available day of the month or year, `h` and `r` on the last; pressed there, they
|
|
222
|
+
* go on to the month or year before (m, y) or after (h, r), as they do from the 1st or the 31st.
|
|
223
|
+
* - `+` and `-` go to the next or previous available day.
|
|
224
|
+
* - `t` is today, or the nearest allowed day to it: `minValue` when today is before it, `maxValue` when today is
|
|
225
|
+
* after it, and the next available day when today is unavailable (the one before, at `maxValue`).
|
|
226
|
+
*
|
|
227
|
+
* Unavailable days are looked through for a year at most. When no allowed day is in reach it returns null, as for a
|
|
228
|
+
* key that isn't a shortcut: isDateShortcut tells the two apart.
|
|
212
229
|
*/
|
|
213
|
-
export function applyDateShortcut(
|
|
230
|
+
export function applyDateShortcut(
|
|
231
|
+
key: string,
|
|
232
|
+
current: PlainDate | null,
|
|
233
|
+
today: PlainDate,
|
|
234
|
+
format: DateEntryFormat | string = US_DATE_ENTRY,
|
|
235
|
+
limits?: DateLimits,
|
|
236
|
+
): PlainDate | null {
|
|
214
237
|
if (key === formatOf(format).separator) return null
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (
|
|
219
|
-
|
|
238
|
+
const k = key.toLowerCase()
|
|
239
|
+
const base = current ?? today
|
|
240
|
+
const target = shortcutTarget(k, base, today)
|
|
241
|
+
if (!target) return null
|
|
242
|
+
const { lo, hi } = bounds(limits)
|
|
243
|
+
if (Temporal.PlainDate.compare(lo, hi) > 0) return null
|
|
244
|
+
const unavailable = limits?.isDateUnavailable
|
|
245
|
+
const walk = WALK[k]!
|
|
246
|
+
const inward = INWARD[k]
|
|
247
|
+
const day = nearestAllowed(target, inward ?? walk, lo, hi, unavailable)
|
|
248
|
+
if (!inward || !day || Temporal.PlainDate.compare(day, base) * walk > 0) return day
|
|
249
|
+
// The first available day of the month is where m already is (or after it): the month before's, as from the 1st.
|
|
250
|
+
const again = nearestAllowed(shortcutTarget(k, target.add({ days: walk }), today)!, inward, lo, hi, unavailable)
|
|
251
|
+
return again && Temporal.PlainDate.compare(again, base) * walk > 0 ? again : day
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Which way each key moves, and for the period keys, which way to look for an available day from the period's edge.
|
|
255
|
+
const WALK: Record<string, 1 | -1> = { 't': 1, 'h': 1, 'r': 1, '+': 1, '=': 1, 'm': -1, 'y': -1, '-': -1, '_': -1 }
|
|
256
|
+
const INWARD: Record<string, 1 | -1> = { m: 1, y: 1, h: -1, r: -1 }
|
|
257
|
+
const UNAVAILABLE_SEARCH_DAYS = 366
|
|
258
|
+
|
|
259
|
+
// `target` pulled inside lo..hi, then the nearest available day from it: `step`'s way, or back into the bounds when
|
|
260
|
+
// it was pulled in. Null when there is none within the bounds and the search's year.
|
|
261
|
+
function nearestAllowed(target: PlainDate, step: 1 | -1, lo: PlainDate, hi: PlainDate, unavailable?: ((date: PlainDate) => boolean) | null) {
|
|
262
|
+
const dir = Temporal.PlainDate.compare(target, hi) > 0 ? -1 : Temporal.PlainDate.compare(target, lo) < 0 ? 1 : step
|
|
263
|
+
let day = clamp(target, lo, hi)
|
|
264
|
+
for (let i = 0; unavailable?.(day); i++) {
|
|
265
|
+
day = day.add({ days: dir })
|
|
266
|
+
if (i >= UNAVAILABLE_SEARCH_DAYS || Temporal.PlainDate.compare(day, lo) < 0 || Temporal.PlainDate.compare(day, hi) > 0) return null
|
|
267
|
+
}
|
|
268
|
+
return day
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Whether `key` is one of applyDateShortcut's keys in this format (its separator never is), so a field can take the
|
|
272
|
+
* key even when the shortcut has no allowed day to go to. */
|
|
273
|
+
export function isDateShortcut(key: string, format: DateEntryFormat | string = US_DATE_ENTRY): boolean {
|
|
274
|
+
return key !== formatOf(format).separator && Object.hasOwn(WALK, key.toLowerCase())
|
|
220
275
|
}
|
|
221
276
|
|
|
222
277
|
function shortcutTarget(key: string, base: PlainDate, today: PlainDate): PlainDate | null {
|
|
@@ -243,3 +298,122 @@ function shortcutTarget(key: string, base: PlainDate, today: PlainDate): PlainDa
|
|
|
243
298
|
return null
|
|
244
299
|
}
|
|
245
300
|
}
|
|
301
|
+
|
|
302
|
+
// ── The two day types ───────────────────────────────────────────────────────────────────────────────────────────
|
|
303
|
+
|
|
304
|
+
/** What a field holds and hands back: a Temporal.PlainDate, or an @internationalized/date CalendarDate. */
|
|
305
|
+
export type DateModelType = 'plain-date' | 'calendar-date'
|
|
306
|
+
|
|
307
|
+
/** The part of an @internationalized/date CalendarDate read here. Matched by shape, not by class, so a CalendarDate
|
|
308
|
+
* from the product's own copy of the package is read the same as one from Nuxt UI's. */
|
|
309
|
+
export interface CalendarDateLike {
|
|
310
|
+
readonly calendar: { readonly identifier: string }
|
|
311
|
+
readonly era: string
|
|
312
|
+
readonly year: number
|
|
313
|
+
readonly month: number
|
|
314
|
+
readonly day: number
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** A day in either type. */
|
|
318
|
+
export type EntryDate = PlainDate | CalendarDateLike
|
|
319
|
+
|
|
320
|
+
/** Which of the two types `value` is, or null for anything else (null included). */
|
|
321
|
+
export function dateModelTypeOf(value: unknown): DateModelType | null {
|
|
322
|
+
if (value instanceof Temporal.PlainDate) return 'plain-date'
|
|
323
|
+
if (!value || typeof value !== 'object') return null
|
|
324
|
+
const v = value as Partial<CalendarDateLike>
|
|
325
|
+
return typeof v.calendar?.identifier === 'string' && typeof v.year === 'number' && typeof v.month === 'number' && typeof v.day === 'number'
|
|
326
|
+
? 'calendar-date'
|
|
327
|
+
: null
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The day as a PlainDate: a PlainDate as it is, a Gregorian CalendarDate by its fields, null for null. A CalendarDate
|
|
332
|
+
* in any other calendar system (Japanese, Buddhist, Persian …) throws a RangeError rather than land on another day:
|
|
333
|
+
* convert it first with @internationalized/date's `toCalendar(date, new GregorianCalendar())`.
|
|
334
|
+
*/
|
|
335
|
+
export function toPlainDate(value: EntryDate | null | undefined): PlainDate | null {
|
|
336
|
+
if (value == null) return null
|
|
337
|
+
if (value instanceof Temporal.PlainDate) return value
|
|
338
|
+
if (dateModelTypeOf(value) !== 'calendar-date') throw new TypeError(`Not a day: expected a Temporal.PlainDate or an @internationalized/date CalendarDate, got ${String(value)}.`)
|
|
339
|
+
const { calendar, era, year, month, day } = value
|
|
340
|
+
if (calendar.identifier !== 'gregory') {
|
|
341
|
+
throw new RangeError(`Only a Gregorian CalendarDate can be used as a day here; this one is in the "${calendar.identifier}" calendar. Convert it with toCalendar(date, new GregorianCalendar()) from @internationalized/date.`)
|
|
342
|
+
}
|
|
343
|
+
// A Gregorian year counts within its era: 1 BC is the ISO year 0.
|
|
344
|
+
return Temporal.PlainDate.from({ year: era === 'BC' ? 1 - year : year, month, day }, { overflow: 'reject' })
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
type CalendarDateClass = new (year: number, month: number, day: number) => CalendarDateLike
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The day in the type asked for: the PlainDate itself, or a Gregorian CalendarDate with its fields. `like`, a
|
|
351
|
+
* CalendarDate the caller already holds, names the class to build with, so the caller's own copy of
|
|
352
|
+
* @internationalized/date gets its own class back; without one it is the layer's.
|
|
353
|
+
*/
|
|
354
|
+
export function fromPlainDate(d: PlainDate, type: 'plain-date', like?: CalendarDateLike | null): PlainDate
|
|
355
|
+
export function fromPlainDate(d: PlainDate, type: 'calendar-date', like?: CalendarDateLike | null): CalendarDate
|
|
356
|
+
export function fromPlainDate(d: PlainDate, type: DateModelType, like?: CalendarDateLike | null): EntryDate
|
|
357
|
+
export function fromPlainDate(d: PlainDate | null, type: DateModelType, like?: CalendarDateLike | null): EntryDate | null
|
|
358
|
+
export function fromPlainDate(d: PlainDate | null, type: DateModelType, like?: CalendarDateLike | null): EntryDate | null {
|
|
359
|
+
if (!d || type === 'plain-date') return d
|
|
360
|
+
const Class = (like?.constructor ?? CalendarDate) as CalendarDateClass
|
|
361
|
+
return new Class(d.year, d.month, d.day)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// ── Limits ──────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
365
|
+
|
|
366
|
+
/** The days a field takes, as Nuxt UI's UCalendar names them: the first and the last, and the days in between it
|
|
367
|
+
* refuses. `minValue` and `maxValue` may be either day type. */
|
|
368
|
+
export interface DateLimits {
|
|
369
|
+
minValue?: EntryDate | null
|
|
370
|
+
maxValue?: EntryDate | null
|
|
371
|
+
isDateUnavailable?: ((date: PlainDate) => boolean) | null
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Why the limits refuse a day. */
|
|
375
|
+
export type DateLimitReason = 'before-min' | 'after-max' | 'unavailable'
|
|
376
|
+
|
|
377
|
+
/** A day the limits refuse: why, the day, and the limit it crossed (`minValue` or `maxValue`; null when unavailable).
|
|
378
|
+
* A PlainDate here; ZDateField's `invalid` hands it back in the model's own type. */
|
|
379
|
+
export interface DateLimitProblem<D = PlainDate> {
|
|
380
|
+
readonly reason: DateLimitReason
|
|
381
|
+
readonly date: D
|
|
382
|
+
readonly limit: D | null
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const earlier = (a: PlainDate, b: PlainDate) => (Temporal.PlainDate.compare(a, b) <= 0 ? a : b)
|
|
386
|
+
const later = (a: PlainDate, b: PlainDate) => (Temporal.PlainDate.compare(a, b) >= 0 ? a : b)
|
|
387
|
+
const clamp = (d: PlainDate, lo: PlainDate, hi: PlainDate) => later(lo, earlier(d, hi))
|
|
388
|
+
|
|
389
|
+
// The first and last days a shortcut may land on: the limits', within the days a field can show and read back.
|
|
390
|
+
function bounds(limits?: DateLimits) {
|
|
391
|
+
const min = toPlainDate(limits?.minValue)
|
|
392
|
+
const max = toPlainDate(limits?.maxValue)
|
|
393
|
+
return { lo: min ? later(min, FIRST_ENTRY_DATE) : FIRST_ENTRY_DATE, hi: max ? earlier(max, LAST_ENTRY_DATE) : LAST_ENTRY_DATE }
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Why the limits refuse `date`, or null when they take it. Checked in that order: before `minValue`, after
|
|
397
|
+
* `maxValue`, then `isDateUnavailable`. */
|
|
398
|
+
export function checkDateLimits(date: PlainDate, limits?: DateLimits): DateLimitProblem | null {
|
|
399
|
+
const min = toPlainDate(limits?.minValue)
|
|
400
|
+
if (min && Temporal.PlainDate.compare(date, min) < 0) return { reason: 'before-min', date, limit: min }
|
|
401
|
+
const max = toPlainDate(limits?.maxValue)
|
|
402
|
+
if (max && Temporal.PlainDate.compare(date, max) > 0) return { reason: 'after-max', date, limit: max }
|
|
403
|
+
if (limits?.isDateUnavailable?.(date)) return { reason: 'unavailable', date, limit: null }
|
|
404
|
+
return null
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** An English sentence for a refused day, its dates in the field's format: "Choose a date on or after 01/01/2026.",
|
|
408
|
+
* "Choose a date on or before 12/31/2026.", "12/25/2026 isn't available." A product in another language builds its
|
|
409
|
+
* own from the problem's `reason`, `date` and `limit`. */
|
|
410
|
+
export function dateLimitMessage(problem: DateLimitProblem, format: DateEntryFormat | string = US_DATE_ENTRY): string {
|
|
411
|
+
switch (problem.reason) {
|
|
412
|
+
case 'before-min':
|
|
413
|
+
return `Choose a date on or after ${formatDateEntry(problem.limit, format)}.`
|
|
414
|
+
case 'after-max':
|
|
415
|
+
return `Choose a date on or before ${formatDateEntry(problem.limit, format)}.`
|
|
416
|
+
case 'unavailable':
|
|
417
|
+
return `${formatDateEntry(problem.date, format)} isn't available.`
|
|
418
|
+
}
|
|
419
|
+
}
|