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
package/SocleSelect.vue
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Prérequis : Vue >= 3.5 (useId, generics), Tailwind CSS v4 avec `@import "socle-ui/tailwind.css"` (utilitaire socle-field-box).
|
|
3
|
+
<SocleSelect
|
|
4
|
+
v-model="country"
|
|
5
|
+
label="Pays"
|
|
6
|
+
name="country"
|
|
7
|
+
:options="[
|
|
8
|
+
{ value: 'fr', label: 'France' },
|
|
9
|
+
{ value: 'be', label: 'Belgique' },
|
|
10
|
+
{ value: 'lu', label: 'Luxembourg', disabled: true },
|
|
11
|
+
]"
|
|
12
|
+
hint="Pays de livraison."
|
|
13
|
+
:error="errors.country"
|
|
14
|
+
required
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
- Erreur : comme SocleInput, la prop `error` colore le champ, affiche le message (annoncé poliment)
|
|
18
|
+
et pose aria-invalid ; `hint` est affiché tant qu'il n'y a pas d'erreur.
|
|
19
|
+
- required : astérisque sur le label + aria-required. La validation reste à faire côté appelant
|
|
20
|
+
(un <input type="hidden"> n'est jamais validé par le navigateur).
|
|
21
|
+
-->
|
|
22
|
+
<script setup lang="ts" generic="T extends string | number">
|
|
23
|
+
import { nextTick, onBeforeUnmount, onMounted, ref, useId, watch, computed } from 'vue'
|
|
24
|
+
import SocleField from './SocleField.vue'
|
|
25
|
+
import './socle.css'
|
|
26
|
+
|
|
27
|
+
export interface SelectOption<V> {
|
|
28
|
+
value: V
|
|
29
|
+
label: string
|
|
30
|
+
disabled?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const props = withDefaults(
|
|
34
|
+
defineProps<{
|
|
35
|
+
options: SelectOption<T>[]
|
|
36
|
+
label?: string
|
|
37
|
+
placeholder?: string
|
|
38
|
+
/** Si défini, un <input type="hidden"> est rendu pour les envois de formulaire classiques (PHP, etc.) */
|
|
39
|
+
name?: string
|
|
40
|
+
disabled?: boolean
|
|
41
|
+
/** Nom accessible quand il n'y a pas de `label` visible (sinon le placeholder est utilisé). */
|
|
42
|
+
ariaLabel?: string
|
|
43
|
+
/** Texte d'aide sous le champ (remplacé par `error` si présent). */
|
|
44
|
+
hint?: string
|
|
45
|
+
/** Message d'erreur : met le champ en erreur. */
|
|
46
|
+
error?: string
|
|
47
|
+
required?: boolean
|
|
48
|
+
}>(),
|
|
49
|
+
{ placeholder: 'Sélectionner…' },
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const model = defineModel<T | null>({ default: null })
|
|
53
|
+
|
|
54
|
+
defineSlots<{
|
|
55
|
+
option?(props: { option: SelectOption<T>; selected: boolean }): unknown
|
|
56
|
+
}>()
|
|
57
|
+
|
|
58
|
+
/* ---------- Identifiants ---------- */
|
|
59
|
+
const uid = useId()
|
|
60
|
+
const triggerId = `${uid}-trigger`
|
|
61
|
+
const listId = `${uid}-list`
|
|
62
|
+
const optionId = (i: number) => `${uid}-opt-${i}`
|
|
63
|
+
// Un nom d'ancre unique par instance (dashed-ident) pour pouvoir en afficher plusieurs.
|
|
64
|
+
const anchorName = `--select-${uid.replace(/[^a-zA-Z0-9_-]/g, '')}`
|
|
65
|
+
|
|
66
|
+
/* ---------- État ---------- */
|
|
67
|
+
const field = ref<InstanceType<typeof SocleField> | null>(null)
|
|
68
|
+
const root = computed(() => field.value?.root ?? null)
|
|
69
|
+
const trigger = ref<HTMLButtonElement | null>(null)
|
|
70
|
+
const listbox = ref<HTMLElement | null>(null)
|
|
71
|
+
const open = ref(false)
|
|
72
|
+
const activeIndex = ref(-1)
|
|
73
|
+
|
|
74
|
+
const selectedIndex = computed(() => props.options.findIndex((o) => o.value === model.value))
|
|
75
|
+
const selected = computed(() => props.options[selectedIndex.value] ?? null)
|
|
76
|
+
|
|
77
|
+
/* ---------- Navigation ---------- */
|
|
78
|
+
/** Prochain index non désactivé dans la direction donnée, ou -1. */
|
|
79
|
+
function nextEnabled(from: number, dir: 1 | -1): number {
|
|
80
|
+
for (let i = from + dir; i >= 0 && i < props.options.length; i += dir) {
|
|
81
|
+
if (!props.options[i]!.disabled) return i
|
|
82
|
+
}
|
|
83
|
+
return -1
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function move(dir: 1 | -1) {
|
|
87
|
+
const n = nextEnabled(activeIndex.value, dir)
|
|
88
|
+
if (n >= 0) activeIndex.value = n
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function show() {
|
|
92
|
+
if (props.disabled || open.value) return
|
|
93
|
+
listbox.value?.showPopover()
|
|
94
|
+
open.value = true
|
|
95
|
+
activeIndex.value =
|
|
96
|
+
selectedIndex.value >= 0 && !props.options[selectedIndex.value]!.disabled
|
|
97
|
+
? selectedIndex.value
|
|
98
|
+
: nextEnabled(-1, 1)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function hide() {
|
|
102
|
+
if (!open.value) return
|
|
103
|
+
listbox.value?.hidePopover()
|
|
104
|
+
open.value = false
|
|
105
|
+
activeIndex.value = -1
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function choose(i: number) {
|
|
109
|
+
const option = props.options[i]
|
|
110
|
+
if (!option || option.disabled) return
|
|
111
|
+
model.value = option.value
|
|
112
|
+
hide()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* ---------- Recherche au clavier (typeahead) ---------- */
|
|
116
|
+
let buffer = ''
|
|
117
|
+
let bufferTimer: ReturnType<typeof setTimeout> | undefined
|
|
118
|
+
|
|
119
|
+
function typeahead(char: string) {
|
|
120
|
+
buffer += char.toLowerCase()
|
|
121
|
+
clearTimeout(bufferTimer)
|
|
122
|
+
bufferTimer = setTimeout(() => (buffer = ''), 500)
|
|
123
|
+
|
|
124
|
+
const n = props.options.length
|
|
125
|
+
if (n === 0) return
|
|
126
|
+
// Point de départ : option active si la liste est ouverte, sinon option sélectionnée.
|
|
127
|
+
// Sans point de départ, on cherche dès la première option ; sinon un premier caractère
|
|
128
|
+
// passe à l'option suivante (taper « b » plusieurs fois parcourt les options en « b »).
|
|
129
|
+
const base = open.value ? activeIndex.value : selectedIndex.value
|
|
130
|
+
const start = base < 0 ? 0 : base + (buffer.length === 1 ? 1 : 0)
|
|
131
|
+
for (let k = 0; k < n; k++) {
|
|
132
|
+
const i = (start + k) % n
|
|
133
|
+
const o = props.options[i]!
|
|
134
|
+
if (!o.disabled && o.label.toLowerCase().startsWith(buffer)) {
|
|
135
|
+
if (!open.value) show()
|
|
136
|
+
activeIndex.value = i
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/* ---------- Événements ---------- */
|
|
143
|
+
function onKeydown(e: KeyboardEvent) {
|
|
144
|
+
if (props.disabled) return
|
|
145
|
+
|
|
146
|
+
switch (e.key) {
|
|
147
|
+
case 'ArrowDown':
|
|
148
|
+
case 'ArrowUp':
|
|
149
|
+
e.preventDefault()
|
|
150
|
+
if (!open.value) show()
|
|
151
|
+
else move(e.key === 'ArrowDown' ? 1 : -1)
|
|
152
|
+
break
|
|
153
|
+
case 'Home':
|
|
154
|
+
case 'End':
|
|
155
|
+
if (!open.value) return
|
|
156
|
+
e.preventDefault()
|
|
157
|
+
activeIndex.value = e.key === 'Home' ? nextEnabled(-1, 1) : nextEnabled(props.options.length, -1)
|
|
158
|
+
break
|
|
159
|
+
case ' ':
|
|
160
|
+
case 'Enter':
|
|
161
|
+
e.preventDefault()
|
|
162
|
+
// Recherche en cours (« new y » → « New York ») : l'espace fait partie du texte cherché.
|
|
163
|
+
if (e.key === ' ' && buffer) typeahead(' ')
|
|
164
|
+
else if (!open.value) show()
|
|
165
|
+
else choose(activeIndex.value)
|
|
166
|
+
break
|
|
167
|
+
case 'Escape':
|
|
168
|
+
if (open.value) {
|
|
169
|
+
e.preventDefault()
|
|
170
|
+
hide()
|
|
171
|
+
}
|
|
172
|
+
break
|
|
173
|
+
case 'Tab':
|
|
174
|
+
hide()
|
|
175
|
+
break
|
|
176
|
+
default:
|
|
177
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) typeahead(e.key)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function toggle() {
|
|
182
|
+
open.value ? hide() : show()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Garde `open` synchronisé si le navigateur ferme le popover de lui-même. */
|
|
186
|
+
function onToggle(e: Event) {
|
|
187
|
+
open.value = (e as ToggleEvent).newState === 'open'
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Ferme la liste quand le focus quitte le composant (pas seulement sur Tab). */
|
|
191
|
+
function onFocusOut(e: FocusEvent) {
|
|
192
|
+
if (!root.value?.contains(e.relatedTarget as Node | null)) hide()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Comme un <select> natif, un clic sur le label donne le focus sans ouvrir la liste
|
|
196
|
+
* (par défaut, cliquer le label d'un <button> déclencherait un clic dessus). */
|
|
197
|
+
function onLabelClick(e: MouseEvent) {
|
|
198
|
+
e.preventDefault()
|
|
199
|
+
trigger.value?.focus()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
watch(
|
|
203
|
+
() => props.disabled,
|
|
204
|
+
(d) => {
|
|
205
|
+
if (d) hide()
|
|
206
|
+
},
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
function onDocumentPointerDown(e: PointerEvent) {
|
|
210
|
+
if (open.value && !root.value?.contains(e.target as Node)) hide()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
onMounted(() => document.addEventListener('pointerdown', onDocumentPointerDown))
|
|
214
|
+
onBeforeUnmount(() => {
|
|
215
|
+
document.removeEventListener('pointerdown', onDocumentPointerDown)
|
|
216
|
+
clearTimeout(bufferTimer)
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
watch(activeIndex, async (i) => {
|
|
221
|
+
if (!open.value || i < 0) return
|
|
222
|
+
await nextTick()
|
|
223
|
+
document.getElementById(optionId(i))?.scrollIntoView({ block: 'nearest' })
|
|
224
|
+
})
|
|
225
|
+
</script>
|
|
226
|
+
|
|
227
|
+
<template>
|
|
228
|
+
<SocleField
|
|
229
|
+
ref="field"
|
|
230
|
+
v-slot="{ labelId, describedBy }"
|
|
231
|
+
:control-id="triggerId"
|
|
232
|
+
:label="label"
|
|
233
|
+
:required="required"
|
|
234
|
+
:hint="hint"
|
|
235
|
+
:error="error"
|
|
236
|
+
:style="{ '--socle-anchor': anchorName }"
|
|
237
|
+
@focusout="onFocusOut"
|
|
238
|
+
@label-click="onLabelClick"
|
|
239
|
+
>
|
|
240
|
+
|
|
241
|
+
<!-- Le bouton est l'ancre -->
|
|
242
|
+
<button
|
|
243
|
+
:id="triggerId"
|
|
244
|
+
ref="trigger"
|
|
245
|
+
type="button"
|
|
246
|
+
role="combobox"
|
|
247
|
+
:aria-label="label ? undefined : (ariaLabel ?? placeholder)"
|
|
248
|
+
aria-haspopup="listbox"
|
|
249
|
+
:aria-expanded="open"
|
|
250
|
+
:aria-controls="listId"
|
|
251
|
+
:aria-activedescendant="open && activeIndex >= 0 ? optionId(activeIndex) : undefined"
|
|
252
|
+
:aria-invalid="error ? true : undefined"
|
|
253
|
+
:aria-required="required || undefined"
|
|
254
|
+
:aria-describedby="describedBy"
|
|
255
|
+
:disabled="disabled"
|
|
256
|
+
class="socle-select-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"
|
|
257
|
+
@click="toggle"
|
|
258
|
+
@keydown="onKeydown"
|
|
259
|
+
@keyup.space.prevent
|
|
260
|
+
>
|
|
261
|
+
<span class="truncate" :class="{ 'text-socle-placeholder': !selected }">
|
|
262
|
+
{{ selected?.label ?? placeholder }}
|
|
263
|
+
</span>
|
|
264
|
+
<svg
|
|
265
|
+
class="size-4 shrink-0 text-socle-text-subtle transition-transform duration-150 motion-reduce:transition-none"
|
|
266
|
+
:class="{ 'rotate-180': open }"
|
|
267
|
+
viewBox="0 0 20 20"
|
|
268
|
+
fill="none"
|
|
269
|
+
stroke="currentColor"
|
|
270
|
+
stroke-width="1.75"
|
|
271
|
+
stroke-linecap="round"
|
|
272
|
+
stroke-linejoin="round"
|
|
273
|
+
aria-hidden="true"
|
|
274
|
+
>
|
|
275
|
+
<path d="m5 7.5 5 5 5-5" />
|
|
276
|
+
</svg>
|
|
277
|
+
</button>
|
|
278
|
+
|
|
279
|
+
<!-- La liste est un popover (top layer) ancré au bouton -->
|
|
280
|
+
<ul
|
|
281
|
+
:id="listId"
|
|
282
|
+
ref="listbox"
|
|
283
|
+
popover="manual"
|
|
284
|
+
role="listbox"
|
|
285
|
+
:aria-labelledby="label ? labelId : undefined"
|
|
286
|
+
:aria-label="label ? undefined : (ariaLabel ?? placeholder)"
|
|
287
|
+
class="socle-select-listbox list-none overflow-y-auto rounded-socle border border-socle-border-subtle bg-socle-surface p-1 text-socle-text shadow-lg"
|
|
288
|
+
@toggle="onToggle"
|
|
289
|
+
@mousedown.prevent
|
|
290
|
+
>
|
|
291
|
+
<li
|
|
292
|
+
v-for="(opt, i) in options"
|
|
293
|
+
:id="optionId(i)"
|
|
294
|
+
:key="opt.value"
|
|
295
|
+
role="option"
|
|
296
|
+
:aria-selected="opt.value === model"
|
|
297
|
+
:aria-disabled="opt.disabled || undefined"
|
|
298
|
+
class="flex cursor-default select-none items-center justify-between gap-2 rounded-socle-sm px-2.5 py-1.5"
|
|
299
|
+
:class="[
|
|
300
|
+
opt.disabled ? 'opacity-40' : '',
|
|
301
|
+
i === activeIndex && !opt.disabled && opt.value !== model ? 'bg-socle-surface-hover' : '',
|
|
302
|
+
opt.value === model ? 'bg-socle-accent font-medium text-socle-on-accent' : '',
|
|
303
|
+
// Option sélectionnée ET active : un liseré garde le curseur clavier visible
|
|
304
|
+
opt.value === model && i === activeIndex ? 'ring-2 ring-inset ring-socle-on-accent/60' : '',
|
|
305
|
+
]"
|
|
306
|
+
@mousemove="!opt.disabled && (activeIndex = i)"
|
|
307
|
+
@click="choose(i)"
|
|
308
|
+
>
|
|
309
|
+
<slot name="option" :option="opt" :selected="opt.value === model">
|
|
310
|
+
<span class="truncate">{{ opt.label }}</span>
|
|
311
|
+
</slot>
|
|
312
|
+
<svg
|
|
313
|
+
v-if="opt.value === model"
|
|
314
|
+
class="size-4 shrink-0"
|
|
315
|
+
viewBox="0 0 20 20"
|
|
316
|
+
fill="none"
|
|
317
|
+
stroke="currentColor"
|
|
318
|
+
stroke-width="2"
|
|
319
|
+
stroke-linecap="round"
|
|
320
|
+
stroke-linejoin="round"
|
|
321
|
+
aria-hidden="true"
|
|
322
|
+
>
|
|
323
|
+
<path d="m4.5 10.5 3.5 3.5 7.5-8" />
|
|
324
|
+
</svg>
|
|
325
|
+
</li>
|
|
326
|
+
</ul>
|
|
327
|
+
|
|
328
|
+
<!-- disabled : comme un <select disabled>, la valeur n'est pas envoyée avec le formulaire -->
|
|
329
|
+
<input v-if="name" type="hidden" :name="name" :value="model ?? ''" :disabled="disabled" />
|
|
330
|
+
</SocleField>
|
|
331
|
+
</template>
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SocleTableHead.vue
|
|
3
|
+
Ligne d'en-têtes d'un tableau, à placer entre <thead> et </thead> : colonnes redimensionnables
|
|
4
|
+
(souris, tactile, clavier) et triables. Chaque colonne a un nom, un libellé, un type (string, date, number) et une largeur.
|
|
5
|
+
|
|
6
|
+
Prérequis : Vue >= 3.5 (defineModel, generics), Tailwind CSS v4.
|
|
7
|
+
|
|
8
|
+
<table>
|
|
9
|
+
<thead>
|
|
10
|
+
<SocleTableHead v-model:sort="sort" v-model:widths="widths" :columns="columns" />
|
|
11
|
+
</thead>
|
|
12
|
+
<tbody>
|
|
13
|
+
<tr v-for="row in sortRows(orders, columns, sort)" :key="row.id">…</tr>
|
|
14
|
+
</tbody>
|
|
15
|
+
</table>
|
|
16
|
+
|
|
17
|
+
- Largeurs : posées sur les <th> ; le tableau passe en `table-layout: fixed` et prend pour
|
|
18
|
+
largeur la somme des colonnes (désactivable avec :auto-table-width="false").
|
|
19
|
+
Placer le tableau dans un conteneur `overflow-x-auto`.
|
|
20
|
+
- Redimensionner : glisser le bord droit d'un en-tête ; double-clic = largeur initiale ;
|
|
21
|
+
au clavier, ← / → sur la poignée (Maj = pas ×5), Début / Fin = min / max.
|
|
22
|
+
- Mémoriser : storage-key="commandes" enregistre les largeurs dans localStorage et les relit
|
|
23
|
+
au montage (écrites à la fin d'un glisser, pas à chaque mouvement).
|
|
24
|
+
- Trier : clic sur l'en-tête d'une colonne `sortable` → croissant, décroissant, sans tri.
|
|
25
|
+
|
|
26
|
+
Le composant ne trie pas les lignes : il met à jour v-model:sort ({ name, direction }),
|
|
27
|
+
et l'appelant trie ses données, avec sortRows() (socle-ui/table) ou sa propre fonction.
|
|
28
|
+
|
|
29
|
+
-->
|
|
30
|
+
<script setup lang="ts" generic="R">
|
|
31
|
+
import { computed, onBeforeUnmount, onMounted, ref, watch, watchEffect } from 'vue'
|
|
32
|
+
import {
|
|
33
|
+
DEFAULT_COLUMN_MIN_WIDTH,
|
|
34
|
+
DEFAULT_COLUMN_WIDTH,
|
|
35
|
+
type SortDirection,
|
|
36
|
+
type TableColumn,
|
|
37
|
+
type TableSort,
|
|
38
|
+
} from './table'
|
|
39
|
+
import './socle.css'
|
|
40
|
+
|
|
41
|
+
interface Labels {
|
|
42
|
+
/** Nom accessible de la poignée, suivi du libellé de la colonne. */
|
|
43
|
+
resize: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const defaultLabels: Labels = {
|
|
47
|
+
resize: 'Redimensionner la colonne',
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const props = withDefaults(
|
|
51
|
+
defineProps<{
|
|
52
|
+
columns: TableColumn<R>[]
|
|
53
|
+
/** Passe le tableau en `table-layout: fixed`, avec pour largeur la somme des colonnes. */
|
|
54
|
+
autoTableWidth?: boolean
|
|
55
|
+
/** Pas du redimensionnement au clavier, en pixels (×5 avec Maj). */
|
|
56
|
+
step?: number
|
|
57
|
+
/** Clé localStorage où mémoriser les largeurs. Sans clé, rien n'est enregistré. */
|
|
58
|
+
storageKey?: string
|
|
59
|
+
labels?: Partial<Labels>
|
|
60
|
+
}>(),
|
|
61
|
+
{ autoTableWidth: true, step: 10 },
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
/** Tri courant : nom de la colonne et sens, ou null. */
|
|
65
|
+
const sort = defineModel<TableSort | null>('sort', { default: null })
|
|
66
|
+
/** Largeurs modifiées par l'utilisateur, par nom de colonne (en pixels). */
|
|
67
|
+
const widths = defineModel<Record<string, number>>('widths', { default: () => ({}) })
|
|
68
|
+
|
|
69
|
+
defineSlots<{
|
|
70
|
+
/** Contenu d'un en-tête, à la place du libellé. */
|
|
71
|
+
header?(props: { column: TableColumn<R>; direction: SortDirection | null }): unknown
|
|
72
|
+
}>()
|
|
73
|
+
|
|
74
|
+
const L = computed<Labels>(() => ({ ...defaultLabels, ...props.labels }))
|
|
75
|
+
|
|
76
|
+
/* ---------- Largeurs ---------- */
|
|
77
|
+
const minOf = (c: TableColumn<R>) => c.minWidth ?? DEFAULT_COLUMN_MIN_WIDTH
|
|
78
|
+
const maxOf = (c: TableColumn<R>) => c.maxWidth ?? Infinity
|
|
79
|
+
|
|
80
|
+
function clamp(c: TableColumn<R>, w: number) {
|
|
81
|
+
return Math.round(Math.min(Math.max(w, minOf(c)), maxOf(c)))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function widthOf(c: TableColumn<R>) {
|
|
85
|
+
return clamp(c, widths.value[c.name] ?? c.width ?? DEFAULT_COLUMN_WIDTH)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function setWidth(c: TableColumn<R>, w: number) {
|
|
89
|
+
const next = clamp(c, w)
|
|
90
|
+
if (next !== widths.value[c.name]) widths.value = { ...widths.value, [c.name]: next }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Revient à la largeur initiale de la colonne. */
|
|
94
|
+
function resetWidth(c: TableColumn<R>) {
|
|
95
|
+
if (!(c.name in widths.value)) return
|
|
96
|
+
const { [c.name]: _, ...rest } = widths.value
|
|
97
|
+
widths.value = rest
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* ---------- Mémorisation (localStorage) ---------- */
|
|
101
|
+
/** Lit les largeurs enregistrées ; ignore les colonnes inconnues et les valeurs invalides. */
|
|
102
|
+
function loadWidths(key: string): Record<string, number> | null {
|
|
103
|
+
try {
|
|
104
|
+
const raw = localStorage.getItem(key)
|
|
105
|
+
if (!raw) return null
|
|
106
|
+
const data: unknown = JSON.parse(raw)
|
|
107
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) return null
|
|
108
|
+
const names = new Set(props.columns.map((c) => c.name))
|
|
109
|
+
const out: Record<string, number> = {}
|
|
110
|
+
for (const [name, w] of Object.entries(data)) {
|
|
111
|
+
if (names.has(name) && typeof w === 'number' && Number.isFinite(w)) out[name] = w
|
|
112
|
+
}
|
|
113
|
+
return out
|
|
114
|
+
} catch {
|
|
115
|
+
// localStorage indisponible (SSR, navigation privée, stockage bloqué) ou JSON invalide.
|
|
116
|
+
return null
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function saveWidths(key: string, value: Record<string, number>) {
|
|
121
|
+
try {
|
|
122
|
+
if (Object.keys(value).length === 0) localStorage.removeItem(key)
|
|
123
|
+
else localStorage.setItem(key, JSON.stringify(value))
|
|
124
|
+
} catch {
|
|
125
|
+
// Quota dépassé ou stockage bloqué : la largeur reste appliquée, sans être mémorisée.
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Lecture dès la création (et à chaque changement de clé) : le premier rendu a déjà les bonnes largeurs.
|
|
130
|
+
watch(
|
|
131
|
+
() => props.storageKey,
|
|
132
|
+
(key) => {
|
|
133
|
+
if (!key) return
|
|
134
|
+
const stored = loadWidths(key)
|
|
135
|
+
if (stored) widths.value = stored
|
|
136
|
+
},
|
|
137
|
+
{ immediate: true },
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
/* ---------- Largeur du tableau ---------- */
|
|
141
|
+
const row = ref<HTMLTableRowElement | null>(null)
|
|
142
|
+
const table = ref<HTMLTableElement | null>(null)
|
|
143
|
+
let initialTableStyle: { tableLayout: string; width: string } | null = null
|
|
144
|
+
|
|
145
|
+
const totalWidth = computed(() => props.columns.reduce((sum, c) => sum + widthOf(c), 0))
|
|
146
|
+
|
|
147
|
+
onMounted(() => {
|
|
148
|
+
table.value = row.value?.closest('table') ?? null
|
|
149
|
+
if (table.value) {
|
|
150
|
+
initialTableStyle = { tableLayout: table.value.style.tableLayout, width: table.value.style.width }
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
watchEffect(() => {
|
|
155
|
+
const t = table.value
|
|
156
|
+
if (!t || !initialTableStyle) return
|
|
157
|
+
if (props.autoTableWidth) {
|
|
158
|
+
t.style.tableLayout = 'fixed'
|
|
159
|
+
t.style.width = `${totalWidth.value}px`
|
|
160
|
+
} else {
|
|
161
|
+
Object.assign(t.style, initialTableStyle)
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
/* ---------- Redimensionnement à la souris / au doigt ---------- */
|
|
166
|
+
const resizing = ref<string | null>(null)
|
|
167
|
+
/** État du glisser ; `startWidths` permet à Échap de rendre le v-model tel qu'il était. */
|
|
168
|
+
let drag: { startX: number; startWidth: number; startWidths: Record<string, number>; sign: 1 | -1 } | null = null
|
|
169
|
+
|
|
170
|
+
function onResizeStart(e: PointerEvent, c: TableColumn<R>) {
|
|
171
|
+
if (e.button !== 0) return
|
|
172
|
+
e.preventDefault()
|
|
173
|
+
const handle = e.currentTarget as HTMLElement
|
|
174
|
+
handle.setPointerCapture(e.pointerId)
|
|
175
|
+
handle.focus()
|
|
176
|
+
// En écriture de droite à gauche, la poignée est sur le bord gauche : le sens s'inverse.
|
|
177
|
+
const sign = getComputedStyle(handle).direction === 'rtl' ? -1 : 1
|
|
178
|
+
drag = { startX: e.clientX, startWidth: widthOf(c), startWidths: widths.value, sign }
|
|
179
|
+
resizing.value = c.name
|
|
180
|
+
document.documentElement.classList.add('socle-table-resizing')
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function onResizeMove(e: PointerEvent, c: TableColumn<R>) {
|
|
184
|
+
if (!drag || resizing.value !== c.name) return
|
|
185
|
+
setWidth(c, drag.startWidth + drag.sign * (e.clientX - drag.startX))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function onResizeEnd(e: PointerEvent) {
|
|
189
|
+
const handle = e.currentTarget as HTMLElement
|
|
190
|
+
if (handle.hasPointerCapture(e.pointerId)) handle.releasePointerCapture(e.pointerId)
|
|
191
|
+
stopResize()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function stopResize() {
|
|
195
|
+
drag = null
|
|
196
|
+
resizing.value = null
|
|
197
|
+
document.documentElement.classList.remove('socle-table-resizing')
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* ---------- Redimensionnement au clavier ---------- */
|
|
201
|
+
function onResizeKeydown(e: KeyboardEvent, c: TableColumn<R>) {
|
|
202
|
+
const step = e.shiftKey ? props.step * 5 : props.step
|
|
203
|
+
const rtl = getComputedStyle(e.currentTarget as HTMLElement).direction === 'rtl'
|
|
204
|
+
const grow = rtl ? 'ArrowLeft' : 'ArrowRight'
|
|
205
|
+
switch (e.key) {
|
|
206
|
+
case 'ArrowLeft':
|
|
207
|
+
case 'ArrowRight':
|
|
208
|
+
setWidth(c, widthOf(c) + (e.key === grow ? step : -step))
|
|
209
|
+
break
|
|
210
|
+
case 'Home':
|
|
211
|
+
setWidth(c, minOf(c))
|
|
212
|
+
break
|
|
213
|
+
case 'End':
|
|
214
|
+
if (c.maxWidth === undefined) return
|
|
215
|
+
setWidth(c, c.maxWidth)
|
|
216
|
+
break
|
|
217
|
+
case 'Escape':
|
|
218
|
+
// Pendant un glisser : annule, et rend v-model:widths tel qu'il était (sans entrée ajoutée).
|
|
219
|
+
if (!drag) return
|
|
220
|
+
widths.value = drag.startWidths
|
|
221
|
+
stopResize()
|
|
222
|
+
break
|
|
223
|
+
default:
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
e.preventDefault()
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Écriture à chaque changement, sauf pendant un glisser : une seule fois, au relâchement.
|
|
230
|
+
watch(
|
|
231
|
+
[widths, resizing],
|
|
232
|
+
() => {
|
|
233
|
+
if (props.storageKey && !resizing.value) saveWidths(props.storageKey, widths.value)
|
|
234
|
+
},
|
|
235
|
+
{ deep: true },
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
onBeforeUnmount(() => {
|
|
239
|
+
stopResize()
|
|
240
|
+
if (table.value && initialTableStyle) Object.assign(table.value.style, initialTableStyle)
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
/* ---------- Tri ---------- */
|
|
244
|
+
function directionOf(c: TableColumn<R>): SortDirection | null {
|
|
245
|
+
return sort.value?.name === c.name ? sort.value.direction : null
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Croissant → décroissant → sans tri. */
|
|
249
|
+
function toggleSort(c: TableColumn<R>) {
|
|
250
|
+
const d = directionOf(c)
|
|
251
|
+
sort.value = d === null ? { name: c.name, direction: 'asc' } : d === 'asc' ? { name: c.name, direction: 'desc' } : null
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function ariaSort(c: TableColumn<R>) {
|
|
255
|
+
const d = directionOf(c)
|
|
256
|
+
return d === 'asc' ? 'ascending' : d === 'desc' ? 'descending' : undefined
|
|
257
|
+
}
|
|
258
|
+
</script>
|
|
259
|
+
|
|
260
|
+
<template>
|
|
261
|
+
<tr ref="row">
|
|
262
|
+
<th
|
|
263
|
+
v-for="col in columns"
|
|
264
|
+
:key="col.name"
|
|
265
|
+
scope="col"
|
|
266
|
+
:aria-sort="ariaSort(col)"
|
|
267
|
+
class="relative px-3 py-2 align-middle text-sm font-medium text-socle-text-muted"
|
|
268
|
+
:class="col.type === 'number' ? 'text-right' : 'text-left'"
|
|
269
|
+
:style="{ width: `${widthOf(col)}px` }"
|
|
270
|
+
>
|
|
271
|
+
<button
|
|
272
|
+
v-if="col.sortable"
|
|
273
|
+
type="button"
|
|
274
|
+
class="group/sort -mx-1 flex w-[calc(100%+0.5rem)] min-w-0 cursor-pointer items-center gap-1 rounded-socle-sm px-1 outline-none hover:text-socle-text focus-visible:ring-2 focus-visible:ring-socle-accent/50"
|
|
275
|
+
:class="col.type === 'number' ? 'flex-row-reverse' : ''"
|
|
276
|
+
@click="toggleSort(col)"
|
|
277
|
+
>
|
|
278
|
+
<span class="min-w-0 truncate">
|
|
279
|
+
<slot name="header" :column="col" :direction="directionOf(col)">{{ col.label }}</slot>
|
|
280
|
+
</span>
|
|
281
|
+
<!-- Croissant / décroissant / triable (visible au survol et au focus) -->
|
|
282
|
+
<svg
|
|
283
|
+
class="size-4 shrink-0"
|
|
284
|
+
:class="
|
|
285
|
+
directionOf(col)
|
|
286
|
+
? 'text-socle-accent'
|
|
287
|
+
: 'text-socle-placeholder opacity-0 group-hover/sort:opacity-100 group-focus-visible/sort:opacity-100'
|
|
288
|
+
"
|
|
289
|
+
viewBox="0 0 20 20"
|
|
290
|
+
fill="none"
|
|
291
|
+
stroke="currentColor"
|
|
292
|
+
stroke-width="1.75"
|
|
293
|
+
stroke-linecap="round"
|
|
294
|
+
stroke-linejoin="round"
|
|
295
|
+
aria-hidden="true"
|
|
296
|
+
>
|
|
297
|
+
<path v-if="directionOf(col) === 'asc'" d="M10 15.5v-11m-4.5 4.5L10 4.5 14.5 9" />
|
|
298
|
+
<path v-else-if="directionOf(col) === 'desc'" d="M10 4.5v11m-4.5-4.5 4.5 4.5 4.5-4.5" />
|
|
299
|
+
<path v-else d="m6.5 8 3.5-3.5L13.5 8m-7 4 3.5 3.5 3.5-3.5" />
|
|
300
|
+
</svg>
|
|
301
|
+
</button>
|
|
302
|
+
|
|
303
|
+
<span v-else class="block truncate">
|
|
304
|
+
<slot name="header" :column="col" :direction="null">{{ col.label }}</slot>
|
|
305
|
+
</span>
|
|
306
|
+
|
|
307
|
+
<!-- Poignée : bord droit de l'en-tête (motif « window splitter » de l'APG) -->
|
|
308
|
+
<span
|
|
309
|
+
v-if="col.resizable !== false"
|
|
310
|
+
role="separator"
|
|
311
|
+
aria-orientation="vertical"
|
|
312
|
+
tabindex="0"
|
|
313
|
+
:aria-label="`${L.resize} « ${col.label} »`"
|
|
314
|
+
:aria-valuenow="widthOf(col)"
|
|
315
|
+
:aria-valuemin="minOf(col)"
|
|
316
|
+
:aria-valuemax="col.maxWidth"
|
|
317
|
+
class="group/resize absolute inset-y-0 -end-1.5 z-10 w-3 cursor-col-resize touch-none select-none outline-none"
|
|
318
|
+
@pointerdown="onResizeStart($event, col)"
|
|
319
|
+
@pointermove="onResizeMove($event, col)"
|
|
320
|
+
@pointerup="onResizeEnd"
|
|
321
|
+
@pointercancel="onResizeEnd"
|
|
322
|
+
@keydown="onResizeKeydown($event, col)"
|
|
323
|
+
@dblclick="resetWidth(col)"
|
|
324
|
+
>
|
|
325
|
+
<span
|
|
326
|
+
class="absolute inset-y-1.5 left-1/2 w-0.5 -translate-x-1/2 rounded-full transition-colors motion-reduce:transition-none"
|
|
327
|
+
:class="
|
|
328
|
+
resizing === col.name
|
|
329
|
+
? 'bg-socle-accent'
|
|
330
|
+
: 'bg-socle-border-subtle group-hover/resize:bg-socle-border-strong group-focus-visible/resize:bg-socle-accent'
|
|
331
|
+
"
|
|
332
|
+
/>
|
|
333
|
+
</span>
|
|
334
|
+
</th>
|
|
335
|
+
</tr>
|
|
336
|
+
</template>
|