@m3ui-vue/m3ui-vue 1.6.2 → 1.7.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.
Files changed (163) hide show
  1. package/dist/API.md +649 -28
  2. package/dist/{MButton-OQ4wIERQ.js → MButton-CN3wYwIG.js} +2 -2
  3. package/dist/{MButton-OQ4wIERQ.js.map → MButton-CN3wYwIG.js.map} +1 -1
  4. package/dist/{MColorPickerModal-NtkihoK6.js → MColorPickerModal-BKM1CHxr.js} +4 -4
  5. package/dist/{MColorPickerModal-NtkihoK6.js.map → MColorPickerModal-BKM1CHxr.js.map} +1 -1
  6. package/dist/{MDialog-DYMnaBlt.js → MDialog-1LAk9WAO.js} +2 -2
  7. package/dist/{MDialog-DYMnaBlt.js.map → MDialog-1LAk9WAO.js.map} +1 -1
  8. package/dist/{MListItem-D2cNUyzP.js → MListItem-BU5KDRCB.js} +376 -270
  9. package/dist/MListItem-BU5KDRCB.js.map +1 -0
  10. package/dist/{MLoadingIndicator-DWUj0BAB.js → MLoadingIndicator-RykOo5tf.js} +2 -2
  11. package/dist/{MLoadingIndicator-DWUj0BAB.js.map → MLoadingIndicator-RykOo5tf.js.map} +1 -1
  12. package/dist/{MSeekBar-C4cn9RIF.js → MSeekBar-B_Y3MpVz.js} +2 -2
  13. package/dist/{MSeekBar-C4cn9RIF.js.map → MSeekBar-B_Y3MpVz.js.map} +1 -1
  14. package/dist/MSegmentedButton-CiOOY1-N.js +105 -0
  15. package/dist/MSegmentedButton-CiOOY1-N.js.map +1 -0
  16. package/dist/MSlider-BCeVIxcq.js +739 -0
  17. package/dist/MSlider-BCeVIxcq.js.map +1 -0
  18. package/dist/{MSpinner-DZkXZJpc.js → MSpinner-Cs03qoqE.js} +2 -2
  19. package/dist/{MSpinner-DZkXZJpc.js.map → MSpinner-Cs03qoqE.js.map} +1 -1
  20. package/dist/{MTextField-CNH-byir.js → MTextField-NaFbMkjv.js} +162 -115
  21. package/dist/MTextField-NaFbMkjv.js.map +1 -0
  22. package/dist/_useFieldValidation-Bz34IAQY.js +191 -0
  23. package/dist/_useFieldValidation-Bz34IAQY.js.map +1 -0
  24. package/dist/api.json +1 -1
  25. package/dist/code-editor.js +184 -103
  26. package/dist/code-editor.js.map +1 -1
  27. package/dist/components/MAddressField.vue.d.ts +18 -3
  28. package/dist/components/MAppBarWithSearch.vue.d.ts +1 -1
  29. package/dist/components/MAudioRecorder.vue.d.ts +2 -2
  30. package/dist/components/MAutocomplete.vue.d.ts +22 -3
  31. package/dist/components/MCheckbox.vue.d.ts +23 -1
  32. package/dist/components/MCodeEditor.vue.d.ts +23 -1
  33. package/dist/components/MColorPicker.vue.d.ts +24 -3
  34. package/dist/components/MConfirmDialog.vue.d.ts +2 -2
  35. package/dist/components/MCountrySelect.vue.d.ts +13 -2
  36. package/dist/components/MCreditCardInput.vue.d.ts +33 -0
  37. package/dist/components/MCronField.vue.d.ts +23 -1
  38. package/dist/components/MDataTable.vue.d.ts +1 -1
  39. package/dist/components/MDatePicker.vue.d.ts +24 -3
  40. package/dist/components/MDateRangePicker.vue.d.ts +24 -3
  41. package/dist/components/MFileUpload.vue.d.ts +23 -2
  42. package/dist/components/MForm.vue.d.ts +61 -0
  43. package/dist/components/MJsonEditor.vue.d.ts +23 -1
  44. package/dist/components/MMapRoute.vue.d.ts +168 -0
  45. package/dist/components/MMaskField.vue.d.ts +20 -2
  46. package/dist/components/MMentionField.vue.d.ts +13 -2
  47. package/dist/components/MMultiAutocomplete.vue.d.ts +22 -3
  48. package/dist/components/MMultiSelect.vue.d.ts +22 -3
  49. package/dist/components/MNavigationRail.vue.d.ts +1 -1
  50. package/dist/components/MNotificationCenter.vue.d.ts +2 -2
  51. package/dist/components/MNumberField.vue.d.ts +20 -2
  52. package/dist/components/MPhoneInput.vue.d.ts +19 -4
  53. package/dist/components/MPinInput.vue.d.ts +24 -3
  54. package/dist/components/MPosKeypad.vue.d.ts +2 -2
  55. package/dist/components/MRadioGroup.vue.d.ts +23 -1
  56. package/dist/components/MRating.vue.d.ts +23 -1
  57. package/dist/components/MRichTextEditor.vue.d.ts +23 -1
  58. package/dist/components/MSearchBar.vue.d.ts +1 -1
  59. package/dist/components/MSegmentedButton.vue.d.ts +23 -2
  60. package/dist/components/MSelect.vue.d.ts +22 -3
  61. package/dist/components/MSignaturePad.vue.d.ts +19 -0
  62. package/dist/components/MSlider.vue.d.ts +23 -1
  63. package/dist/components/MSwitch.vue.d.ts +23 -1
  64. package/dist/components/MTable.vue.d.ts +1 -1
  65. package/dist/components/MTagInput.vue.d.ts +20 -2
  66. package/dist/components/MText.vue.d.ts +1 -1
  67. package/dist/components/MTextField.vue.d.ts +26 -3
  68. package/dist/components/MTimePicker.vue.d.ts +24 -3
  69. package/dist/components/MTimezoneSelect.vue.d.ts +13 -2
  70. package/dist/components/MTour.vue.d.ts +2 -2
  71. package/dist/components/MTrackingTimeline.vue.d.ts +75 -0
  72. package/dist/components/MWindow.vue.d.ts +2 -2
  73. package/dist/composables/_routeGeometry.d.ts +26 -0
  74. package/dist/composables/_useFieldValidation.d.ts +91 -0
  75. package/dist/composables/mapRouting.d.ts +35 -0
  76. package/dist/composables/useLocale.d.ts +23 -0
  77. package/dist/composables/validation.d.ts +82 -0
  78. package/dist/draw.js +2 -2
  79. package/dist/flow.js +1 -1
  80. package/dist/gantt.js +2 -2
  81. package/dist/index.d.ts +5 -0
  82. package/dist/locales/index.js +154 -7
  83. package/dist/locales/index.js.map +1 -1
  84. package/dist/m3ui-vue.css +1 -1
  85. package/dist/m3ui.js +6768 -5602
  86. package/dist/m3ui.js.map +1 -1
  87. package/dist/map.d.ts +4 -0
  88. package/dist/map.js +1230 -784
  89. package/dist/map.js.map +1 -1
  90. package/dist/media.js +4 -4
  91. package/dist/pdf.js +4 -4
  92. package/dist/rich-text-editor.js +189 -146
  93. package/dist/rich-text-editor.js.map +1 -1
  94. package/dist/styles.css +1 -1
  95. package/dist/{useLocale-DaVSTdm0.js → useLocale-Dh1xZZ8i.js} +22 -1
  96. package/dist/useLocale-Dh1xZZ8i.js.map +1 -0
  97. package/package.json +1 -1
  98. package/src/__tests__/MForm.test.ts +325 -0
  99. package/src/__tests__/MMapRoute.test.ts +384 -0
  100. package/src/__tests__/MPhoneInput.test.ts +5 -2
  101. package/src/__tests__/MRichTextEditor.test.ts +2 -1
  102. package/src/__tests__/MTrackingTimeline.test.ts +66 -0
  103. package/src/__tests__/_fakeMaplibre.ts +4 -1
  104. package/src/__tests__/_osrmRoutes.ts +10 -0
  105. package/src/__tests__/attrsFallthrough.test.ts +11 -0
  106. package/src/__tests__/fieldsValidation.test.ts +229 -0
  107. package/src/__tests__/ssr.test.ts +1 -0
  108. package/src/__tests__/validation.test.ts +77 -0
  109. package/src/components/MAddressField.vue +45 -8
  110. package/src/components/MAutocomplete.vue +69 -23
  111. package/src/components/MCheckbox.vue +80 -17
  112. package/src/components/MCodeEditor.vue +59 -2
  113. package/src/components/MColorPicker.vue +65 -16
  114. package/src/components/MCountrySelect.vue +17 -3
  115. package/src/components/MCreditCardInput.vue +76 -7
  116. package/src/components/MCronField.vue +60 -10
  117. package/src/components/MDatePicker.vue +65 -16
  118. package/src/components/MDateRangePicker.vue +66 -16
  119. package/src/components/MFileUpload.vue +71 -8
  120. package/src/components/MForm.vue +208 -0
  121. package/src/components/MJsonEditor.vue +57 -3
  122. package/src/components/MMapRoute.vue +470 -0
  123. package/src/components/MMaskField.vue +55 -13
  124. package/src/components/MMentionField.vue +17 -2
  125. package/src/components/MMultiAutocomplete.vue +70 -23
  126. package/src/components/MMultiSelect.vue +70 -23
  127. package/src/components/MNumberField.vue +58 -16
  128. package/src/components/MPhoneInput.vue +60 -18
  129. package/src/components/MPinInput.vue +58 -10
  130. package/src/components/MRadioGroup.vue +65 -4
  131. package/src/components/MRating.vue +62 -6
  132. package/src/components/MRichTextEditor.vue +64 -4
  133. package/src/components/MSegmentedButton.vue +59 -2
  134. package/src/components/MSelect.vue +72 -26
  135. package/src/components/MSignaturePad.vue +71 -6
  136. package/src/components/MSlider.vue +61 -10
  137. package/src/components/MSwitch.vue +63 -8
  138. package/src/components/MTagInput.vue +67 -25
  139. package/src/components/MTextField.vue +68 -14
  140. package/src/components/MTimePicker.vue +65 -16
  141. package/src/components/MTimezoneSelect.vue +17 -3
  142. package/src/components/MTrackingTimeline.vue +257 -0
  143. package/src/composables/_routeGeometry.ts +140 -0
  144. package/src/composables/_useFieldValidation.ts +287 -0
  145. package/src/composables/mapRouting.ts +55 -0
  146. package/src/composables/useLocale.ts +46 -0
  147. package/src/composables/validation.ts +178 -0
  148. package/src/index.ts +5 -0
  149. package/src/locales/de.ts +21 -0
  150. package/src/locales/es.ts +28 -7
  151. package/src/locales/fr.ts +21 -0
  152. package/src/locales/ja.ts +21 -0
  153. package/src/locales/ko.ts +21 -0
  154. package/src/locales/pt.ts +21 -0
  155. package/src/locales/zh.ts +21 -0
  156. package/src/map.ts +4 -0
  157. package/dist/MListItem-D2cNUyzP.js.map +0 -1
  158. package/dist/MSegmentedButton-BEB4gXLA.js +0 -64
  159. package/dist/MSegmentedButton-BEB4gXLA.js.map +0 -1
  160. package/dist/MSlider-BIkA6DiE.js +0 -690
  161. package/dist/MSlider-BIkA6DiE.js.map +0 -1
  162. package/dist/MTextField-CNH-byir.js.map +0 -1
  163. package/dist/useLocale-DaVSTdm0.js.map +0 -1
@@ -6,6 +6,8 @@
6
6
  * @event debounced Emitted debounce ms after the user stops typing the search query (only when debounce > 0)
7
7
  */
8
8
  import { computed, ref, useId, onMounted, onUnmounted, watch, nextTick } from 'vue'
9
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
10
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
9
11
  import MIcon from './MIcon.vue'
10
12
  import MIconButton from './MIconButton.vue'
11
13
  import MCheckbox from './MCheckbox.vue'
@@ -38,10 +40,21 @@ const props = withDefaults(
38
40
  mode?: 'docked' | 'modal'
39
41
  /** Disables the field */
40
42
  disabled?: boolean
41
- /** Error state */
42
- error?: boolean
43
+ /** Error state. A string is also the message (`:error="errors.x"`) */
44
+ error?: boolean | string
43
45
  /** Error message shown when error is true */
44
46
  errorLabel?: string
47
+ /**
48
+ * Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
49
+ * `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
50
+ */
51
+ rules?: ValidationRule | ValidationRule[]
52
+ /** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
53
+ validateOn?: ValidateOn
54
+ /** Wait this long (ms) after the value stops changing before re-checking (one request for an async rule, not one per key). Defaults to its `MForm`'s */
55
+ validateDebounce?: number
56
+ /** The key of this field in its `MForm`'s values, errors and schema */
57
+ name?: string
45
58
  /** Helper text below field */
46
59
  hint?: string
47
60
  /** Marks as required — adds a red asterisk after the label */
@@ -76,7 +89,26 @@ const props = withDefaults(
76
89
  },
77
90
  )
78
91
 
79
- const emit = defineEmits<{ 'update:modelValue': [unknown[]]; debounced: [string] }>()
92
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [unknown[]]; debounced: [string] }>()
93
+
94
+ const rootEl = ref<HTMLElement | null>(null)
95
+ const validation = useFieldValidation({
96
+ value: () => props.modelValue,
97
+ rules: () => props.rules,
98
+ required: () => props.required,
99
+ error: () => props.error,
100
+ errorLabel: () => props.errorLabel,
101
+ name: () => props.name,
102
+ validateOn: () => props.validateOn,
103
+ validateDebounce: () => props.validateDebounce,
104
+ disabled: () => props.disabled,
105
+ focus: () => { inputEl.value?.focus() },
106
+ el: () => inputEl.value,
107
+ setValue: (v) => emit('update:modelValue', v as unknown[]),
108
+ })
109
+ /** Its own `disabled` or its MForm's */
110
+ const isDisabled = validation.disabled
111
+ const { hasError, message: errorMessage, messageId } = validation
80
112
 
81
113
  const locale = useLocale()
82
114
  const id = useId()
@@ -169,7 +201,7 @@ function toggle(value: unknown) {
169
201
 
170
202
  function removeChip(value: unknown, e: Event) {
171
203
  e.stopPropagation()
172
- if (props.disabled) return
204
+ if (isDisabled.value) return
173
205
  emit('update:modelValue', props.modelValue.filter((v) => !eq(v, value)))
174
206
  nextTick(() => inputEl.value?.focus())
175
207
  }
@@ -198,7 +230,7 @@ const modalFilteredOptions = computed(() => {
198
230
  })
199
231
 
200
232
  function openDropdown() {
201
- if (props.disabled || open.value) return
233
+ if (isDisabled.value || open.value) return
202
234
  if (props.mode === 'modal') {
203
235
  modalOpen.value = true
204
236
  modalSearch.value = ''
@@ -332,20 +364,20 @@ const triggerClasses = computed(() => {
332
364
  ]
333
365
 
334
366
  if (props.variant === 'outlined') {
335
- const border = props.disabled
367
+ const border = isDisabled.value
336
368
  ? 'border-on-surface/12'
337
369
  : open.value
338
- ? (props.error ? 'border-2 border-error' : 'border-2 border-primary')
339
- : (props.error ? 'border-error' : 'border-outline hover:border-on-surface')
370
+ ? (hasError.value ? 'border-2 border-error' : 'border-2 border-primary')
371
+ : (hasError.value ? 'border-error' : 'border-outline hover:border-on-surface')
340
372
  return [...base, 'rounded-xs border bg-transparent py-2', border].join(' ')
341
373
  }
342
374
 
343
- const border = props.disabled
375
+ const border = isDisabled.value
344
376
  ? 'border-on-surface/38'
345
377
  : open.value
346
- ? (props.error ? 'border-b-2 border-error' : 'border-b-2 border-primary')
347
- : (props.error ? 'border-error' : 'border-on-surface-variant hover:border-on-surface')
348
- const bg = props.disabled
378
+ ? (hasError.value ? 'border-b-2 border-error' : 'border-b-2 border-primary')
379
+ : (hasError.value ? 'border-error' : 'border-on-surface-variant hover:border-on-surface')
380
+ const bg = isDisabled.value
349
381
  ? 'bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]'
350
382
  : 'bg-surface-container-highest'
351
383
  return [...base, 'rounded-t-xs border-b pb-2', isFloated.value ? 'pt-7' : 'pt-4', bg, border].join(' ')
@@ -357,17 +389,30 @@ const labelClasses = computed(() =>
357
389
  leading: !!props.leadingIcon,
358
390
  right: rightInset.value,
359
391
  floated: isFloated.value,
360
- color: props.disabled
392
+ color: isDisabled.value
361
393
  ? 'text-on-surface/38'
362
394
  : open.value
363
- ? (props.error ? 'text-error' : 'text-primary')
364
- : (props.error ? 'text-error' : 'text-on-surface-variant'),
395
+ ? (hasError.value ? 'text-error' : 'text-primary')
396
+ : (hasError.value ? 'text-error' : 'text-on-surface-variant'),
365
397
  }),
366
398
  )
399
+
400
+ defineExpose({
401
+ /** Checks its rules and shows the result; resolves to whether they pass */
402
+ validate: validation.validate,
403
+ /** Hides its validation error (until the next check) */
404
+ resetValidation: validation.resetValidation,
405
+ })
406
+
407
+ // Last: it watches `open`, declared above
408
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
409
+ validation.onBlur()
410
+ emit('blur')
411
+ }, () => open.value)
367
412
  </script>
368
413
 
369
414
  <template>
370
- <div class="flex flex-col gap-1" v-bind="$attrs">
415
+ <div ref="rootEl" class="flex flex-col gap-1" v-bind="$attrs" @focusout="onFocusOut">
371
416
  <div
372
417
  ref="fieldEl"
373
418
  class="relative"
@@ -380,7 +425,7 @@ const labelClasses = computed(() =>
380
425
  class="pointer-events-none absolute left-3.5"
381
426
  :class="[
382
427
  variant === 'filled' ? 'top-4.5' : 'top-4',
383
- disabled ? 'text-on-surface/38' : 'text-on-surface-variant',
428
+ isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant',
384
429
  ]"
385
430
  >
386
431
  <MIcon :name="leadingIcon" :size="24" />
@@ -390,7 +435,7 @@ const labelClasses = computed(() =>
390
435
  <div
391
436
  :class="[
392
437
  triggerClasses,
393
- disabled ? 'pointer-events-none' : 'cursor-text',
438
+ isDisabled ? 'pointer-events-none' : 'cursor-text',
394
439
  ]"
395
440
  @click="inputEl?.focus()"
396
441
  >
@@ -399,12 +444,12 @@ const labelClasses = computed(() =>
399
444
  v-for="(chip, i) in displayChips"
400
445
  :key="i"
401
446
  class="inline-flex max-w-full min-w-0 items-center gap-1 rounded-full px-2 py-0.5 text-label-small"
402
- :class="disabled ? 'bg-on-surface/12 text-on-surface/38' : 'bg-secondary-container text-on-secondary-container'"
447
+ :class="isDisabled ? 'bg-on-surface/12 text-on-surface/38' : 'bg-secondary-container text-on-secondary-container'"
403
448
  >
404
449
  <span v-overflow-title class="min-w-0 truncate">{{ chip.label }}</span>
405
450
  <button
406
451
  :aria-label="`${locale.remove} ${chip.label}`"
407
- v-if="!disabled"
452
+ v-if="!isDisabled"
408
453
  type="button"
409
454
  class="flex h-4 w-4 shrink-0 items-center justify-center rounded-full hover:bg-on-secondary-container/20"
410
455
  @click="removeChip(chip.value, $event)"
@@ -432,6 +477,8 @@ const labelClasses = computed(() =>
432
477
  <input
433
478
  :id="id"
434
479
  ref="inputEl"
480
+ :aria-invalid="hasError || undefined"
481
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
435
482
  type="text"
436
483
  :value="search"
437
484
  :placeholder="!label && !hasValue ? placeholder : undefined"
@@ -444,7 +491,7 @@ const labelClasses = computed(() =>
444
491
  :aria-disabled="disabled"
445
492
  :class="[
446
493
  'flex-1 min-w-[60px] bg-transparent text-body-large outline-none placeholder:text-on-surface-variant',
447
- error ? 'caret-error' : 'caret-primary',
494
+ hasError ? 'caret-error' : 'caret-primary',
448
495
  disabled ? 'text-on-surface/38' : 'text-on-surface',
449
496
  ]"
450
497
  @focus="onInputFocus"
@@ -484,8 +531,8 @@ const labelClasses = computed(() =>
484
531
  </div>
485
532
  </div>
486
533
 
487
- <p v-if="error && errorLabel" class="px-4 text-body-small text-error">{{ errorLabel }}</p>
488
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
534
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
535
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
489
536
  </div>
490
537
 
491
538
  <!-- Docked dropdown -->
@@ -6,6 +6,8 @@
6
6
  * @event update:modelValue Emitted with the new array of selected values.
7
7
  */
8
8
  import { computed, ref, useId, onMounted, onUnmounted, nextTick, watch } from 'vue'
9
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
10
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
9
11
  import MIcon from './MIcon.vue'
10
12
  import MIconButton from './MIconButton.vue'
11
13
  import MCheckbox from './MCheckbox.vue'
@@ -43,10 +45,21 @@ const props = withDefaults(
43
45
  mode?: 'docked' | 'modal'
44
46
  /** Disables the field */
45
47
  disabled?: boolean
46
- /** Error state */
47
- error?: boolean
48
+ /** Error state. A string is also the message (`:error="errors.x"`) */
49
+ error?: boolean | string
48
50
  /** Error message shown when error is true */
49
51
  errorLabel?: string
52
+ /**
53
+ * Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
54
+ * `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
55
+ */
56
+ rules?: ValidationRule | ValidationRule[]
57
+ /** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
58
+ validateOn?: ValidateOn
59
+ /** Wait this long (ms) after the value stops changing before re-checking (one request for an async rule, not one per key). Defaults to its `MForm`'s */
60
+ validateDebounce?: number
61
+ /** The key of this field in its `MForm`'s values, errors and schema */
62
+ name?: string
50
63
  /** Hint text below the field */
51
64
  hint?: string
52
65
  /** Marks as required — adds a red asterisk after the label */
@@ -83,7 +96,26 @@ const props = withDefaults(
83
96
  },
84
97
  )
85
98
 
86
- const emit = defineEmits<{ 'update:modelValue': [unknown[]] }>()
99
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [unknown[]] }>()
100
+
101
+ const rootEl = ref<HTMLElement | null>(null)
102
+ const validation = useFieldValidation({
103
+ value: () => props.modelValue,
104
+ rules: () => props.rules,
105
+ required: () => props.required,
106
+ error: () => props.error,
107
+ errorLabel: () => props.errorLabel,
108
+ name: () => props.name,
109
+ validateOn: () => props.validateOn,
110
+ validateDebounce: () => props.validateDebounce,
111
+ disabled: () => props.disabled,
112
+ focus: () => { fieldEl.value?.querySelector<HTMLElement>('[tabindex]')?.focus() },
113
+ el: () => fieldEl.value,
114
+ setValue: (v) => emit('update:modelValue', v as unknown[]),
115
+ })
116
+ /** Its own `disabled` or its MForm's */
117
+ const isDisabled = validation.disabled
118
+ const { hasError, message: errorMessage, messageId } = validation
87
119
 
88
120
  const id = useId()
89
121
  const open = ref(false)
@@ -157,7 +189,7 @@ function toggle(value: unknown) {
157
189
 
158
190
  function removeChip(value: unknown, e: Event) {
159
191
  e.stopPropagation()
160
- if (props.disabled) return
192
+ if (isDisabled.value) return
161
193
  emit('update:modelValue', props.modelValue.filter((v) => !eq(v, value)))
162
194
  }
163
195
 
@@ -172,7 +204,7 @@ function computeDropPos() {
172
204
  }
173
205
 
174
206
  async function openDropdown() {
175
- if (props.disabled) return
207
+ if (isDisabled.value) return
176
208
  if (props.mode === 'modal') {
177
209
  modalOpen.value = true
178
210
  modalSearch.value = ''
@@ -227,26 +259,26 @@ const triggerClasses = computed(() => {
227
259
  const base = [
228
260
  'flex min-h-[56px] w-full min-w-0 items-center gap-1.5 flex-wrap outline-none',
229
261
  'transition-[border-color,border-width] duration-150',
230
- props.disabled ? 'cursor-not-allowed' : 'cursor-pointer',
262
+ isDisabled.value ? 'cursor-not-allowed' : 'cursor-pointer',
231
263
  props.leadingIcon ? 'pl-12' : 'pl-4',
232
264
  FIELD_PADDING_RIGHT[rightInset.value],
233
265
  ]
234
266
 
235
267
  if (props.variant === 'outlined') {
236
- const border = props.disabled
268
+ const border = isDisabled.value
237
269
  ? 'border-on-surface/12'
238
270
  : highlighted.value
239
- ? (props.error ? 'border-2 border-error' : 'border-2 border-primary')
240
- : (props.error ? 'border-error' : 'border-outline hover:border-on-surface')
271
+ ? (hasError.value ? 'border-2 border-error' : 'border-2 border-primary')
272
+ : (hasError.value ? 'border-error' : 'border-outline hover:border-on-surface')
241
273
  return [...base, 'rounded-xs border bg-transparent py-2', border].join(' ')
242
274
  }
243
275
 
244
- const border = props.disabled
276
+ const border = isDisabled.value
245
277
  ? 'border-on-surface/38'
246
278
  : highlighted.value
247
- ? (props.error ? 'border-b-2 border-error' : 'border-b-2 border-primary')
248
- : (props.error ? 'border-error' : 'border-on-surface-variant hover:border-on-surface')
249
- const bg = props.disabled
279
+ ? (hasError.value ? 'border-b-2 border-error' : 'border-b-2 border-primary')
280
+ : (hasError.value ? 'border-error' : 'border-on-surface-variant hover:border-on-surface')
281
+ const bg = isDisabled.value
250
282
  ? 'bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]'
251
283
  : 'bg-surface-container-highest'
252
284
  return [
@@ -264,17 +296,30 @@ const labelClasses = computed(() =>
264
296
  leading: !!props.leadingIcon,
265
297
  right: rightInset.value,
266
298
  floated: highlighted.value || hasValue.value,
267
- color: props.disabled
299
+ color: isDisabled.value
268
300
  ? 'text-on-surface/38'
269
301
  : highlighted.value
270
- ? (props.error ? 'text-error' : 'text-primary')
271
- : (props.error ? 'text-error' : 'text-on-surface-variant'),
302
+ ? (hasError.value ? 'text-error' : 'text-primary')
303
+ : (hasError.value ? 'text-error' : 'text-on-surface-variant'),
272
304
  }),
273
305
  )
306
+
307
+ defineExpose({
308
+ /** Checks its rules and shows the result; resolves to whether they pass */
309
+ validate: validation.validate,
310
+ /** Hides its validation error (until the next check) */
311
+ resetValidation: validation.resetValidation,
312
+ })
313
+
314
+ // Last: it watches `open`, declared above
315
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
316
+ validation.onBlur()
317
+ emit('blur')
318
+ }, () => open.value)
274
319
  </script>
275
320
 
276
321
  <template>
277
- <div class="flex flex-col gap-1" v-bind="$attrs">
322
+ <div ref="rootEl" class="flex flex-col gap-1" v-bind="$attrs" @focusout="onFocusOut">
278
323
  <div
279
324
  ref="fieldEl"
280
325
  class="relative"
@@ -286,7 +331,7 @@ const labelClasses = computed(() =>
286
331
  class="pointer-events-none absolute left-3.5"
287
332
  :class="[
288
333
  variant === 'filled' ? 'top-4.5' : 'top-4',
289
- disabled ? 'text-on-surface/38' : 'text-on-surface-variant',
334
+ isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant',
290
335
  ]"
291
336
  >
292
337
  <MIcon :name="leadingIcon" :size="24" />
@@ -297,7 +342,9 @@ const labelClasses = computed(() =>
297
342
  :id="id"
298
343
  :class="triggerClasses"
299
344
  role="button"
300
- :tabindex="disabled ? -1 : 0"
345
+ :tabindex="isDisabled ? -1 : 0"
346
+ :aria-invalid="hasError || undefined"
347
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
301
348
  :aria-expanded="open || modalOpen"
302
349
  :aria-haspopup="true"
303
350
  :aria-controls="open ? `${id}-list` : undefined"
@@ -314,12 +361,12 @@ const labelClasses = computed(() =>
314
361
  v-for="(chip, i) in displayChips"
315
362
  :key="i"
316
363
  class="inline-flex max-w-full min-w-0 items-center gap-1 rounded-full px-2 py-0.5 text-label-small"
317
- :class="disabled ? 'bg-on-surface/12 text-on-surface/38' : 'bg-secondary-container text-on-secondary-container'"
364
+ :class="isDisabled ? 'bg-on-surface/12 text-on-surface/38' : 'bg-secondary-container text-on-secondary-container'"
318
365
  >
319
366
  <span v-overflow-title class="min-w-0 truncate">{{ chip.label }}</span>
320
367
  <button
321
368
  :aria-label="`${locale.remove} ${chip.label}`"
322
- v-if="!disabled"
369
+ v-if="!isDisabled"
323
370
  type="button"
324
371
  class="flex h-4 w-4 shrink-0 items-center justify-center rounded-full hover:bg-on-secondary-container/20"
325
372
  @click="removeChip(chip.value, $event)"
@@ -375,8 +422,8 @@ const labelClasses = computed(() =>
375
422
  </div>
376
423
  </div>
377
424
 
378
- <p v-if="error && errorLabel" class="px-4 text-body-small text-error">{{ errorLabel }}</p>
379
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
425
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
426
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
380
427
  </div>
381
428
 
382
429
  <!-- Docked dropdown -->
@@ -7,6 +7,8 @@
7
7
  * clicks/arrow keys still emit instantly
8
8
  */
9
9
  import { computed, ref, useId } from 'vue'
10
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
11
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
10
12
  import MIcon from './MIcon.vue'
11
13
  import MIconButton from './MIconButton.vue'
12
14
  import { useFieldBg } from '../composables/useFieldBg'
@@ -27,10 +29,21 @@ const props = withDefaults(
27
29
  max?: number
28
30
  /** Increment/decrement step */
29
31
  step?: number
30
- /** Error state */
31
- error?: boolean
32
+ /** Error state. A string is also the message (`:error="errors.x"`) */
33
+ error?: boolean | string
32
34
  /** Error message shown when error is true */
33
35
  errorLabel?: string
36
+ /**
37
+ * Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
38
+ * `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
39
+ */
40
+ rules?: ValidationRule | ValidationRule[]
41
+ /** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
42
+ validateOn?: ValidateOn
43
+ /** Wait this long (ms) after the value stops changing before re-checking (one request for an async rule, not one per key). Defaults to its `MForm`'s */
44
+ validateDebounce?: number
45
+ /** The key of this field in its `MForm`'s values, errors and schema */
46
+ name?: string
34
47
  /** Hint text */
35
48
  hint?: string
36
49
  /** Disables the field */
@@ -57,7 +70,30 @@ const props = withDefaults(
57
70
  },
58
71
  )
59
72
 
60
- const emit = defineEmits<{ 'update:modelValue': [number | null]; debounced: [number | null] }>()
73
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [number | null]; debounced: [number | null] }>()
74
+
75
+ const rootEl = ref<HTMLElement | null>(null)
76
+ const validation = useFieldValidation({
77
+ value: () => props.modelValue,
78
+ rules: () => props.rules,
79
+ required: () => props.required,
80
+ error: () => props.error,
81
+ errorLabel: () => props.errorLabel,
82
+ name: () => props.name,
83
+ validateOn: () => props.validateOn,
84
+ validateDebounce: () => props.validateDebounce,
85
+ disabled: () => props.disabled,
86
+ focus: () => { inputEl.value?.focus() },
87
+ el: () => inputEl.value,
88
+ setValue: (v) => emit('update:modelValue', v as number | null),
89
+ })
90
+ /** Its own `disabled` or its MForm's */
91
+ const isDisabled = validation.disabled
92
+ const { hasError, message: errorMessage, messageId } = validation
93
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
94
+ validation.onBlur()
95
+ emit('blur')
96
+ })
61
97
 
62
98
  const id = useId()
63
99
  const fieldBgEl = ref<HTMLElement | null>(null)
@@ -90,14 +126,14 @@ function onInput(e: Event) {
90
126
  }
91
127
 
92
128
  function increment() {
93
- if (props.disabled) return
129
+ if (isDisabled.value) return
94
130
  const v = clamp((props.modelValue ?? 0) + props.step)
95
131
  emit('update:modelValue', v)
96
132
  if (props.debounce > 0) emitDebounced(v)
97
133
  }
98
134
 
99
135
  function decrement() {
100
- if (props.disabled) return
136
+ if (isDisabled.value) return
101
137
  const v = clamp((props.modelValue ?? 0) - props.step)
102
138
  emit('update:modelValue', v)
103
139
  if (props.debounce > 0) emitDebounced(v)
@@ -118,13 +154,13 @@ const inputClasses = computed(() => {
118
154
  'peer block w-full text-body-large text-on-surface outline-none placeholder:text-transparent',
119
155
  'transition-[border-color,border-width] duration-150',
120
156
  'disabled:cursor-not-allowed disabled:text-on-surface/38',
121
- props.error ? 'caret-error' : 'caret-primary',
157
+ hasError.value ? 'caret-error' : 'caret-primary',
122
158
  'h-14', pl, pr,
123
159
  ]
124
160
  if (props.variant === 'outlined') {
125
161
  return [...base,
126
162
  'rounded-xs border bg-transparent py-4',
127
- props.error
163
+ hasError.value
128
164
  ? 'border-error focus:border-2 focus:border-error'
129
165
  : 'border-outline hover:border-on-surface focus:border-2 focus:border-primary',
130
166
  'disabled:border-on-surface/12',
@@ -132,7 +168,7 @@ const inputClasses = computed(() => {
132
168
  }
133
169
  return [...base,
134
170
  'rounded-t-xs bg-surface-container-highest border-b pt-6 pb-2',
135
- props.error
171
+ hasError.value
136
172
  ? 'border-error focus:border-b-2 focus:border-error'
137
173
  : 'border-on-surface-variant hover:border-on-surface focus:border-b-2 focus:border-primary',
138
174
  'disabled:border-on-surface/38 disabled:bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]',
@@ -146,13 +182,17 @@ const labelClasses = computed(() =>
146
182
  right: rightInset.value,
147
183
  floated: 'peer',
148
184
  color: [
149
- props.error ? 'text-error peer-focus:text-error' : 'text-on-surface-variant peer-focus:text-primary',
185
+ hasError.value ? 'text-error peer-focus:text-error' : 'text-on-surface-variant peer-focus:text-primary',
150
186
  'peer-disabled:text-on-surface/38',
151
187
  ].join(' '),
152
188
  }),
153
189
  )
154
190
 
155
191
  defineExpose({
192
+ /** Checks its rules and shows the result; resolves to whether they pass */
193
+ validate: validation.validate,
194
+ /** Hides its validation error (until the next check) */
195
+ resetValidation: validation.resetValidation,
156
196
  /** The `<input>` (or `<textarea>`) element */
157
197
  inputEl: inputEl,
158
198
  /** Focuses the field */
@@ -165,7 +205,7 @@ defineExpose({
165
205
  </script>
166
206
 
167
207
  <template>
168
- <div class="flex flex-col gap-1">
208
+ <div ref="rootEl" class="flex flex-col gap-1" @focusout="onFocusOut">
169
209
  <div
170
210
  ref="fieldBgEl"
171
211
  class="relative"
@@ -177,7 +217,7 @@ defineExpose({
177
217
  class="pointer-events-none absolute left-3.5"
178
218
  :class="[
179
219
  variant === 'filled' ? 'top-4.5' : 'top-4',
180
- disabled ? 'text-on-surface/38' : 'text-on-surface-variant',
220
+ isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant',
181
221
  ]"
182
222
  >
183
223
  <MIcon :name="leadingIcon" :size="24" />
@@ -185,10 +225,12 @@ defineExpose({
185
225
 
186
226
  <input
187
227
  ref="inputEl"
228
+ :aria-invalid="hasError || undefined"
229
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
188
230
  :id="id"
189
231
  type="number"
190
232
  :value="modelValue ?? ''"
191
- :disabled="disabled"
233
+ :disabled="isDisabled"
192
234
  :required="required"
193
235
  :min="min"
194
236
  :max="max"
@@ -205,13 +247,13 @@ defineExpose({
205
247
  </label>
206
248
 
207
249
  <div v-if="stepper" class="absolute right-1 top-1/2 -translate-y-1/2 flex items-center gap-0.5">
208
- <MIconButton icon="remove" label="Decrease" :size="32" :disabled="disabled || (min !== undefined && (modelValue ?? 0) <= min)" @click="decrement" />
209
- <MIconButton icon="add" label="Increase" :size="32" :disabled="disabled || (max !== undefined && (modelValue ?? 0) >= max)" @click="increment" />
250
+ <MIconButton icon="remove" label="Decrease" :size="32" :disabled="isDisabled || (min !== undefined && (modelValue ?? 0) <= min)" @click="decrement" />
251
+ <MIconButton icon="add" label="Increase" :size="32" :disabled="isDisabled || (max !== undefined && (modelValue ?? 0) >= max)" @click="increment" />
210
252
  </div>
211
253
  </div>
212
254
 
213
- <p v-if="error && errorLabel" class="px-4 text-body-small text-error">{{ errorLabel }}</p>
214
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
255
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
256
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
215
257
  </div>
216
258
  </template>
217
259