@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
@@ -11,8 +11,11 @@
11
11
  * @event update:modelValue Emitted with the number in E.164, or null when empty — powers v-model
12
12
  * @event update:country Emitted with the ISO code when the country changes — powers v-model:country
13
13
  * @event update:valid Emitted when the number becomes (in)valid for its country — powers v-model:valid
14
+ * @event blur Focus left the field and its country list
14
15
  */
15
- import { computed, nextTick, onBeforeUnmount, ref, useId, watch } from 'vue'
16
+ import { computed, inject, nextTick, onBeforeUnmount, ref, useId, watch } from 'vue'
17
+ import { FORM_KEY, useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
18
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
16
19
  import MIcon from './MIcon.vue'
17
20
  import { useFieldBg } from '../composables/useFieldBg'
18
21
  import { useLocale } from '../composables/useLocale'
@@ -47,17 +50,25 @@ const props = withDefaults(
47
50
  preferred?: string[]
48
51
  /** Visual style */
49
52
  variant?: 'filled' | 'outlined'
50
- /** Puts the field in error state */
51
- error?: boolean
53
+ /** Puts the field in error state. A string is also the message */
54
+ error?: boolean | string
52
55
  /** Error message below the field (when `error`, or when `validate` finds the number incomplete) */
53
56
  errorLabel?: string
57
+ /** More validation rules (functions or a Standard Schema), run on the E.164 value; see `MForm` */
58
+ rules?: ValidationRule | ValidationRule[]
59
+ /** When the rules are checked and shown: on leaving the field (default), while typing, or only on submit */
60
+ validateOn?: ValidateOn
61
+ /** 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 */
62
+ validateDebounce?: number
63
+ /** The key of this field in its `MForm` */
64
+ name?: string
54
65
  /** Hint text below the field */
55
66
  hint?: string
56
67
  /** Disables the field */
57
68
  disabled?: boolean
58
69
  /** Marks as required — adds a red asterisk after the label */
59
70
  required?: boolean
60
- /** After leaving the field, show an error when the number doesn't fit its country's length */
71
+ /** After leaving the field, show an error when the number doesn't fit its country's length (and block an `MForm`'s submit) */
61
72
  validate?: boolean
62
73
  /** Placeholder; defaults to an example in the country's format */
63
74
  placeholder?: string
@@ -75,6 +86,7 @@ const emit = defineEmits<{
75
86
  'update:modelValue': [string | null]
76
87
  'update:country': [string]
77
88
  'update:valid': [boolean]
89
+ blur: []
78
90
  }>()
79
91
 
80
92
  const t = useLocale()
@@ -132,7 +144,6 @@ const examplePlaceholder = computed(() => {
132
144
 
133
145
  // ── Typing ────────────────────────────────────────────────────────
134
146
  const inputEl = ref<HTMLInputElement>()
135
- const touched = ref(false)
136
147
 
137
148
  // Puts the caret after the n-th digit of the formatted text, so reformatting doesn't throw it to the end
138
149
  function caretAfterDigits(text: string, n: number) {
@@ -188,9 +199,30 @@ function onKeydown(e: KeyboardEvent) {
188
199
  commit(digits.slice(0, digitsBefore - 1) + digits.slice(digitsBefore), digitsBefore - 1)
189
200
  }
190
201
 
191
- const showInvalid = computed(() => props.validate && touched.value && !!national.value && !valid.value)
192
- const isError = computed(() => props.error || showInvalid.value)
193
- const errorText = computed(() => props.errorLabel ?? (showInvalid.value ? t.phoneInvalid : undefined))
202
+ // The number's own check (`validate`) is one more rule, after the consumer's
203
+ const phoneRule: ValidationRule = () => !props.validate || !national.value || valid.value || props.errorLabel || t.phoneInvalid
204
+ const inForm = !!inject(FORM_KEY, null)
205
+ const rootEl = ref<HTMLElement | null>(null)
206
+ const validation = useFieldValidation({
207
+ value: () => props.modelValue,
208
+ rules: () => [...(props.rules ? (Array.isArray(props.rules) ? props.rules : [props.rules]) : []), phoneRule],
209
+ // phoneRule is always there: `required` alone (no MForm, no rules) still only shows the asterisk
210
+ required: () => props.required && (inForm || !!props.rules),
211
+ error: () => props.error,
212
+ errorLabel: () => props.errorLabel,
213
+ name: () => props.name,
214
+ validateOn: () => props.validateOn,
215
+ validateDebounce: () => props.validateDebounce,
216
+ disabled: () => props.disabled,
217
+ focus: () => inputEl.value?.focus(),
218
+ el: () => inputEl.value,
219
+ setValue: (v) => emit('update:modelValue', (v as string | null) ?? null),
220
+ })
221
+ /** Its own `disabled` or its MForm's */
222
+ const isDisabled = validation.disabled
223
+ const isError = validation.hasError
224
+ const errorText = validation.message
225
+ const messageId = validation.messageId
194
226
 
195
227
  // ── Country list ──────────────────────────────────────────────────
196
228
  const open = ref(false)
@@ -258,7 +290,7 @@ function place() {
258
290
  }
259
291
 
260
292
  async function openList() {
261
- if (props.disabled) return
293
+ if (isDisabled.value) return
262
294
  open.value = true
263
295
  search.value = ''
264
296
  await nextTick()
@@ -320,15 +352,25 @@ onBeforeUnmount(() => {
320
352
  document.removeEventListener('mousedown', onDocDown)
321
353
  })
322
354
 
355
+ // Last: it watches `open`, declared above
356
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
357
+ validation.onBlur()
358
+ emit('blur')
359
+ }, () => open.value)
360
+
323
361
  defineExpose({
324
362
  focus: (options?: FocusOptions) => inputEl.value?.focus(options),
363
+ /** Checks its rules (and the number's length) and shows the result; resolves to whether they pass */
364
+ validate: validation.validate,
365
+ /** Hides its validation error (until the next check) */
366
+ resetValidation: validation.resetValidation,
325
367
  /** Whether the number fits its country's length */
326
368
  valid,
327
369
  })
328
370
  </script>
329
371
 
330
372
  <template>
331
- <div class="m3-phone-input flex flex-col gap-1">
373
+ <div ref="rootEl" class="m3-phone-input flex flex-col gap-1" @focusout="onFocusOut">
332
374
  <div
333
375
  ref="fieldEl"
334
376
  class="group relative flex h-14 transition-[border-color,border-width] duration-150"
@@ -336,7 +378,7 @@ defineExpose({
336
378
  variant === 'outlined'
337
379
  ? [
338
380
  'mt-2 items-center rounded-xs border',
339
- disabled
381
+ isDisabled
340
382
  ? 'border-on-surface/12'
341
383
  : isError
342
384
  ? 'border-error focus-within:border-2 focus-within:border-error'
@@ -344,7 +386,7 @@ defineExpose({
344
386
  ]
345
387
  : [
346
388
  'items-end rounded-t-xs border-b bg-surface-container-highest pb-2',
347
- disabled
389
+ isDisabled
348
390
  ? 'border-b-on-surface/38 bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]'
349
391
  : isError
350
392
  ? 'border-b-error focus-within:border-b-2 focus-within:border-b-error'
@@ -359,7 +401,7 @@ defineExpose({
359
401
  class="pointer-events-none absolute truncate text-label-small"
360
402
  :class="[
361
403
  variant === 'outlined' ? 'left-3 -top-2.5 max-w-[calc(100%-1.5rem)] bg-[var(--field-bg)] px-1' : 'left-4 top-2 right-4',
362
- disabled ? 'text-on-surface/38' : isError ? 'text-error' : 'text-on-surface-variant group-focus-within:text-primary',
404
+ isDisabled ? 'text-on-surface/38' : isError ? 'text-error' : 'text-on-surface-variant group-focus-within:text-primary',
363
405
  ]"
364
406
  >
365
407
  {{ label }}<span v-if="required" class="text-error">&nbsp;*</span>
@@ -371,7 +413,7 @@ defineExpose({
371
413
  type="button"
372
414
  class="m3-phone-country ml-1 flex h-8 shrink-0 cursor-pointer items-center gap-1 rounded-full pr-1 pl-3 text-body-large text-on-surface outline-none transition-colors hover:bg-on-surface/8 focus-visible:ring-2 focus-visible:ring-secondary disabled:cursor-not-allowed disabled:text-on-surface/38"
373
415
  :class="variant === 'filled' && '-mb-1'"
374
- :disabled="disabled"
416
+ :disabled="isDisabled"
375
417
  :aria-label="`${t.phoneCountry}: ${nameOf(country.iso2)} +${country.dial}`"
376
418
  aria-haspopup="listbox"
377
419
  :aria-expanded="open"
@@ -392,20 +434,20 @@ defineExpose({
392
434
  inputmode="tel"
393
435
  autocomplete="tel"
394
436
  :value="display"
395
- :disabled="disabled"
437
+ :disabled="isDisabled"
396
438
  :required="required"
397
439
  :placeholder="placeholder ?? examplePlaceholder"
398
440
  :aria-invalid="isError || undefined"
441
+ :aria-describedby="errorText || hint ? messageId : undefined"
399
442
  class="min-w-0 flex-1 bg-transparent pr-4 pl-1 text-body-large text-on-surface outline-none placeholder:text-on-surface-variant/60 disabled:cursor-not-allowed disabled:text-on-surface/38"
400
443
  :class="isError ? 'caret-error' : 'caret-primary'"
401
444
  @input="onInput"
402
445
  @keydown="onKeydown"
403
- @blur="touched = true"
404
446
  />
405
447
  </div>
406
448
 
407
- <p v-if="isError && errorText" class="px-4 text-body-small text-error">{{ errorText }}</p>
408
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
449
+ <p v-if="errorText" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorText }}</p>
450
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
409
451
 
410
452
  <!-- Country list: a menu surface (surface-container, elevation 2) with a search field on top -->
411
453
  <Teleport to="body">
@@ -8,6 +8,8 @@
8
8
  * auto-submit
9
9
  */
10
10
  import { computed, nextTick, ref, watch } from 'vue'
11
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
12
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
11
13
 
12
14
  const props = withDefaults(
13
15
  defineProps<{
@@ -25,10 +27,23 @@ const props = withDefaults(
25
27
  masked?: boolean
26
28
  /** Visual style */
27
29
  variant?: 'filled' | 'outlined'
28
- /** Error state */
29
- error?: boolean
30
+ /** Error state. A string is also the message (`:error="errors.x"`) */
31
+ error?: boolean | string
30
32
  /** Error message shown when error is true */
31
33
  errorLabel?: string
34
+ /** Marks as required — adds a red asterisk after the label; inside an `MForm` or with `rules`, also validates it */
35
+ required?: boolean
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
32
47
  /** Hint text below the boxes */
33
48
  hint?: string
34
49
  /** Disables every box */
@@ -65,7 +80,31 @@ const groupBoundaries = computed(() => {
65
80
  return boundaries
66
81
  })
67
82
 
68
- const emit = defineEmits<{ 'update:modelValue': [string]; complete: [string] }>()
83
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [string]; complete: [string] }>()
84
+
85
+ const rootEl = ref<HTMLElement | null>(null)
86
+ const validation = useFieldValidation({
87
+ value: () => props.modelValue,
88
+ rules: () => props.rules,
89
+ required: () => props.required,
90
+ error: () => props.error,
91
+ errorLabel: () => props.errorLabel,
92
+ name: () => props.name,
93
+ validateOn: () => props.validateOn,
94
+ validateDebounce: () => props.validateDebounce,
95
+ disabled: () => props.disabled,
96
+ focus: () => { inputEls.value[0]?.focus() },
97
+ el: () => inputEls.value[0],
98
+ setValue: (v) => emit('update:modelValue', v as string),
99
+ isEmpty: (v) => String(v ?? '').length < props.length,
100
+ })
101
+ /** Its own `disabled` or its MForm's */
102
+ const isDisabled = validation.disabled
103
+ const { hasError, message: errorMessage, messageId } = validation
104
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
105
+ validation.onBlur()
106
+ emit('blur')
107
+ })
69
108
 
70
109
  const inputEls = ref<(HTMLInputElement | null)[]>([])
71
110
 
@@ -171,12 +210,12 @@ const boxClasses = computed(() => {
171
210
  'h-14 w-12 min-w-0 shrink text-center text-title-large outline-none',
172
211
  'transition-[border-color,border-width] duration-150',
173
212
  'disabled:cursor-not-allowed disabled:text-on-surface/38',
174
- props.error ? 'caret-error' : 'caret-primary',
213
+ hasError.value ? 'caret-error' : 'caret-primary',
175
214
  ]
176
215
  if (props.variant === 'outlined') {
177
216
  return [...base,
178
217
  'rounded-xs border bg-transparent text-on-surface',
179
- props.error
218
+ hasError.value
180
219
  ? 'border-error focus:border-2 focus:border-error'
181
220
  : 'border-outline hover:border-on-surface focus:border-2 focus:border-primary',
182
221
  'disabled:border-on-surface/12',
@@ -184,16 +223,23 @@ const boxClasses = computed(() => {
184
223
  }
185
224
  return [...base,
186
225
  'rounded-t-xs bg-surface-container-highest border-b text-on-surface',
187
- props.error
226
+ hasError.value
188
227
  ? 'border-error focus:border-b-2 focus:border-error'
189
228
  : 'border-on-surface-variant hover:border-on-surface focus:border-b-2 focus:border-primary',
190
229
  'disabled:border-on-surface/38 disabled:bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]',
191
230
  ].join(' ')
192
231
  })
232
+
233
+ defineExpose({
234
+ /** Checks its rules and shows the result; resolves to whether they pass */
235
+ validate: validation.validate,
236
+ /** Hides its validation error (until the next check) */
237
+ resetValidation: validation.resetValidation,
238
+ })
193
239
  </script>
194
240
 
195
241
  <template>
196
- <div class="flex flex-col gap-1">
242
+ <div ref="rootEl" class="flex flex-col gap-1" @focusout="onFocusOut">
197
243
  <div role="group" :aria-label="label" class="flex min-w-0 items-center gap-2">
198
244
  <template v-for="(digit, i) in digits" :key="i">
199
245
  <input
@@ -203,8 +249,10 @@ const boxClasses = computed(() => {
203
249
  :inputmode="type === 'numeric' ? 'numeric' : 'text'"
204
250
  autocomplete="one-time-code"
205
251
  maxlength="1"
206
- :disabled="disabled"
252
+ :disabled="isDisabled"
207
253
  :aria-label="`Digit ${i + 1} of ${totalLength}`"
254
+ :aria-invalid="hasError || undefined"
255
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
208
256
  :class="boxClasses"
209
257
  @input="onInput(i, $event)"
210
258
  @keydown="onKeydown(i, $event)"
@@ -219,7 +267,7 @@ const boxClasses = computed(() => {
219
267
  </template>
220
268
  </div>
221
269
 
222
- <p v-if="error && errorLabel" class="px-1 text-body-small text-error">{{ errorLabel }}</p>
223
- <p v-else-if="hint" class="px-1 text-body-small text-on-surface-variant">{{ hint }}</p>
270
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-1 text-body-small text-error">{{ errorMessage }}</p>
271
+ <p v-else-if="hint" :id="messageId" class="px-1 text-body-small text-on-surface-variant">{{ hint }}</p>
224
272
  </div>
225
273
  </template>
@@ -3,8 +3,12 @@
3
3
  * A group of radio buttons sharing a single selection, column or row layout.
4
4
  *
5
5
  * @event update:modelValue Emitted with the selected option's value.
6
+ * @event blur Emitted when focus leaves the group
6
7
  */
8
+ import { ref } from 'vue'
7
9
  import MRadio from './MRadio.vue'
10
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
11
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
8
12
 
9
13
  interface Option {
10
14
  label: string
@@ -25,14 +29,69 @@ const props = withDefaults(defineProps<{
25
29
  disabled?: boolean
26
30
  /** Radio color */
27
31
  color?: 'primary' | 'secondary' | 'tertiary' | 'error'
32
+ /** Error state. A string is also the message */
33
+ error?: boolean | string
34
+ /** Error message shown when `error` is true */
35
+ errorLabel?: string
36
+ /** Marks it as required (asterisk); inside an `MForm` or with `rules`, an option must be picked */
37
+ required?: boolean
38
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
39
+ rules?: ValidationRule | ValidationRule[]
40
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
41
+ validateOn?: ValidateOn
42
+ /** 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 */
43
+ validateDebounce?: number
44
+ /** The key of this field in its `MForm` */
45
+ name?: string
28
46
  }>(), { direction: 'column', disabled: false, color: 'primary' })
29
47
 
30
- const emit = defineEmits<{ 'update:modelValue': [unknown] }>()
48
+ const emit = defineEmits<{ 'update:modelValue': [unknown]; blur: [] }>()
49
+
50
+ const rootEl = ref<HTMLElement | null>(null)
51
+
52
+ const validation = useFieldValidation({
53
+ value: () => props.modelValue,
54
+ rules: () => props.rules,
55
+ required: () => props.required,
56
+ error: () => props.error,
57
+ errorLabel: () => props.errorLabel,
58
+ name: () => props.name,
59
+ validateOn: () => props.validateOn,
60
+ validateDebounce: () => props.validateDebounce,
61
+ disabled: () => props.disabled,
62
+ focus: () => { (rootEl.value?.querySelector<HTMLInputElement>('input:checked:not(:disabled)') ?? rootEl.value?.querySelector<HTMLInputElement>('input:not(:disabled)'))?.focus() },
63
+ el: () => rootEl.value,
64
+ setValue: (v) => emit('update:modelValue', v as unknown),
65
+ })
66
+ /** Its own `disabled` or its MForm's */
67
+ const isDisabled = validation.disabled
68
+ const { hasError, message: errorMessage, messageId } = validation
69
+ // Moving between its radios isn't leaving it
70
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
71
+ validation.onBlur()
72
+ emit('blur')
73
+ })
74
+
75
+ defineExpose({
76
+ /** Checks its rules and shows the result; resolves to whether they pass */
77
+ validate: validation.validate,
78
+ /** Hides its validation error (until the next check) */
79
+ resetValidation: validation.resetValidation,
80
+ })
31
81
  </script>
32
82
 
33
83
  <template>
34
- <div class="flex flex-col gap-2">
35
- <span v-if="label" class="text-label-large text-on-surface-variant">{{ label }}</span>
84
+ <div
85
+ ref="rootEl"
86
+ class="flex flex-col gap-2"
87
+ role="radiogroup"
88
+ :aria-label="label"
89
+ :aria-required="required || undefined"
90
+ :aria-invalid="hasError || undefined"
91
+ :aria-describedby="errorMessage ? messageId : undefined"
92
+ @focusout="onFocusOut"
93
+ >
94
+ <span v-if="label" class="text-label-large" :class="hasError ? 'text-error' : 'text-on-surface-variant'">{{ label }}<span v-if="required" class="text-error">&nbsp;*</span></span>
36
95
  <div
37
96
  class="flex gap-4"
38
97
  :class="direction === 'row' ? 'flex-row flex-wrap' : 'flex-col'"
@@ -44,9 +103,11 @@ const emit = defineEmits<{ 'update:modelValue': [unknown] }>()
44
103
  :value="opt.value"
45
104
  :label="opt.label"
46
105
  :color="color"
47
- :disabled="disabled || !!opt.disabled"
106
+ :disabled="isDisabled || !!opt.disabled"
48
107
  @update:model-value="emit('update:modelValue', $event)"
49
108
  />
50
109
  </div>
110
+ <!-- M3's radio has no error state: the message (and the group label) say so -->
111
+ <p v-if="errorMessage" :id="messageId" role="alert" class="text-body-small text-error">{{ errorMessage }}</p>
51
112
  </div>
52
113
  </template>
@@ -6,8 +6,11 @@
6
6
  *
7
7
  * @event update:modelValue Emitted when the user rates by click, drag or keyboard (v-model). Dragging past the
8
8
  * first icon emits 0
9
+ * @event blur Emitted when the rating loses focus
9
10
  */
10
11
  import { computed, ref } from 'vue'
12
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
13
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
11
14
  import MIcon from './MIcon.vue'
12
15
 
13
16
  // Not part of the official M3 spec — built from M3 primitives instead: filled/outlined
@@ -33,9 +36,26 @@ const props = withDefaults(defineProps<{
33
36
  * without this prop
34
37
  */
35
38
  halfIncrements?: boolean
39
+ /** Error state. A string is also the message */
40
+ error?: boolean | string
41
+ /** Error message shown when `error` is true */
42
+ errorLabel?: string
43
+ /** Inside an `MForm` or with `rules`, 0 (not rated) fails */
44
+ required?: boolean
45
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
46
+ rules?: ValidationRule | ValidationRule[]
47
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
48
+ validateOn?: ValidateOn
49
+ /** 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 */
50
+ validateDebounce?: number
51
+ /** The key of this field in its `MForm` */
52
+ name?: string
36
53
  }>(), { max: 5, size: 28, color: 'primary', icon: 'star', halfIncrements: false })
37
54
 
38
- const emit = defineEmits<{ 'update:modelValue': [number] }>()
55
+ // Rating + its message: attributes go on the rating
56
+ defineOptions({ inheritAttrs: false })
57
+
58
+ const emit = defineEmits<{ 'update:modelValue': [number]; blur: [] }>()
39
59
 
40
60
  const ROLE_COLORS: Record<string, string> = {
41
61
  primary: 'text-primary',
@@ -47,10 +67,33 @@ const ROLE_COLORS: Record<string, string> = {
47
67
  const colorClass = computed(() => ROLE_COLORS[props.color] ?? '')
48
68
  const customStyle = computed(() => (props.color in ROLE_COLORS ? undefined : { color: props.color }))
49
69
 
50
- const interactive = computed(() => !props.readonly && !props.disabled)
70
+ const interactive = computed(() => !props.readonly && !isDisabled.value)
51
71
  const step = computed(() => (props.halfIncrements ? 0.5 : 1))
52
72
 
53
73
  const rootEl = ref<HTMLElement | null>(null)
74
+
75
+ const validation = useFieldValidation({
76
+ value: () => props.modelValue,
77
+ rules: () => props.rules,
78
+ required: () => props.required,
79
+ error: () => props.error,
80
+ errorLabel: () => props.errorLabel,
81
+ name: () => props.name,
82
+ validateOn: () => props.validateOn,
83
+ validateDebounce: () => props.validateDebounce,
84
+ disabled: () => props.disabled || props.readonly,
85
+ focus: () => { rootEl.value?.focus() },
86
+ el: () => rootEl.value,
87
+ setValue: (v) => emit('update:modelValue', v as number),
88
+ isEmpty: (v) => !v
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
+ })
54
97
  const itemEls: HTMLElement[] = []
55
98
 
56
99
  // Pointer preview: `value` is what the rating would become, `index` is the item under the
@@ -160,11 +203,19 @@ function stateLayerClass(i: number) {
160
203
  }
161
204
 
162
205
  const valueText = computed(() => `${clamp(props.modelValue || 0)} / ${props.max}`)
206
+
207
+ defineExpose({
208
+ /** Checks its rules and shows the result; resolves to whether they pass */
209
+ validate: validation.validate,
210
+ /** Hides its validation error (until the next check) */
211
+ resetValidation: validation.resetValidation,
212
+ })
163
213
  </script>
164
214
 
165
215
  <template>
166
216
  <div
167
217
  ref="rootEl"
218
+ v-bind="$attrs"
168
219
  class="inline-flex select-none items-center gap-0.5 rounded-full outline-none ring-secondary focus-visible:ring-2"
169
220
  :class="interactive ? 'cursor-pointer touch-pan-y' : 'cursor-default'"
170
221
  :role="readonly ? 'img' : 'slider'"
@@ -174,7 +225,10 @@ const valueText = computed(() => `${clamp(props.modelValue || 0)} / ${props.max}
174
225
  :aria-valuemax="readonly ? undefined : max"
175
226
  :aria-valuenow="readonly ? undefined : clamp(modelValue || 0)"
176
227
  :aria-valuetext="readonly ? undefined : valueText"
177
- :aria-disabled="disabled || undefined"
228
+ :aria-disabled="isDisabled || undefined"
229
+ :aria-invalid="hasError || undefined"
230
+ :aria-describedby="errorMessage ? messageId : undefined"
231
+ @focusout="onFocusOut"
178
232
  @pointerdown="onPointerDown"
179
233
  @pointermove="onPointerMove"
180
234
  @pointerup="onPointerUp"
@@ -203,7 +257,7 @@ const valueText = computed(() => `${clamp(props.modelValue || 0)} / ${props.max}
203
257
  :name="icon"
204
258
  :size="size"
205
259
  class="absolute end-0 top-0"
206
- :class="disabled ? 'text-on-surface/38' : 'text-on-surface-variant'"
260
+ :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant'"
207
261
  />
208
262
  </span>
209
263
  <!-- Filled part (FILL 1 glyph), clipped from the start side — this is what renders
@@ -213,11 +267,13 @@ const valueText = computed(() => `${clamp(props.modelValue || 0)} / ${props.max}
213
267
  :name="icon"
214
268
  :size="size"
215
269
  class="absolute start-0 top-0"
216
- :class="disabled ? 'text-on-surface/38' : colorClass"
217
- :style="[disabled ? undefined : customStyle, { fontVariationSettings: `'FILL' 1` }]"
270
+ :class="isDisabled ? 'text-on-surface/38' : colorClass"
271
+ :style="[isDisabled ? undefined : customStyle, { fontVariationSettings: `'FILL' 1` }]"
218
272
  />
219
273
  </span>
220
274
  </span>
221
275
  </span>
222
276
  </div>
277
+ <!-- Not an M3 component, and M3 has no rating error: the message says so -->
278
+ <p v-if="errorMessage" :id="messageId" role="alert" class="mt-1 text-body-small text-error">{{ errorMessage }}</p>
223
279
  </template>
@@ -4,8 +4,11 @@
4
4
  * and images.
5
5
  *
6
6
  * @event update:modelValue Emitted with the editor HTML on every content change — powers v-model
7
+ * @event blur Emitted when focus leaves the editor (toolbar and dialogs included)
7
8
  */
8
9
  import { ref, watch, onMounted, onBeforeUnmount, shallowRef, computed } from 'vue'
10
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
11
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
9
12
  import MIcon from './MIcon.vue'
10
13
  import MMenu from './MMenu.vue'
11
14
  import MMenuItem from './MMenuItem.vue'
@@ -52,6 +55,20 @@ const props = withDefaults(
52
55
  placeholder?: string
53
56
  /** Disables editing */
54
57
  disabled?: boolean
58
+ /** Error state. A string is also the message */
59
+ error?: boolean | string
60
+ /** Error message shown when `error` is true */
61
+ errorLabel?: string
62
+ /** Inside an `MForm` or with `rules`, the document must have some text (or an image) */
63
+ required?: boolean
64
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
65
+ rules?: ValidationRule | ValidationRule[]
66
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
67
+ validateOn?: ValidateOn
68
+ /** 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 */
69
+ validateDebounce?: number
70
+ /** The key of this field in its `MForm` */
71
+ name?: string
55
72
  /** Minimum height of the text area (not used with `fill`) */
56
73
  minHeight?: string
57
74
  /**
@@ -97,12 +114,42 @@ const l = computed<Required<RichTextEditorLabels>>(() => ({
97
114
  ...props.labels,
98
115
  }))
99
116
 
100
- const emit = defineEmits<{ 'update:modelValue': [string] }>()
117
+ // Editor + its message: attributes go on the editor
118
+ defineOptions({ inheritAttrs: false })
119
+
120
+ const emit = defineEmits<{ 'update:modelValue': [string]; blur: [] }>()
121
+
122
+ /** An empty document is still HTML (`<p></p>`): empty means no text and no image */
123
+ const isBlankHtml = (v: unknown) => !/<(img|hr|iframe|video)\b/i.test(String(v ?? '')) && !String(v ?? '').replace(/<[^>]*>/g, '').replace(/&nbsp;/g, ' ').trim()
124
+ const rootEl = ref<HTMLElement | null>(null)
101
125
 
102
126
  const ready = ref(false)
103
127
  const editorRef = shallowRef<any>(null)
104
128
  const editorContainer = ref<HTMLElement | null>(null)
105
129
 
130
+ const validation = useFieldValidation({
131
+ value: () => props.modelValue,
132
+ rules: () => props.rules,
133
+ required: () => props.required,
134
+ error: () => props.error,
135
+ errorLabel: () => props.errorLabel,
136
+ name: () => props.name,
137
+ validateOn: () => props.validateOn,
138
+ validateDebounce: () => props.validateDebounce,
139
+ disabled: () => props.disabled,
140
+ focus: () => { editorRef.value?.commands.focus() },
141
+ el: () => rootEl.value,
142
+ setValue: (v) => emit('update:modelValue', v as string),
143
+ isEmpty: isBlankHtml
144
+ })
145
+ /** Its own `disabled` or its MForm's */
146
+ const isDisabled = validation.disabled
147
+ const { hasError, message: errorMessage, messageId } = validation
148
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
149
+ validation.onBlur()
150
+ emit('blur')
151
+ })
152
+
106
153
  interface ToolBtn {
107
154
  icon: string
108
155
  label: string
@@ -140,7 +187,7 @@ onMounted(async () => {
140
187
  const editor = new Editor({
141
188
  element: editorContainer.value!,
142
189
  content: props.modelValue,
143
- editable: !props.disabled,
190
+ editable: !isDisabled.value,
144
191
  extensions: [
145
192
  StarterKit,
146
193
  Underline,
@@ -188,7 +235,7 @@ onMounted(async () => {
188
235
  if (editor.getHTML() !== val) editor.commands.setContent(val)
189
236
  })
190
237
 
191
- watch(() => props.disabled, (v) => editor.setEditable(!v))
238
+ watch(() => isDisabled.value, (v) => editor.setEditable(!v))
192
239
 
193
240
  ready.value = true
194
241
  })
@@ -231,13 +278,25 @@ function confirmImage() {
231
278
  if (imageUrl.value) editorRef.value?.chain().focus().setImage({ src: imageUrl.value }).run()
232
279
  imageDialogOpen.value = false
233
280
  }
281
+
282
+ defineExpose({
283
+ /** Checks its rules and shows the result; resolves to whether they pass */
284
+ validate: validation.validate,
285
+ /** Hides its validation error (until the next check) */
286
+ resetValidation: validation.resetValidation,
287
+ })
234
288
  </script>
235
289
 
236
290
  <template>
237
291
  <div
292
+ ref="rootEl"
293
+ v-bind="$attrs"
238
294
  class="overflow-hidden rounded-lg border transition-colors duration-150"
295
+ :aria-invalid="hasError || undefined"
296
+ :aria-describedby="errorMessage ? messageId : undefined"
297
+ @focusout="onFocusOut"
239
298
  :class="[
240
- disabled ? 'border-outline-variant/50 opacity-60' : 'border-outline-variant focus-within:border-primary',
299
+ isDisabled ? 'border-outline-variant/50 opacity-60' : hasError ? 'border-error' : 'border-outline-variant focus-within:border-primary',
241
300
  { 'flex flex-col': fill },
242
301
  ]"
243
302
  >
@@ -323,6 +382,7 @@ function confirmImage() {
323
382
  </template>
324
383
  </MDialog>
325
384
  </div>
385
+ <p v-if="errorMessage" :id="messageId" role="alert" class="mt-1 px-4 text-body-small text-error">{{ errorMessage }}</p>
326
386
  </template>
327
387
 
328
388
  <style scoped>