codevdesign 1.0.23 → 1.0.24

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.
@@ -1,280 +1,332 @@
1
- <template>
2
- <div>
3
- <v-form
4
- ref="form"
5
- v-model="formValide"
6
- @submit.prevent
7
- >
8
- <v-combobox
9
- v-model="codeBudgetaire"
10
- :items="codeBudgetairesProp"
11
- v-bind="$attrs"
12
- persistent-hint
13
- variant="outlined"
14
- hide-details="auto"
15
- :error="!estValideComplet"
16
- :rules="reglesVuetify"
17
- :hint="afficherHint ? placeholder : ''"
18
- @blur="sauvegarder"
19
- @keydown.enter="sauvegarder"
20
- @keydown="caractereAutorises"
21
- @update:modelValue="gererChangement"
22
- @paste="gererPaste"
23
- />
24
- </v-form>
25
- </div>
26
- </template>
27
-
28
- <script setup lang="ts">
29
- import { ref, computed, onMounted, nextTick, watch } from 'vue'
30
- import type { VForm } from 'vuetify/components'
31
-
32
- const emit = defineEmits<{
33
- 'update:modelValue': [string | null]
34
- 'update:codeBudgetairesProp': [string[]]
35
- 'update:valide': [boolean] // 👈 nouveau
36
- }>()
37
-
38
- const props = withDefaults(
39
- defineProps<{
40
- codeBudgetairesProp: string[]
41
- modelValue: string | null
42
- afficherHint?: boolean
43
- regleMessageErreur: string
44
- format?: string
45
- activerExtension?: boolean
46
- reglesSupp?: ((v: string) => true | string)[]
47
- }>(),
48
- {
49
- afficherHint: false,
50
- format: '999-9-99999-999',
51
- activerExtension: false,
52
- reglesSupp: () => [],
53
- },
54
- )
55
-
56
- const formValide = ref(false)
57
- const form = ref<VForm | null>(null)
58
- const codeBudgetaire = ref(props.modelValue ?? '')
59
- const derniereValeurSauvegardee = ref<string | null>(null)
60
- const format = props.format
61
- const activerExtension = props.activerExtension
62
-
63
- onMounted(() => {
64
- derniereValeurSauvegardee.value = codeBudgetaire.value
65
- })
66
-
67
- const placeholder = computed(() => {
68
- const base = format.replace(/9/g, '0')
69
- const extension = activerExtension ? '-XXX/XXX' : ''
70
- return base + extension
71
- })
72
-
73
- const estValide = computed(() => {
74
- const val = codeBudgetaire.value?.toUpperCase().trim() || ''
75
- const base = val.slice(0, 15)
76
- const extension = val.slice(15)
77
-
78
- if (!/^\d{3}-\d{1}-\d{5}-\d{3}$/.test(base)) return false
79
-
80
- if (!activerExtension) return val.length === 15
81
-
82
- if (val.length === 15) return true
83
- if (val.length !== 22) return false
84
- if (extension.length !== 7) return false
85
-
86
- if (extension[3] !== '/') return false
87
- if (!/^[A-Z0-9/]$/i.test(extension[0]!)) return false
88
-
89
- const alphanumAt = [1, 2, 4, 5, 6]
90
- return alphanumAt.every(i => /^[A-Z0-9]$/i.test(extension[i]!))
91
- })
92
-
93
- // Règles Vuetify combinées (interne + supplémentaires)
94
- const reglesVuetify = computed(() => [
95
- // règle interne
96
- () => (estValide.value ? true : props.regleMessageErreur),
97
-
98
- // règles supplémentaires venant du parent
99
- ...props.reglesSupp.map(rule => {
100
- return () => rule(codeBudgetaire.value) // on passe la valeur formatée
101
- }),
102
- ])
103
-
104
- // Validité globale du composant (interne + toutes les règles supp)
105
- const estValideComplet = computed(() => {
106
- if (!estValide.value) return false
107
- return props.reglesSupp.every(rule => rule(codeBudgetaire.value) === true)
108
- })
109
-
110
- const caractereAutorises = (e: KeyboardEvent) => {
111
- if (e.ctrlKey || e.metaKey) return
112
-
113
- const touchesSpecifiques = ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Tab', 'Home', 'End']
114
- if (touchesSpecifiques.includes(e.key)) return
115
-
116
- const input = e.target as HTMLInputElement
117
- let position = input.selectionStart ?? 0
118
-
119
- // Gestion de la partie de base (15 premiers caractères)
120
- if (position < 15) {
121
- if (!/^\d$/.test(e.key)) {
122
- e.preventDefault()
123
- return
124
- }
125
-
126
- // Insérer chiffre et auto-ajout des tirets
127
- e.preventDefault()
128
-
129
- const value = codeBudgetaire.value.replace(/-/g, '')
130
- const clean = value.slice(0, 12) + e.key
131
-
132
- let formatted = ''
133
- if (clean.length > 0) formatted += clean.slice(0, 3)
134
- if (clean.length > 3) formatted += '-' + clean.slice(3, 4)
135
- if (clean.length > 4) formatted += '-' + clean.slice(4, 9)
136
- if (clean.length > 9) formatted += '-' + clean.slice(9, 12)
137
-
138
- codeBudgetaire.value = formatted.slice(0, 15)
139
-
140
- nextTick(() => {
141
- const newPos = codeBudgetaire.value.length
142
- input.selectionStart = input.selectionEnd = newPos
143
- })
144
-
145
- return
146
- }
147
-
148
- // --- Gestion de l'extension ---
149
- if (!activerExtension || position >= 22 || codeBudgetaire.value.length >= 22) {
150
- e.preventDefault()
151
- return
152
- }
153
-
154
- const extensionPos = position - 15
155
-
156
- // Règle 1 : extension[0] = alphanum ou /
157
- if (extensionPos === 0) {
158
- if (!/^[A-Z0-9/]$/i.test(e.key)) {
159
- e.preventDefault()
160
- return
161
- }
162
- return
163
- }
164
-
165
- // Règle 2 : extension[1,2,4,5,6] = alphanum
166
- if ([1, 2, 4, 5, 6].includes(extensionPos)) {
167
- if (!/^[A-Z0-9]$/i.test(e.key)) {
168
- e.preventDefault()
169
- return
170
- }
171
- return
172
- }
173
-
174
- // Règle 3 : slash automatique à position 3 (index 18)
175
- if (extensionPos === 3) {
176
- e.preventDefault()
177
- const before = codeBudgetaire.value.slice(0, position)
178
- const after = codeBudgetaire.value.slice(position)
179
- codeBudgetaire.value = before + '/' + after
180
- nextTick(() => {
181
- input.selectionStart = input.selectionEnd = position + 1
182
- })
183
- return
184
- }
185
-
186
- // Tout autre cas = bloqué
187
- e.preventDefault()
188
- }
189
-
190
- const formaterCodeBudgetaire = (valeur: string): string => {
191
- if (!valeur) return ''
192
-
193
- const upper = valeur.toUpperCase().replace(/[^A-Z0-9/]/g, '')
194
- const chiffres = upper.replace(/[^0-9]/g, '').slice(0, 12)
195
-
196
- let base = ''
197
- if (chiffres.length > 0) base += chiffres.slice(0, 3)
198
- if (chiffres.length > 3) base += '-' + chiffres.slice(3, 4)
199
- if (chiffres.length > 4) base += '-' + chiffres.slice(4, 9)
200
- if (chiffres.length > 9) base += '-' + chiffres.slice(9, 12)
201
-
202
- if (!activerExtension || base.length < 15) return base
203
-
204
- const reste = upper.slice(chiffres.length).replace(/[^A-Z0-9/]/gi, '')
205
-
206
- // Extraire les 7 premiers caractères restants pour l’extension
207
- let ext = reste.slice(0, 7).split('')
208
-
209
- // Ne garder que le premier slash s’il est à l’index 0 ou 3
210
- ext = ext.filter((c, i) => {
211
- if (c !== '/') return true
212
- return i === 0 || i === 3
213
- })
214
-
215
- // Enlever les slash non autorisés
216
- ext = ext.map((c, i) => {
217
- if (c === '/' && i !== 0 && i !== 3) return ''
218
- if (c !== '/' && !/^[A-Z0-9]$/i.test(c)) return ''
219
- return c
220
- })
221
-
222
- // Forcer le slash à la 4e position
223
- if (ext.length > 3) {
224
- ext[3] = '/'
225
- }
226
-
227
- // Réduire à 7 caractères
228
- ext = ext.slice(0, 7)
229
-
230
- return (base + ext.join('')).slice(0, 22)
231
- }
232
-
233
- const gererPaste = (e: ClipboardEvent) => {
234
- e.preventDefault()
235
- const clipboardData = e.clipboardData
236
- if (!clipboardData) return
237
- let pasted = clipboardData.getData('text') || ''
238
- codeBudgetaire.value = formaterCodeBudgetaire(pasted)
239
-
240
- setTimeout(() => {
241
- const input = e.target as HTMLInputElement
242
- input.selectionStart = input.selectionEnd = codeBudgetaire.value.length
243
- }, 0)
244
- }
245
-
246
- const sauvegarder = () => {
247
- codeBudgetaire.value = formaterCodeBudgetaire(codeBudgetaire.value)
248
-
249
- // on utilise la validité globale
250
- if (!estValideComplet.value) return
251
- if (codeBudgetaire.value === derniereValeurSauvegardee.value) return
252
-
253
- const existe = props.codeBudgetairesProp.some(
254
- item => item.trim().toUpperCase() === codeBudgetaire.value.trim().toUpperCase(),
255
- )
256
-
257
- if (!existe) {
258
- const nouvelleListe = [...props.codeBudgetairesProp, codeBudgetaire.value]
259
- emit('update:codeBudgetairesProp', nouvelleListe)
260
- }
261
-
262
- derniereValeurSauvegardee.value = codeBudgetaire.value
263
- emit('update:modelValue', codeBudgetaire.value)
264
- }
265
-
266
- const gererChangement = (val: string) => {
267
- codeBudgetaire.value = formaterCodeBudgetaire(val)
268
-
269
- const valeurFormatee = codeBudgetaire.value
270
- const estDansListe = props.codeBudgetairesProp.includes(valeurFormatee)
271
-
272
- if (estDansListe && valeurFormatee !== derniereValeurSauvegardee.value && estValideComplet.value) {
273
- sauvegarder()
274
- }
275
- }
276
-
277
- watch(estValideComplet, value => {
278
- emit('update:valide', value)
279
- })
280
- </script>
1
+ <template>
2
+ <div>
3
+ <v-form
4
+ ref="form"
5
+ v-model="formValide"
6
+ @submit.prevent
7
+ >
8
+ <v-combobox
9
+ v-model="codeBudgetaire"
10
+ :items="itemsCombobox"
11
+ item-value="code"
12
+ item-title="code"
13
+ :return-object="false"
14
+ v-bind="$attrs"
15
+ persistent-hint
16
+ variant="outlined"
17
+ hide-details="auto"
18
+ :error="!estValideComplet"
19
+ :rules="reglesVuetify"
20
+ :hint="afficherHint ? placeholder : ''"
21
+ @blur="sauvegarder"
22
+ @keydown.enter="sauvegarder"
23
+ @keydown="caractereAutorises"
24
+ @update:modelValue="gererChangement"
25
+ @paste="gererPaste"
26
+ >
27
+ <template #item="{ props, item }">
28
+ <v-list-item
29
+ v-bind="props"
30
+ :title="item.raw.nom || item.raw.code"
31
+ :subtitle="item.raw.nom ? item.raw.code : undefined"
32
+ />
33
+ </template>
34
+ </v-combobox>
35
+ </v-form>
36
+ </div>
37
+ </template>
38
+
39
+ <script setup lang="ts">
40
+ import { ref, computed, onMounted, nextTick, watch } from 'vue'
41
+ import type { VForm } from 'vuetify/components'
42
+
43
+ const emit = defineEmits<{
44
+ 'update:modelValue': [string | null]
45
+ 'update:codeBudgetairesProp': [CodeBudgetaireItem[]]
46
+ 'update:valide': [boolean]
47
+ }>()
48
+ type CodeBudgetaireItem = string | [string, string]
49
+
50
+ const props = withDefaults(
51
+ defineProps<{
52
+ codeBudgetairesProp: CodeBudgetaireItem[]
53
+ modelValue: string | null
54
+ afficherHint?: boolean
55
+ regleMessageErreur: string
56
+ format?: string
57
+ activerExtension?: boolean
58
+ reglesSupp?: ((v: string) => true | string)[]
59
+ }>(),
60
+ {
61
+ afficherHint: false,
62
+ format: '999-9-99999-999',
63
+ activerExtension: false,
64
+ reglesSupp: () => [],
65
+ },
66
+ )
67
+
68
+ const formValide = ref(false)
69
+ const form = ref<VForm | null>(null)
70
+ const codeBudgetaire = ref(props.modelValue ?? '')
71
+ const derniereValeurSauvegardee = ref<string | null>(null)
72
+ const format = props.format
73
+ const activerExtension = props.activerExtension
74
+
75
+ onMounted(() => {
76
+ derniereValeurSauvegardee.value = codeBudgetaire.value
77
+ })
78
+
79
+ const itemsCombobox = computed(() => {
80
+ return props.codeBudgetairesProp.map(item => {
81
+ if (typeof item === 'string') {
82
+ return {
83
+ code: item,
84
+ nom: '', // pas de nom
85
+ }
86
+ }
87
+
88
+ const [code, nomBrut] = item
89
+ const nom = (nomBrut ?? '').toString().trim()
90
+
91
+ return { code, nom }
92
+ })
93
+ })
94
+
95
+ const placeholder = computed(() => {
96
+ const base = format.replace(/9/g, '0')
97
+ const extension = activerExtension ? '-XXX/XXX' : ''
98
+ return base + extension
99
+ })
100
+
101
+ const estValide = computed(() => {
102
+ const val = codeBudgetaire.value?.toUpperCase().trim() || ''
103
+ const base = val.slice(0, 15)
104
+ const extension = val.slice(15)
105
+
106
+ if (!/^\d{3}-\d{1}-\d{5}-\d{3}$/.test(base)) return false
107
+
108
+ if (!activerExtension) return val.length === 15
109
+
110
+ if (val.length === 15) return true
111
+ if (val.length !== 22) return false
112
+ if (extension.length !== 7) return false
113
+
114
+ if (extension[3] !== '/') return false
115
+ if (!/^[A-Z0-9/]$/i.test(extension[0]!)) return false
116
+
117
+ const alphanumAt = [1, 2, 4, 5, 6]
118
+ return alphanumAt.every(i => /^[A-Z0-9]$/i.test(extension[i]!))
119
+ })
120
+
121
+ // Règles Vuetify combinées (interne + supplémentaires)
122
+ const reglesVuetify = computed(() => [
123
+ // règle interne
124
+ () => (estValide.value ? true : props.regleMessageErreur),
125
+
126
+ // règles supplémentaires venant du parent
127
+ ...props.reglesSupp.map(rule => {
128
+ return () => rule(codeBudgetaire.value) // on passe la valeur formatée
129
+ }),
130
+ ])
131
+
132
+ // Validité globale du composant (interne + toutes les règles supp)
133
+ const estValideComplet = computed(() => {
134
+ if (!estValide.value) return false
135
+ return props.reglesSupp.every(rule => rule(codeBudgetaire.value) === true)
136
+ })
137
+
138
+ const caractereAutorises = (e: KeyboardEvent) => {
139
+ if (e.ctrlKey || e.metaKey) return
140
+
141
+ const touchesSpecifiques = ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Tab', 'Home', 'End']
142
+ if (touchesSpecifiques.includes(e.key)) return
143
+
144
+ const input = e.target as HTMLInputElement
145
+ let position = input.selectionStart ?? 0
146
+
147
+ // Gestion de la partie de base (15 premiers caractères)
148
+ if (position < 15) {
149
+ if (!/^\d$/.test(e.key)) {
150
+ e.preventDefault()
151
+ return
152
+ }
153
+
154
+ // Insérer chiffre et auto-ajout des tirets
155
+ e.preventDefault()
156
+
157
+ const value = codeBudgetaire.value.replace(/-/g, '')
158
+ const clean = value.slice(0, 12) + e.key
159
+
160
+ let formatted = ''
161
+ if (clean.length > 0) formatted += clean.slice(0, 3)
162
+ if (clean.length > 3) formatted += '-' + clean.slice(3, 4)
163
+ if (clean.length > 4) formatted += '-' + clean.slice(4, 9)
164
+ if (clean.length > 9) formatted += '-' + clean.slice(9, 12)
165
+
166
+ codeBudgetaire.value = formatted.slice(0, 15)
167
+
168
+ nextTick(() => {
169
+ const newPos = codeBudgetaire.value.length
170
+ input.selectionStart = input.selectionEnd = newPos
171
+ })
172
+
173
+ return
174
+ }
175
+
176
+ // --- Gestion de l'extension ---
177
+ if (!activerExtension || position >= 22 || codeBudgetaire.value.length >= 22) {
178
+ e.preventDefault()
179
+ return
180
+ }
181
+
182
+ const extensionPos = position - 15
183
+
184
+ // Règle 1 : extension[0] = alphanum ou /
185
+ if (extensionPos === 0) {
186
+ if (!/^[A-Z0-9/]$/i.test(e.key)) {
187
+ e.preventDefault()
188
+ return
189
+ }
190
+ return
191
+ }
192
+
193
+ // Règle 2 : extension[1,2,4,5,6] = alphanum
194
+ if ([1, 2, 4, 5, 6].includes(extensionPos)) {
195
+ if (!/^[A-Z0-9]$/i.test(e.key)) {
196
+ e.preventDefault()
197
+ return
198
+ }
199
+ return
200
+ }
201
+
202
+ // Règle 3 : slash automatique à position 3 (index 18)
203
+ if (extensionPos === 3) {
204
+ e.preventDefault()
205
+ const before = codeBudgetaire.value.slice(0, position)
206
+ const after = codeBudgetaire.value.slice(position)
207
+ codeBudgetaire.value = before + '/' + after
208
+ nextTick(() => {
209
+ input.selectionStart = input.selectionEnd = position + 1
210
+ })
211
+ return
212
+ }
213
+
214
+ // Tout autre cas = bloqué
215
+ e.preventDefault()
216
+ }
217
+
218
+ const formaterCodeBudgetaire = (valeur: string): string => {
219
+ if (!valeur) return ''
220
+
221
+ const upper = valeur.toUpperCase().replace(/[^A-Z0-9/]/g, '')
222
+ const chiffres = upper.replace(/[^0-9]/g, '').slice(0, 12)
223
+
224
+ let base = ''
225
+ if (chiffres.length > 0) base += chiffres.slice(0, 3)
226
+ if (chiffres.length > 3) base += '-' + chiffres.slice(3, 4)
227
+ if (chiffres.length > 4) base += '-' + chiffres.slice(4, 9)
228
+ if (chiffres.length > 9) base += '-' + chiffres.slice(9, 12)
229
+
230
+ if (!activerExtension || base.length < 15) return base
231
+
232
+ const reste = upper.slice(chiffres.length).replace(/[^A-Z0-9/]/gi, '')
233
+
234
+ // Extraire les 7 premiers caractères restants pour l’extension
235
+ let ext = reste.slice(0, 7).split('')
236
+
237
+ // Ne garder que le premier slash s’il est à l’index 0 ou 3
238
+ ext = ext.filter((c, i) => {
239
+ if (c !== '/') return true
240
+ return i === 0 || i === 3
241
+ })
242
+
243
+ // Enlever les slash non autorisés
244
+ ext = ext.map((c, i) => {
245
+ if (c === '/' && i !== 0 && i !== 3) return ''
246
+ if (c !== '/' && !/^[A-Z0-9]$/i.test(c)) return ''
247
+ return c
248
+ })
249
+
250
+ // Forcer le slash à la 4e position
251
+ if (ext.length > 3) {
252
+ ext[3] = '/'
253
+ }
254
+
255
+ // Réduire à 7 caractères
256
+ ext = ext.slice(0, 7)
257
+
258
+ return (base + ext.join('')).slice(0, 22)
259
+ }
260
+
261
+ const gererPaste = (e: ClipboardEvent) => {
262
+ e.preventDefault()
263
+ const clipboardData = e.clipboardData
264
+ if (!clipboardData) return
265
+ let pasted = clipboardData.getData('text') || ''
266
+ codeBudgetaire.value = formaterCodeBudgetaire(pasted)
267
+
268
+ setTimeout(() => {
269
+ const input = e.target as HTMLInputElement
270
+ input.selectionStart = input.selectionEnd = codeBudgetaire.value.length
271
+ }, 0)
272
+ }
273
+
274
+ const sauvegarder = () => {
275
+ codeBudgetaire.value = formaterCodeBudgetaire(codeBudgetaire.value)
276
+
277
+ if (!estValideComplet.value) return
278
+ if (codeBudgetaire.value === derniereValeurSauvegardee.value) return
279
+
280
+ const codeNormalise = codeBudgetaire.value.trim().toUpperCase()
281
+
282
+ const existe = props.codeBudgetairesProp.some(item => {
283
+ const code = typeof item === 'string' ? item : item[0]
284
+ return code.trim().toUpperCase() === codeNormalise
285
+ })
286
+
287
+ if (!existe) {
288
+ const nouvelleListe = [...props.codeBudgetairesProp, codeBudgetaire.value]
289
+ emit('update:codeBudgetairesProp', nouvelleListe)
290
+ }
291
+
292
+ derniereValeurSauvegardee.value = codeBudgetaire.value
293
+ emit('update:modelValue', codeBudgetaire.value)
294
+ }
295
+
296
+ const extraireCode = (val: unknown): string => {
297
+ if (val == null) return ''
298
+ if (typeof val === 'string') return val
299
+
300
+ // Cas où Vuetify enverrait { code, label }
301
+ if (typeof val === 'object' && 'code' in (val as Record<string, unknown>)) {
302
+ const obj = val as { code?: unknown }
303
+ return typeof obj.code === 'string' ? obj.code : String(obj.code ?? '')
304
+ }
305
+
306
+ return String(val)
307
+ }
308
+
309
+ const gererChangement = (val: unknown) => {
310
+ const code = extraireCode(val)
311
+ codeBudgetaire.value = formaterCodeBudgetaire(code)
312
+
313
+ const valeurFormatee = codeBudgetaire.value.trim().toUpperCase()
314
+
315
+ const estDansListe = props.codeBudgetairesProp.some(item => {
316
+ const codeItem = typeof item === 'string' ? item : item[0]
317
+ return codeItem.trim().toUpperCase() === valeurFormatee
318
+ })
319
+
320
+ if (
321
+ estDansListe &&
322
+ valeurFormatee !== (derniereValeurSauvegardee.value ?? '').toUpperCase() &&
323
+ estValideComplet.value
324
+ ) {
325
+ sauvegarder()
326
+ }
327
+ }
328
+
329
+ watch(estValideComplet, value => {
330
+ emit('update:valide', value)
331
+ })
332
+ </script>