socle-ui 0.0.0-stage → 0.4.2
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/CHANGELOG.md +167 -0
- package/LICENSE +21 -0
- package/README.md +255 -2
- package/SocleDatePicker.vue +659 -0
- package/SocleDialog.vue +250 -0
- package/SocleField.vue +72 -0
- package/SocleInput.vue +164 -0
- package/SoclePopover.vue +171 -0
- package/SocleSelect.vue +331 -0
- package/SocleTableHead.vue +336 -0
- package/SocleTabs.vue +226 -0
- package/classes.ts +18 -0
- package/index.ts +13 -0
- package/package.json +51 -4
- package/socle.css +356 -0
- package/table.ts +120 -0
- package/tailwind.css +190 -0
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SocleDatePicker.vue
|
|
3
|
+
Sélecteur de date : bouton déclencheur + calendrier en popover ancré (CSS Anchor Positioning).
|
|
4
|
+
Prérequis : Vue >= 3.5 (useId, defineModel), Tailwind CSS v4 avec `@import "socle-ui/tailwind.css"` (utilitaire socle-field-box).
|
|
5
|
+
|
|
6
|
+
<SocleDatePicker
|
|
7
|
+
v-model="date"
|
|
8
|
+
label="Date de livraison"
|
|
9
|
+
name="delivery_date"
|
|
10
|
+
min="2026-11-24"
|
|
11
|
+
max="2027-03-31"
|
|
12
|
+
hint="Livraison du lundi au samedi."
|
|
13
|
+
:error="errors.date"
|
|
14
|
+
required
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
- v-model : chaîne ISO "YYYY-MM-DD" (ou null), en heure locale, sans fuseau horaire.
|
|
18
|
+
- Années : boutons « » (±1 an) et clic sur le titre pour ouvrir une grille d'années
|
|
19
|
+
(pages de 12), pratique pour une date de naissance ou une échéance lointaine.
|
|
20
|
+
- Clavier : flèches, Début/Fin (semaine), PageHaut/PageBas (mois), Maj+PageHaut/Bas (année),
|
|
21
|
+
Entrée/Espace pour choisir, Échap pour fermer (ou revenir aux jours).
|
|
22
|
+
- Erreur : comme SocleInput, la prop `error` colore le champ, affiche le message (annoncé poliment)
|
|
23
|
+
et pose aria-invalid ; `hint` est affiché tant qu'il n'y a pas d'erreur.
|
|
24
|
+
- required : astérisque sur le label + aria-required. La validation reste à faire côté appelant
|
|
25
|
+
(un <input type="hidden"> n'est jamais validé par le navigateur).
|
|
26
|
+
-->
|
|
27
|
+
<script setup lang="ts">
|
|
28
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, useId, watch } from 'vue'
|
|
29
|
+
import SocleField from './SocleField.vue'
|
|
30
|
+
import './socle.css'
|
|
31
|
+
|
|
32
|
+
interface Labels {
|
|
33
|
+
previousMonth: string
|
|
34
|
+
nextMonth: string
|
|
35
|
+
today: string
|
|
36
|
+
clear: string
|
|
37
|
+
calendar: string
|
|
38
|
+
previousYear: string
|
|
39
|
+
nextYear: string
|
|
40
|
+
previousYears: string
|
|
41
|
+
nextYears: string
|
|
42
|
+
chooseYear: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const defaultLabels: Labels = {
|
|
46
|
+
previousMonth: 'Mois précédent',
|
|
47
|
+
nextMonth: 'Mois suivant',
|
|
48
|
+
today: "Aujourd'hui",
|
|
49
|
+
clear: 'Effacer',
|
|
50
|
+
calendar: 'Calendrier',
|
|
51
|
+
previousYear: 'Année précédente',
|
|
52
|
+
nextYear: 'Année suivante',
|
|
53
|
+
previousYears: '12 années précédentes',
|
|
54
|
+
nextYears: '12 années suivantes',
|
|
55
|
+
chooseYear: 'Choisir une année',
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const props = withDefaults(
|
|
59
|
+
defineProps<{
|
|
60
|
+
label?: string
|
|
61
|
+
placeholder?: string
|
|
62
|
+
/** Si défini, un <input type="hidden"> (valeur ISO) est rendu pour les formulaires classiques. */
|
|
63
|
+
name?: string
|
|
64
|
+
disabled?: boolean
|
|
65
|
+
/** Nom accessible quand il n'y a pas de `label` visible (sinon le libellé « Calendrier » est utilisé). */
|
|
66
|
+
ariaLabel?: string
|
|
67
|
+
/** Texte d'aide sous le champ (remplacé par `error` si présent). */
|
|
68
|
+
hint?: string
|
|
69
|
+
/** Message d'erreur : met le champ en erreur. */
|
|
70
|
+
error?: string
|
|
71
|
+
required?: boolean
|
|
72
|
+
/** Dates limites incluses, au format "YYYY-MM-DD". */
|
|
73
|
+
min?: string
|
|
74
|
+
max?: string
|
|
75
|
+
locale?: string
|
|
76
|
+
/** 0 = dimanche, 1 = lundi… */
|
|
77
|
+
firstDayOfWeek?: 0 | 1 | 2 | 3 | 4 | 5 | 6
|
|
78
|
+
labels?: Partial<Labels>
|
|
79
|
+
}>(),
|
|
80
|
+
{ placeholder: 'Choisir une date', locale: 'fr-FR', firstDayOfWeek: 1 },
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const model = defineModel<string | null>({ default: null })
|
|
84
|
+
|
|
85
|
+
const L = computed<Labels>(() => ({ ...defaultLabels, ...props.labels }))
|
|
86
|
+
|
|
87
|
+
/* ---------- Utilitaires de dates (heure locale, sans fuseau) ---------- */
|
|
88
|
+
const pad = (n: number) => String(n).padStart(2, '0')
|
|
89
|
+
const toISO = (d: Date) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
|
90
|
+
|
|
91
|
+
function fromISO(s?: string | null): Date | null {
|
|
92
|
+
const m = s ? /^(\d{4})-(\d{2})-(\d{2})$/.exec(s) : null
|
|
93
|
+
if (!m) return null
|
|
94
|
+
const d = new Date(+m[1]!, +m[2]! - 1, +m[3]!)
|
|
95
|
+
return d.getMonth() === +m[2]! - 1 ? d : null // rejette 2026-02-31
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const startOfDay = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate())
|
|
99
|
+
const addDays = (d: Date, n: number) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n)
|
|
100
|
+
|
|
101
|
+
function addMonths(d: Date, n: number): Date {
|
|
102
|
+
const t = new Date(d.getFullYear(), d.getMonth() + n, 1)
|
|
103
|
+
const lastDay = new Date(t.getFullYear(), t.getMonth() + 1, 0).getDate()
|
|
104
|
+
t.setDate(Math.min(d.getDate(), lastDay)) // 31 janvier + 1 mois → 28/29 février
|
|
105
|
+
return t
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const minDate = computed(() => fromISO(props.min))
|
|
109
|
+
const maxDate = computed(() => fromISO(props.max))
|
|
110
|
+
const isDisabled = (d: Date) =>
|
|
111
|
+
(!!minDate.value && d < minDate.value) || (!!maxDate.value && d > maxDate.value)
|
|
112
|
+
|
|
113
|
+
function clamp(d: Date): Date {
|
|
114
|
+
if (minDate.value && d < minDate.value) return minDate.value
|
|
115
|
+
if (maxDate.value && d > maxDate.value) return maxDate.value
|
|
116
|
+
return d
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* ---------- Formatage (Intl) ---------- */
|
|
120
|
+
const fmtTrigger = computed(() => new Intl.DateTimeFormat(props.locale, { dateStyle: 'long' }))
|
|
121
|
+
const fmtMonth = computed(() => new Intl.DateTimeFormat(props.locale, { month: 'long', year: 'numeric' }))
|
|
122
|
+
const fmtFull = computed(() => new Intl.DateTimeFormat(props.locale, { dateStyle: 'full' }))
|
|
123
|
+
|
|
124
|
+
const weekdays = computed(() => {
|
|
125
|
+
const narrow = new Intl.DateTimeFormat(props.locale, { weekday: 'narrow' })
|
|
126
|
+
const long = new Intl.DateTimeFormat(props.locale, { weekday: 'long' })
|
|
127
|
+
// Le 7 janvier 2024 est un dimanche : sert de référence.
|
|
128
|
+
return Array.from({ length: 7 }, (_, i) => {
|
|
129
|
+
const d = new Date(2024, 0, 7 + ((props.firstDayOfWeek + i) % 7))
|
|
130
|
+
return { short: narrow.format(d), long: long.format(d) }
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
/* ---------- Identifiants ---------- */
|
|
135
|
+
const uid = useId()
|
|
136
|
+
const triggerId = `${uid}-trigger`
|
|
137
|
+
const popoverId = `${uid}-popover`
|
|
138
|
+
const monthId = `${uid}-month`
|
|
139
|
+
// Un nom d'ancre unique par instance (dashed-ident) pour pouvoir en afficher plusieurs.
|
|
140
|
+
const anchorName = `--datepicker-${uid.replace(/[^a-zA-Z0-9_-]/g, '')}`
|
|
141
|
+
|
|
142
|
+
/* ---------- État ---------- */
|
|
143
|
+
const field = ref<InstanceType<typeof SocleField> | null>(null)
|
|
144
|
+
const root = computed(() => field.value?.root ?? null)
|
|
145
|
+
const trigger = ref<HTMLButtonElement | null>(null)
|
|
146
|
+
const popover = ref<HTMLElement | null>(null)
|
|
147
|
+
const grid = ref<HTMLElement | null>(null)
|
|
148
|
+
const yearsEl = ref<HTMLElement | null>(null)
|
|
149
|
+
|
|
150
|
+
const open = ref(false)
|
|
151
|
+
/** Jour "actif" (tabindex=0) ; le mois affiché est toujours celui du curseur. */
|
|
152
|
+
const cursor = ref(startOfDay(new Date()))
|
|
153
|
+
const todayISO = ref(toISO(new Date()))
|
|
154
|
+
|
|
155
|
+
/** Vue affichée : grille des jours, ou grille des années (pages de 12). */
|
|
156
|
+
const view = ref<'days' | 'years'>('days')
|
|
157
|
+
/** Année "active" (tabindex=0) dans la vue des années. */
|
|
158
|
+
const yearCursor = ref(new Date().getFullYear())
|
|
159
|
+
|
|
160
|
+
const selectedDate = computed(() => fromISO(model.value))
|
|
161
|
+
const triggerText = computed(() => (selectedDate.value ? fmtTrigger.value.format(selectedDate.value) : ''))
|
|
162
|
+
|
|
163
|
+
const weeks = computed(() => {
|
|
164
|
+
const y = cursor.value.getFullYear()
|
|
165
|
+
const m = cursor.value.getMonth()
|
|
166
|
+
const offset = (new Date(y, m, 1).getDay() - props.firstDayOfWeek + 7) % 7
|
|
167
|
+
// Toujours 6 semaines : la hauteur du calendrier reste stable d'un mois à l'autre.
|
|
168
|
+
return Array.from({ length: 6 }, (_, w) =>
|
|
169
|
+
Array.from({ length: 7 }, (_, d) => {
|
|
170
|
+
const date = new Date(y, m, 1 - offset + w * 7 + d)
|
|
171
|
+
const iso = toISO(date)
|
|
172
|
+
return {
|
|
173
|
+
date,
|
|
174
|
+
iso,
|
|
175
|
+
day: date.getDate(),
|
|
176
|
+
inMonth: date.getMonth() === m,
|
|
177
|
+
disabled: isDisabled(date),
|
|
178
|
+
today: iso === todayISO.value,
|
|
179
|
+
selected: iso === model.value,
|
|
180
|
+
label: fmtFull.value.format(date),
|
|
181
|
+
}
|
|
182
|
+
}),
|
|
183
|
+
)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
type Cell = (typeof weeks.value)[number][number]
|
|
187
|
+
|
|
188
|
+
const isYearDisabled = (y: number) =>
|
|
189
|
+
(!!minDate.value && y < minDate.value.getFullYear()) || (!!maxDate.value && y > maxDate.value.getFullYear())
|
|
190
|
+
|
|
191
|
+
const clampYear = (y: number) =>
|
|
192
|
+
Math.min(
|
|
193
|
+
Math.max(y, minDate.value?.getFullYear() ?? -Infinity),
|
|
194
|
+
maxDate.value?.getFullYear() ?? Infinity,
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
/** Première année de la page de 12 années affichée. */
|
|
198
|
+
const pageStart = computed(() => Math.floor(yearCursor.value / 12) * 12)
|
|
199
|
+
|
|
200
|
+
const years = computed(() => {
|
|
201
|
+
const selectedYear = selectedDate.value?.getFullYear()
|
|
202
|
+
const thisYear = new Date().getFullYear()
|
|
203
|
+
return Array.from({ length: 12 }, (_, i) => {
|
|
204
|
+
const year = pageStart.value + i
|
|
205
|
+
return {
|
|
206
|
+
year,
|
|
207
|
+
disabled: isYearDisabled(year),
|
|
208
|
+
selected: year === selectedYear,
|
|
209
|
+
today: year === thisYear,
|
|
210
|
+
current: year === cursor.value.getFullYear(),
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
type YearCell = (typeof years.value)[number]
|
|
216
|
+
|
|
217
|
+
// Précédent / suivant : un mois en vue des jours, 12 ans en vue des années.
|
|
218
|
+
const prevDisabled = computed(() => {
|
|
219
|
+
if (!minDate.value) return false
|
|
220
|
+
return view.value === 'years'
|
|
221
|
+
? pageStart.value <= minDate.value.getFullYear()
|
|
222
|
+
: new Date(cursor.value.getFullYear(), cursor.value.getMonth(), 0) < minDate.value
|
|
223
|
+
})
|
|
224
|
+
const nextDisabled = computed(() => {
|
|
225
|
+
if (!maxDate.value) return false
|
|
226
|
+
return view.value === 'years'
|
|
227
|
+
? pageStart.value + 11 >= maxDate.value.getFullYear()
|
|
228
|
+
: new Date(cursor.value.getFullYear(), cursor.value.getMonth() + 1, 1) > maxDate.value
|
|
229
|
+
})
|
|
230
|
+
// ±1 an (vue des jours uniquement)
|
|
231
|
+
const prevYearDisabled = computed(
|
|
232
|
+
() => !!minDate.value && cursor.value.getFullYear() <= minDate.value.getFullYear(),
|
|
233
|
+
)
|
|
234
|
+
const nextYearDisabled = computed(
|
|
235
|
+
() => !!maxDate.value && cursor.value.getFullYear() >= maxDate.value.getFullYear(),
|
|
236
|
+
)
|
|
237
|
+
const todayDisabled = computed(() => isDisabled(startOfDay(new Date())))
|
|
238
|
+
|
|
239
|
+
/* ---------- Classes (exclusives : pas de conflit d'ordre CSS Tailwind) ---------- */
|
|
240
|
+
const navBtn =
|
|
241
|
+
'flex size-8 shrink-0 items-center justify-center rounded-socle-sm text-socle-text-muted outline-none hover:bg-socle-surface-hover focus-visible:ring-2 focus-visible:ring-socle-accent/50 disabled:cursor-not-allowed disabled:opacity-30 disabled:hover:bg-transparent'
|
|
242
|
+
|
|
243
|
+
function yearClasses(y: YearCell) {
|
|
244
|
+
return [
|
|
245
|
+
y.selected
|
|
246
|
+
? 'bg-socle-accent font-medium text-socle-on-accent'
|
|
247
|
+
: [
|
|
248
|
+
y.current ? 'bg-socle-surface-hover' : '',
|
|
249
|
+
y.today ? 'font-semibold text-socle-accent-text' : 'text-socle-text',
|
|
250
|
+
y.disabled ? '' : 'hover:bg-socle-surface-hover',
|
|
251
|
+
],
|
|
252
|
+
y.disabled ? 'cursor-not-allowed opacity-30' : '',
|
|
253
|
+
]
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function dayClasses(c: Cell) {
|
|
257
|
+
return [
|
|
258
|
+
c.selected
|
|
259
|
+
? 'bg-socle-accent font-medium text-socle-on-accent'
|
|
260
|
+
: [
|
|
261
|
+
c.inMonth ? 'text-socle-text' : 'text-socle-placeholder',
|
|
262
|
+
c.today ? 'font-semibold text-socle-accent-text' : '',
|
|
263
|
+
c.disabled ? '' : 'hover:bg-socle-surface-hover',
|
|
264
|
+
],
|
|
265
|
+
c.disabled ? 'cursor-not-allowed opacity-30' : '',
|
|
266
|
+
c.today ? 'after:absolute after:bottom-1 after:size-1 after:rounded-full after:bg-current' : '',
|
|
267
|
+
]
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* ---------- Actions ---------- */
|
|
271
|
+
function focusDay() {
|
|
272
|
+
grid.value?.querySelector<HTMLElement>(`[data-iso="${toISO(cursor.value)}"]`)?.focus()
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function setCursor(d: Date, focus = true) {
|
|
276
|
+
cursor.value = clamp(d)
|
|
277
|
+
if (focus) {
|
|
278
|
+
await nextTick()
|
|
279
|
+
focusDay()
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function show() {
|
|
284
|
+
if (props.disabled || open.value) return
|
|
285
|
+
todayISO.value = toISO(new Date())
|
|
286
|
+
view.value = 'days'
|
|
287
|
+
cursor.value = clamp(selectedDate.value ?? startOfDay(new Date()))
|
|
288
|
+
open.value = true
|
|
289
|
+
popover.value?.showPopover()
|
|
290
|
+
await nextTick()
|
|
291
|
+
focusDay()
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function hide(restoreFocus = false) {
|
|
295
|
+
if (!open.value) return
|
|
296
|
+
popover.value?.hidePopover()
|
|
297
|
+
open.value = false
|
|
298
|
+
if (restoreFocus) trigger.value?.focus()
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function toggle() {
|
|
302
|
+
open.value ? hide() : show()
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function select(c: Cell) {
|
|
306
|
+
if (c.disabled) return
|
|
307
|
+
model.value = c.iso
|
|
308
|
+
hide(true)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function selectToday() {
|
|
312
|
+
if (todayDisabled.value) return
|
|
313
|
+
model.value = toISO(new Date())
|
|
314
|
+
hide(true)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function clear() {
|
|
318
|
+
model.value = null
|
|
319
|
+
hide(true)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* ---------- Années ---------- */
|
|
323
|
+
async function setYearCursor(y: number, focus = true) {
|
|
324
|
+
yearCursor.value = clampYear(y)
|
|
325
|
+
if (focus) {
|
|
326
|
+
await nextTick()
|
|
327
|
+
yearsEl.value?.querySelector<HTMLElement>(`[data-year="${yearCursor.value}"]`)?.focus()
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function showYears() {
|
|
332
|
+
view.value = 'years'
|
|
333
|
+
await setYearCursor(cursor.value.getFullYear())
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function showDays() {
|
|
337
|
+
view.value = 'days'
|
|
338
|
+
await nextTick()
|
|
339
|
+
focusDay()
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function toggleView() {
|
|
343
|
+
view.value === 'days' ? showYears() : showDays()
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function prev() {
|
|
347
|
+
view.value === 'days' ? setCursor(addMonths(cursor.value, -1), false) : setYearCursor(yearCursor.value - 12, false)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function next() {
|
|
351
|
+
view.value === 'days' ? setCursor(addMonths(cursor.value, 1), false) : setYearCursor(yearCursor.value + 12, false)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function chooseYear(y: number) {
|
|
355
|
+
if (isYearDisabled(y)) return
|
|
356
|
+
// Garde le mois et le jour (31 janvier → 28/29 février si besoin)
|
|
357
|
+
cursor.value = clamp(addMonths(cursor.value, (y - cursor.value.getFullYear()) * 12))
|
|
358
|
+
showDays()
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/* ---------- Clavier ---------- */
|
|
362
|
+
function onYearsKeydown(e: KeyboardEvent) {
|
|
363
|
+
const y = yearCursor.value
|
|
364
|
+
let target: number
|
|
365
|
+
|
|
366
|
+
switch (e.key) {
|
|
367
|
+
case 'ArrowLeft': target = y - 1; break
|
|
368
|
+
case 'ArrowRight': target = y + 1; break
|
|
369
|
+
case 'ArrowUp': target = y - 4; break
|
|
370
|
+
case 'ArrowDown': target = y + 4; break
|
|
371
|
+
case 'Home': target = pageStart.value; break
|
|
372
|
+
case 'End': target = pageStart.value + 11; break
|
|
373
|
+
case 'PageUp': target = y - 12; break
|
|
374
|
+
case 'PageDown': target = y + 12; break
|
|
375
|
+
default: return
|
|
376
|
+
}
|
|
377
|
+
e.preventDefault()
|
|
378
|
+
setYearCursor(target)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function onGridKeydown(e: KeyboardEvent) {
|
|
382
|
+
const c = cursor.value
|
|
383
|
+
const weekStart = addDays(c, -((c.getDay() - props.firstDayOfWeek + 7) % 7))
|
|
384
|
+
let next: Date | null = null
|
|
385
|
+
|
|
386
|
+
switch (e.key) {
|
|
387
|
+
case 'ArrowLeft': next = addDays(c, -1); break
|
|
388
|
+
case 'ArrowRight': next = addDays(c, 1); break
|
|
389
|
+
case 'ArrowUp': next = addDays(c, -7); break
|
|
390
|
+
case 'ArrowDown': next = addDays(c, 7); break
|
|
391
|
+
case 'Home': next = weekStart; break
|
|
392
|
+
case 'End': next = addDays(weekStart, 6); break
|
|
393
|
+
case 'PageUp': next = addMonths(c, e.shiftKey ? -12 : -1); break
|
|
394
|
+
case 'PageDown': next = addMonths(c, e.shiftKey ? 12 : 1); break
|
|
395
|
+
default: return
|
|
396
|
+
}
|
|
397
|
+
e.preventDefault()
|
|
398
|
+
setCursor(next)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function onTriggerKeydown(e: KeyboardEvent) {
|
|
402
|
+
if (e.key === 'ArrowDown') {
|
|
403
|
+
e.preventDefault()
|
|
404
|
+
show()
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function onEscape(e: KeyboardEvent) {
|
|
409
|
+
if (!open.value) return
|
|
410
|
+
e.preventDefault()
|
|
411
|
+
if (view.value === 'years') showDays()
|
|
412
|
+
else hide(true)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/* ---------- Fermeture ---------- */
|
|
416
|
+
function onFocusOut(e: FocusEvent) {
|
|
417
|
+
const next = e.relatedTarget as Node | null
|
|
418
|
+
if (open.value && next && !root.value?.contains(next)) hide()
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function onDocumentPointerDown(e: PointerEvent) {
|
|
422
|
+
if (open.value && !root.value?.contains(e.target as Node)) hide()
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** Comme pour SocleSelect, un clic sur le label donne le focus sans ouvrir le calendrier
|
|
426
|
+
* (par défaut, cliquer le label d'un <button> déclencherait un clic dessus). */
|
|
427
|
+
function onLabelClick(e: MouseEvent) {
|
|
428
|
+
e.preventDefault()
|
|
429
|
+
trigger.value?.focus()
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
watch(
|
|
433
|
+
() => props.disabled,
|
|
434
|
+
(d) => {
|
|
435
|
+
if (d) hide()
|
|
436
|
+
},
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
/** Garde `open` synchronisé si le navigateur ferme le popover de lui-même. */
|
|
440
|
+
function onToggle(e: Event) {
|
|
441
|
+
open.value = (e as ToggleEvent).newState === 'open'
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
onMounted(() => document.addEventListener('pointerdown', onDocumentPointerDown))
|
|
445
|
+
onBeforeUnmount(() => document.removeEventListener('pointerdown', onDocumentPointerDown))
|
|
446
|
+
</script>
|
|
447
|
+
|
|
448
|
+
<template>
|
|
449
|
+
<SocleField
|
|
450
|
+
ref="field"
|
|
451
|
+
v-slot="{ labelId, describedBy }"
|
|
452
|
+
:control-id="triggerId"
|
|
453
|
+
:label="label"
|
|
454
|
+
:required="required"
|
|
455
|
+
:hint="hint"
|
|
456
|
+
:error="error"
|
|
457
|
+
:style="{ '--socle-anchor': anchorName }"
|
|
458
|
+
@keydown.esc="onEscape"
|
|
459
|
+
@focusout="onFocusOut"
|
|
460
|
+
@label-click="onLabelClick"
|
|
461
|
+
>
|
|
462
|
+
|
|
463
|
+
<!-- Le bouton est l'ancre -->
|
|
464
|
+
<button
|
|
465
|
+
:id="triggerId"
|
|
466
|
+
ref="trigger"
|
|
467
|
+
type="button"
|
|
468
|
+
aria-haspopup="dialog"
|
|
469
|
+
:aria-expanded="open"
|
|
470
|
+
:aria-controls="popoverId"
|
|
471
|
+
:aria-label="label ? undefined : [ariaLabel ?? L.calendar, triggerText].filter(Boolean).join(', ')"
|
|
472
|
+
:aria-invalid="error ? true : undefined"
|
|
473
|
+
:aria-required="required || undefined"
|
|
474
|
+
:aria-describedby="describedBy"
|
|
475
|
+
:disabled="disabled"
|
|
476
|
+
class="socle-datepicker-trigger socle-field-box flex w-full items-center justify-between gap-2 bg-socle-surface py-2 text-left outline-none disabled:cursor-not-allowed disabled:opacity-50"
|
|
477
|
+
@click="toggle"
|
|
478
|
+
@keydown="onTriggerKeydown"
|
|
479
|
+
>
|
|
480
|
+
<span class="truncate" :class="{ 'text-socle-placeholder': !triggerText }">
|
|
481
|
+
{{ triggerText || placeholder }}
|
|
482
|
+
</span>
|
|
483
|
+
<svg
|
|
484
|
+
class="size-4 shrink-0 text-socle-text-subtle"
|
|
485
|
+
viewBox="0 0 20 20"
|
|
486
|
+
fill="none"
|
|
487
|
+
stroke="currentColor"
|
|
488
|
+
stroke-width="1.75"
|
|
489
|
+
stroke-linecap="round"
|
|
490
|
+
stroke-linejoin="round"
|
|
491
|
+
aria-hidden="true"
|
|
492
|
+
>
|
|
493
|
+
<rect x="3" y="4.5" width="14" height="12.5" rx="2" />
|
|
494
|
+
<path d="M3 8.5h14M7 2.5v3M13 2.5v3" />
|
|
495
|
+
</svg>
|
|
496
|
+
</button>
|
|
497
|
+
|
|
498
|
+
<!-- Le calendrier est un popover (top layer) ancré au bouton -->
|
|
499
|
+
<div
|
|
500
|
+
:id="popoverId"
|
|
501
|
+
ref="popover"
|
|
502
|
+
popover="manual"
|
|
503
|
+
role="dialog"
|
|
504
|
+
:aria-labelledby="label ? labelId : undefined"
|
|
505
|
+
:aria-label="label ? undefined : (ariaLabel ?? L.calendar)"
|
|
506
|
+
class="socle-datepicker-popover rounded-socle-lg border border-socle-border-subtle bg-socle-surface p-3 text-socle-text shadow-lg"
|
|
507
|
+
@toggle="onToggle"
|
|
508
|
+
>
|
|
509
|
+
<!-- En-tête : ‹‹ année · ‹ mois · [titre cliquable → grille d'années] · › · ›› -->
|
|
510
|
+
<div class="mb-2 flex items-center justify-between gap-0.5">
|
|
511
|
+
<div class="flex">
|
|
512
|
+
<button
|
|
513
|
+
v-if="view === 'days'"
|
|
514
|
+
type="button"
|
|
515
|
+
:aria-label="L.previousYear"
|
|
516
|
+
:disabled="prevYearDisabled"
|
|
517
|
+
:class="navBtn"
|
|
518
|
+
@click="setCursor(addMonths(cursor, -12), false)"
|
|
519
|
+
>
|
|
520
|
+
<svg class="size-4" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
521
|
+
<path d="m10 5-5 5 5 5M15.5 5l-5 5 5 5" />
|
|
522
|
+
</svg>
|
|
523
|
+
</button>
|
|
524
|
+
<button
|
|
525
|
+
type="button"
|
|
526
|
+
:aria-label="view === 'days' ? L.previousMonth : L.previousYears"
|
|
527
|
+
:disabled="prevDisabled"
|
|
528
|
+
:class="navBtn"
|
|
529
|
+
@click="prev"
|
|
530
|
+
>
|
|
531
|
+
<svg class="size-4" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
532
|
+
<path d="m12.5 5-5 5 5 5" />
|
|
533
|
+
</svg>
|
|
534
|
+
</button>
|
|
535
|
+
</div>
|
|
536
|
+
|
|
537
|
+
<button
|
|
538
|
+
:id="monthId"
|
|
539
|
+
type="button"
|
|
540
|
+
:title="L.chooseYear"
|
|
541
|
+
:aria-expanded="view === 'years'"
|
|
542
|
+
aria-live="polite"
|
|
543
|
+
class="min-w-0 flex-1 truncate rounded-socle-sm px-2 py-1 text-sm font-medium capitalize outline-none hover:bg-socle-surface-hover focus-visible:ring-2 focus-visible:ring-socle-accent/50"
|
|
544
|
+
@click="toggleView"
|
|
545
|
+
>
|
|
546
|
+
{{ view === 'days' ? fmtMonth.format(cursor) : `${pageStart} – ${pageStart + 11}` }}
|
|
547
|
+
</button>
|
|
548
|
+
|
|
549
|
+
<div class="flex">
|
|
550
|
+
<button
|
|
551
|
+
type="button"
|
|
552
|
+
:aria-label="view === 'days' ? L.nextMonth : L.nextYears"
|
|
553
|
+
:disabled="nextDisabled"
|
|
554
|
+
:class="navBtn"
|
|
555
|
+
@click="next"
|
|
556
|
+
>
|
|
557
|
+
<svg class="size-4" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
558
|
+
<path d="m7.5 5 5 5-5 5" />
|
|
559
|
+
</svg>
|
|
560
|
+
</button>
|
|
561
|
+
<button
|
|
562
|
+
v-if="view === 'days'"
|
|
563
|
+
type="button"
|
|
564
|
+
:aria-label="L.nextYear"
|
|
565
|
+
:disabled="nextYearDisabled"
|
|
566
|
+
:class="navBtn"
|
|
567
|
+
@click="setCursor(addMonths(cursor, 12), false)"
|
|
568
|
+
>
|
|
569
|
+
<svg class="size-4" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
570
|
+
<path d="m10 5 5 5-5 5M4.5 5l5 5-5 5" />
|
|
571
|
+
</svg>
|
|
572
|
+
</button>
|
|
573
|
+
</div>
|
|
574
|
+
</div>
|
|
575
|
+
|
|
576
|
+
<!-- Vue des jours (tabindex roulant : un seul jour est tabulable) -->
|
|
577
|
+
<div v-if="view === 'days'" ref="grid" role="grid" :aria-labelledby="monthId" @keydown="onGridKeydown">
|
|
578
|
+
<div role="row" class="grid grid-cols-7">
|
|
579
|
+
<div
|
|
580
|
+
v-for="w in weekdays"
|
|
581
|
+
:key="w.long"
|
|
582
|
+
role="columnheader"
|
|
583
|
+
:aria-label="w.long"
|
|
584
|
+
class="flex h-8 items-center justify-center text-xs font-medium text-socle-text-subtle"
|
|
585
|
+
>
|
|
586
|
+
{{ w.short }}
|
|
587
|
+
</div>
|
|
588
|
+
</div>
|
|
589
|
+
|
|
590
|
+
<div v-for="(week, wi) in weeks" :key="wi" role="row" class="grid grid-cols-7">
|
|
591
|
+
<div v-for="cell in week" :key="cell.iso" role="gridcell" :aria-selected="cell.selected" class="flex justify-center">
|
|
592
|
+
<button
|
|
593
|
+
type="button"
|
|
594
|
+
:data-iso="cell.iso"
|
|
595
|
+
:tabindex="cell.iso === toISO(cursor) ? 0 : -1"
|
|
596
|
+
:aria-label="cell.label"
|
|
597
|
+
:aria-current="cell.today ? 'date' : undefined"
|
|
598
|
+
:disabled="cell.disabled"
|
|
599
|
+
class="relative flex size-9 items-center justify-center rounded-socle-sm text-sm tabular-nums outline-none transition-colors focus-visible:ring-2 focus-visible:ring-socle-accent/50"
|
|
600
|
+
:class="dayClasses(cell)"
|
|
601
|
+
@click="select(cell)"
|
|
602
|
+
>
|
|
603
|
+
{{ cell.day }}
|
|
604
|
+
</button>
|
|
605
|
+
</div>
|
|
606
|
+
</div>
|
|
607
|
+
</div>
|
|
608
|
+
|
|
609
|
+
<!-- Vue des années : 12 par page, même hauteur que la vue des jours (pas de saut) -->
|
|
610
|
+
<div
|
|
611
|
+
v-else
|
|
612
|
+
ref="yearsEl"
|
|
613
|
+
role="group"
|
|
614
|
+
:aria-labelledby="monthId"
|
|
615
|
+
class="grid h-[15.5rem] grid-cols-4 content-center gap-1"
|
|
616
|
+
@keydown="onYearsKeydown"
|
|
617
|
+
>
|
|
618
|
+
<button
|
|
619
|
+
v-for="y in years"
|
|
620
|
+
:key="y.year"
|
|
621
|
+
type="button"
|
|
622
|
+
:data-year="y.year"
|
|
623
|
+
:tabindex="y.year === yearCursor ? 0 : -1"
|
|
624
|
+
:disabled="y.disabled"
|
|
625
|
+
:aria-pressed="y.selected"
|
|
626
|
+
:aria-current="y.today ? 'date' : undefined"
|
|
627
|
+
class="flex h-10 items-center justify-center rounded-socle-sm text-sm tabular-nums outline-none transition-colors focus-visible:ring-2 focus-visible:ring-socle-accent/50"
|
|
628
|
+
:class="yearClasses(y)"
|
|
629
|
+
@click="chooseYear(y.year)"
|
|
630
|
+
>
|
|
631
|
+
{{ y.year }}
|
|
632
|
+
</button>
|
|
633
|
+
</div>
|
|
634
|
+
|
|
635
|
+
<!-- Pied : raccourcis -->
|
|
636
|
+
<div class="mt-2 flex items-center justify-between border-t border-socle-border-subtle pt-2">
|
|
637
|
+
<button
|
|
638
|
+
type="button"
|
|
639
|
+
:disabled="todayDisabled"
|
|
640
|
+
class="rounded-socle-sm px-2 py-1 text-sm text-socle-accent-text outline-none hover:bg-socle-surface-hover focus-visible:ring-2 focus-visible:ring-socle-accent/50 disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:bg-transparent"
|
|
641
|
+
@click="selectToday"
|
|
642
|
+
>
|
|
643
|
+
{{ L.today }}
|
|
644
|
+
</button>
|
|
645
|
+
<button
|
|
646
|
+
type="button"
|
|
647
|
+
class="rounded-socle-sm px-2 py-1 text-sm text-socle-text-muted outline-none hover:bg-socle-surface-hover focus-visible:ring-2 focus-visible:ring-socle-accent/50"
|
|
648
|
+
@click="clear"
|
|
649
|
+
>
|
|
650
|
+
{{ L.clear }}
|
|
651
|
+
</button>
|
|
652
|
+
</div>
|
|
653
|
+
</div>
|
|
654
|
+
|
|
655
|
+
<!-- disabled : comme un champ désactivé, la valeur n'est pas envoyée avec le formulaire -->
|
|
656
|
+
<input v-if="name" type="hidden" :name="name" :value="model ?? ''" :disabled="disabled" />
|
|
657
|
+
</SocleField>
|
|
658
|
+
</template>
|
|
659
|
+
|