@m3ui-vue/m3ui-vue 1.6.1 → 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 (171) 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-BKM1CHxr.js +577 -0
  5. package/dist/MColorPickerModal-BKM1CHxr.js.map +1 -0
  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/MTooltip-CK39Zon4.js +183 -0
  23. package/dist/MTooltip-CK39Zon4.js.map +1 -0
  24. package/dist/_useFieldValidation-Bz34IAQY.js +191 -0
  25. package/dist/_useFieldValidation-Bz34IAQY.js.map +1 -0
  26. package/dist/api.json +1 -1
  27. package/dist/code-editor.js +184 -103
  28. package/dist/code-editor.js.map +1 -1
  29. package/dist/components/MAddressField.vue.d.ts +18 -3
  30. package/dist/components/MAppBarWithSearch.vue.d.ts +1 -1
  31. package/dist/components/MAudioRecorder.vue.d.ts +2 -2
  32. package/dist/components/MAutocomplete.vue.d.ts +22 -3
  33. package/dist/components/MCheckbox.vue.d.ts +23 -1
  34. package/dist/components/MCodeEditor.vue.d.ts +23 -1
  35. package/dist/components/MColorPicker.vue.d.ts +24 -3
  36. package/dist/components/MConfirmDialog.vue.d.ts +2 -2
  37. package/dist/components/MCountrySelect.vue.d.ts +13 -2
  38. package/dist/components/MCreditCardInput.vue.d.ts +33 -0
  39. package/dist/components/MCronField.vue.d.ts +23 -1
  40. package/dist/components/MDataTable.vue.d.ts +1 -1
  41. package/dist/components/MDatePicker.vue.d.ts +24 -3
  42. package/dist/components/MDateRangePicker.vue.d.ts +24 -3
  43. package/dist/components/MFileUpload.vue.d.ts +23 -2
  44. package/dist/components/MForm.vue.d.ts +61 -0
  45. package/dist/components/MJsonEditor.vue.d.ts +23 -1
  46. package/dist/components/MMapRoute.vue.d.ts +168 -0
  47. package/dist/components/MMaskField.vue.d.ts +20 -2
  48. package/dist/components/MMentionField.vue.d.ts +13 -2
  49. package/dist/components/MMultiAutocomplete.vue.d.ts +22 -3
  50. package/dist/components/MMultiSelect.vue.d.ts +22 -3
  51. package/dist/components/MNavigationRail.vue.d.ts +1 -1
  52. package/dist/components/MNotificationCenter.vue.d.ts +2 -2
  53. package/dist/components/MNumberField.vue.d.ts +20 -2
  54. package/dist/components/MPhoneInput.vue.d.ts +19 -4
  55. package/dist/components/MPinInput.vue.d.ts +24 -3
  56. package/dist/components/MPosKeypad.vue.d.ts +2 -2
  57. package/dist/components/MRadioGroup.vue.d.ts +23 -1
  58. package/dist/components/MRating.vue.d.ts +23 -1
  59. package/dist/components/MRichTextEditor.vue.d.ts +23 -1
  60. package/dist/components/MSearchBar.vue.d.ts +1 -1
  61. package/dist/components/MSegmentedButton.vue.d.ts +23 -2
  62. package/dist/components/MSelect.vue.d.ts +22 -3
  63. package/dist/components/MSignaturePad.vue.d.ts +19 -0
  64. package/dist/components/MSlider.vue.d.ts +23 -1
  65. package/dist/components/MSwitch.vue.d.ts +23 -1
  66. package/dist/components/MTable.vue.d.ts +1 -1
  67. package/dist/components/MTagInput.vue.d.ts +20 -2
  68. package/dist/components/MText.vue.d.ts +1 -1
  69. package/dist/components/MTextField.vue.d.ts +26 -3
  70. package/dist/components/MTimePicker.vue.d.ts +24 -3
  71. package/dist/components/MTimezoneSelect.vue.d.ts +13 -2
  72. package/dist/components/MTour.vue.d.ts +2 -2
  73. package/dist/components/MTrackingTimeline.vue.d.ts +75 -0
  74. package/dist/components/MWindow.vue.d.ts +2 -2
  75. package/dist/composables/_routeGeometry.d.ts +26 -0
  76. package/dist/composables/_useFieldValidation.d.ts +91 -0
  77. package/dist/composables/mapRouting.d.ts +35 -0
  78. package/dist/composables/useLocale.d.ts +23 -0
  79. package/dist/composables/validation.d.ts +82 -0
  80. package/dist/draw.js +27 -26
  81. package/dist/draw.js.map +1 -1
  82. package/dist/flow.js +1 -1
  83. package/dist/gantt.js +2 -2
  84. package/dist/index.d.ts +5 -0
  85. package/dist/locales/index.js +154 -7
  86. package/dist/locales/index.js.map +1 -1
  87. package/dist/m3ui-vue.css +1 -1
  88. package/dist/m3ui.js +7350 -6183
  89. package/dist/m3ui.js.map +1 -1
  90. package/dist/map.d.ts +4 -0
  91. package/dist/map.js +1319 -867
  92. package/dist/map.js.map +1 -1
  93. package/dist/media.js +4 -4
  94. package/dist/pdf.js +4 -4
  95. package/dist/rich-text-editor.js +189 -146
  96. package/dist/rich-text-editor.js.map +1 -1
  97. package/dist/styles.css +1 -1
  98. package/dist/{useLocale-DaVSTdm0.js → useLocale-Dh1xZZ8i.js} +22 -1
  99. package/dist/useLocale-Dh1xZZ8i.js.map +1 -0
  100. package/package.json +1 -1
  101. package/src/__tests__/MForm.test.ts +325 -0
  102. package/src/__tests__/MMapRoute.test.ts +384 -0
  103. package/src/__tests__/MPhoneInput.test.ts +5 -2
  104. package/src/__tests__/MRichTextEditor.test.ts +2 -1
  105. package/src/__tests__/MTrackingTimeline.test.ts +66 -0
  106. package/src/__tests__/_fakeMaplibre.ts +4 -1
  107. package/src/__tests__/_osrmRoutes.ts +10 -0
  108. package/src/__tests__/attrsFallthrough.test.ts +11 -0
  109. package/src/__tests__/fieldsValidation.test.ts +229 -0
  110. package/src/__tests__/ssr.test.ts +1 -0
  111. package/src/__tests__/validation.test.ts +77 -0
  112. package/src/components/MAddressField.vue +45 -8
  113. package/src/components/MAudioRecorder.vue +9 -9
  114. package/src/components/MAutocomplete.vue +69 -23
  115. package/src/components/MCheckbox.vue +80 -17
  116. package/src/components/MCodeEditor.vue +59 -2
  117. package/src/components/MColorPicker.vue +65 -16
  118. package/src/components/MCountrySelect.vue +17 -3
  119. package/src/components/MCreditCardInput.vue +76 -7
  120. package/src/components/MCronField.vue +60 -10
  121. package/src/components/MDatePicker.vue +65 -16
  122. package/src/components/MDateRangePicker.vue +66 -16
  123. package/src/components/MFileUpload.vue +71 -8
  124. package/src/components/MForm.vue +208 -0
  125. package/src/components/MJsonEditor.vue +57 -3
  126. package/src/components/MMapBaseLayers.vue +29 -28
  127. package/src/components/MMapMeasure.vue +1 -1
  128. package/src/components/MMapRoute.vue +470 -0
  129. package/src/components/MMaskField.vue +55 -13
  130. package/src/components/MMentionField.vue +17 -2
  131. package/src/components/MMultiAutocomplete.vue +70 -23
  132. package/src/components/MMultiSelect.vue +70 -23
  133. package/src/components/MNumberField.vue +58 -16
  134. package/src/components/MPhoneInput.vue +60 -18
  135. package/src/components/MPinInput.vue +58 -10
  136. package/src/components/MRadioGroup.vue +65 -4
  137. package/src/components/MRating.vue +62 -6
  138. package/src/components/MRichTextEditor.vue +64 -4
  139. package/src/components/MSegmentedButton.vue +59 -2
  140. package/src/components/MSelect.vue +72 -26
  141. package/src/components/MSignaturePad.vue +71 -6
  142. package/src/components/MSlider.vue +61 -10
  143. package/src/components/MSwitch.vue +63 -8
  144. package/src/components/MTagInput.vue +67 -25
  145. package/src/components/MTextField.vue +68 -14
  146. package/src/components/MTimePicker.vue +65 -16
  147. package/src/components/MTimezoneSelect.vue +17 -3
  148. package/src/components/MTrackingTimeline.vue +257 -0
  149. package/src/composables/_routeGeometry.ts +140 -0
  150. package/src/composables/_useFieldValidation.ts +287 -0
  151. package/src/composables/mapRouting.ts +55 -0
  152. package/src/composables/useLocale.ts +46 -0
  153. package/src/composables/validation.ts +178 -0
  154. package/src/index.ts +5 -0
  155. package/src/locales/de.ts +21 -0
  156. package/src/locales/es.ts +28 -7
  157. package/src/locales/fr.ts +21 -0
  158. package/src/locales/ja.ts +21 -0
  159. package/src/locales/ko.ts +21 -0
  160. package/src/locales/pt.ts +21 -0
  161. package/src/locales/zh.ts +21 -0
  162. package/src/map.ts +4 -0
  163. package/dist/MListItem-D2cNUyzP.js.map +0 -1
  164. package/dist/MSegmentedButton-BEB4gXLA.js +0 -64
  165. package/dist/MSegmentedButton-BEB4gXLA.js.map +0 -1
  166. package/dist/MSlider-BIkA6DiE.js +0 -690
  167. package/dist/MSlider-BIkA6DiE.js.map +0 -1
  168. package/dist/MTextField-CNH-byir.js.map +0 -1
  169. package/dist/MTooltip-Dd5g2zhh.js +0 -754
  170. package/dist/MTooltip-Dd5g2zhh.js.map +0 -1
  171. package/dist/useLocale-DaVSTdm0.js.map +0 -1
@@ -24,8 +24,11 @@ export type CreditCardField = 'number' | 'expiry' | 'cvc' | 'name'
24
24
  * @event update:modelValue Emitted on every change — powers v-model
25
25
  * @event complete Emitted when every field becomes valid, with the value
26
26
  * @event brand Emitted when the detected network changes
27
+ * @event blur Emitted when focus leaves the card fields
27
28
  */
28
- import { computed, nextTick, onMounted, reactive, ref, watch } from 'vue'
29
+ import { computed, nextTick, onMounted, provide, reactive, ref, watch } from 'vue'
30
+ import { FORM_KEY, useFieldValidation, useFocusLeave, type FormContext } from '../composables/_useFieldValidation'
31
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
29
32
  import MTextField from './MTextField.vue'
30
33
  import { useLocale } from '../composables/useLocale'
31
34
  import {
@@ -54,6 +57,20 @@ const props = withDefaults(
54
57
  errors?: Partial<Record<CreditCardField, string>>
55
58
  /** Disables every field */
56
59
  disabled?: boolean
60
+ /** Error state for the whole card (e.g. a decline). A string is also the message */
61
+ error?: boolean | string
62
+ /** Error message shown when `error` is true */
63
+ errorLabel?: string
64
+ /** Inside an `MForm` or with `rules`, an empty card fails (an empty optional card passes; a half-typed one never does) */
65
+ required?: boolean
66
+ /** More validation rules on the whole value (functions or a Standard Schema); see `MForm` */
67
+ rules?: ValidationRule | ValidationRule[]
68
+ /** When the rules are checked and shown: on leaving the card fields (default), while typing, or only on submit */
69
+ validateOn?: ValidateOn
70
+ /** 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 */
71
+ validateDebounce?: number
72
+ /** The key of the card value in its `MForm` */
73
+ name?: string
57
74
  }>(),
58
75
  {
59
76
  preview: true,
@@ -68,6 +85,7 @@ const emit = defineEmits<{
68
85
  'update:modelValue': [CreditCardValue]
69
86
  complete: [CreditCardValue]
70
87
  brand: [CardBrand]
88
+ blur: []
71
89
  }>()
72
90
 
73
91
  const numberField = ref<InstanceType<typeof MTextField> | null>(null)
@@ -101,6 +119,50 @@ const messages: Record<CreditCardField, () => string> = {
101
119
  name: () => '',
102
120
  }
103
121
 
122
+ const isBlank = (v: CreditCardValue) => !v.number && !v.expiry && !v.cvc && !v.name?.trim()
123
+ // The card's own checks are one more rule: an empty card passes unless required, anything typed must be valid
124
+ const cardRule: ValidationRule<CreditCardValue> = (v) => {
125
+ if (isBlank(v ?? ({} as CreditCardValue)) || valid.value) return true
126
+ const bad = (['number', 'expiry', 'cvc', 'name'] as const).find((f) => !validity.value[f])!
127
+ return messages[bad]() || locale.validationRequired
128
+ }
129
+ const rootEl = ref<HTMLElement | null>(null)
130
+ const touchAll = () => { touched.number = touched.expiry = touched.cvc = touched.name = true }
131
+ const validation = useFieldValidation({
132
+ value: () => props.modelValue,
133
+ rules: () => [...(props.rules ? (Array.isArray(props.rules) ? props.rules : [props.rules]) : []), cardRule],
134
+ required: () => props.required,
135
+ error: () => props.error,
136
+ errorLabel: () => props.errorLabel,
137
+ name: () => props.name,
138
+ validateOn: () => props.validateOn,
139
+ validateDebounce: () => props.validateDebounce,
140
+ disabled: () => props.disabled,
141
+ focus: () => {
142
+ const bad = (['number', 'name', 'expiry', 'cvc'] as const).find((f) => !!errorFor(f)) ?? 'number'
143
+ ;({ number: numberField, expiry: expiryField, cvc: cvcField, name: nameField })[bad].value?.focus()
144
+ },
145
+ el: () => rootEl.value,
146
+ setValue: (v) => emit('update:modelValue', v as CreditCardValue),
147
+ isEmpty: (v) => isBlank(v as CreditCardValue),
148
+ onValidate: touchAll,
149
+ })
150
+ /** Its own `disabled` or its MForm's */
151
+ const isDisabled = validation.disabled
152
+ const { message: cardMessage, messageId } = validation
153
+ // Leaving the card (not moving between its fields) shows every field's error, like a submit
154
+ const onCardLeave = useFocusLeave(() => rootEl.value, () => {
155
+ touchAll()
156
+ validation.onBlur()
157
+ emit('blur')
158
+ })
159
+ // Its MTextFields are its own: the form sees one field, the card
160
+ provide(FORM_KEY, null as unknown as FormContext)
161
+ /** A message for the whole card (from outside or the consumer's rules), when no field already shows one */
162
+ const cardError = computed(() =>
163
+ cardMessage.value && !(['number', 'expiry', 'cvc', 'name'] as const).some((f) => errorFor(f)) ? cardMessage.value : null,
164
+ )
165
+
104
166
  function errorFor(field: CreditCardField): string | undefined {
105
167
  if (props.errors?.[field]) return props.errors[field]
106
168
  if (!touched[field] || validity.value[field]) return undefined
@@ -222,9 +284,15 @@ defineExpose({
222
284
  valid,
223
285
  /** Shows the errors of every field (as if each had been visited) and returns whether all are valid */
224
286
  validate: () => {
225
- touched.number = touched.expiry = touched.cvc = touched.name = true
287
+ touchAll()
288
+ void validation.validate()
226
289
  return valid.value
227
290
  },
291
+ /** Hides every error until the fields are left again */
292
+ resetValidation: () => {
293
+ touched.number = touched.expiry = touched.cvc = touched.name = false
294
+ validation.resetValidation()
295
+ },
228
296
  /** Focuses a field (the number by default) */
229
297
  focus: (field: CreditCardField = 'number') =>
230
298
  ({ number: numberField, expiry: expiryField, cvc: cvcField, name: nameField })[field].value?.focus(),
@@ -232,7 +300,7 @@ defineExpose({
232
300
  </script>
233
301
 
234
302
  <template>
235
- <div class="flex flex-col gap-4">
303
+ <div ref="rootEl" class="flex flex-col gap-4" @focusout="onCardLeave">
236
304
  <div v-if="preview" class="m3-cc-preview mx-auto w-full max-w-[340px]" aria-hidden="true">
237
305
  <div class="m3-cc-preview__inner relative aspect-[1.586]" :class="{ 'is-flipped': cvcFocused }">
238
306
  <!-- Front -->
@@ -269,7 +337,7 @@ defineExpose({
269
337
  :model-value="formattedNumber"
270
338
  :label="locale.cardNumber"
271
339
  :variant="variant"
272
- :disabled="disabled"
340
+ :disabled="isDisabled"
273
341
  autocomplete="cc-number"
274
342
  leading-icon="credit_card"
275
343
  :error="!!errorFor('number')"
@@ -283,7 +351,7 @@ defineExpose({
283
351
  :model-value="modelValue.name ?? ''"
284
352
  :label="locale.cardName"
285
353
  :variant="variant"
286
- :disabled="disabled"
354
+ :disabled="isDisabled"
287
355
  autocomplete="cc-name"
288
356
  :error="!!errors?.name"
289
357
  :error-label="errors?.name"
@@ -297,7 +365,7 @@ defineExpose({
297
365
  :model-value="modelValue.expiry ?? ''"
298
366
  :label="locale.cardExpiry"
299
367
  :variant="variant"
300
- :disabled="disabled"
368
+ :disabled="isDisabled"
301
369
  autocomplete="cc-exp"
302
370
  :error="!!errorFor('expiry')"
303
371
  :error-label="errorFor('expiry')"
@@ -310,7 +378,7 @@ defineExpose({
310
378
  :model-value="modelValue.cvc ?? ''"
311
379
  :label="locale.cardCvc"
312
380
  :variant="variant"
313
- :disabled="disabled"
381
+ :disabled="isDisabled"
314
382
  autocomplete="cc-csc"
315
383
  :error="!!errorFor('cvc')"
316
384
  :error-label="errorFor('cvc')"
@@ -319,6 +387,7 @@ defineExpose({
319
387
  </div>
320
388
  </div>
321
389
  </div>
390
+ <p v-if="cardError" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ cardError }}</p>
322
391
  </div>
323
392
  </template>
324
393
 
@@ -6,8 +6,11 @@
6
6
  * Anything else stays editable as text (Custom). Invalid text shows an error and isn't emitted until it's valid.
7
7
  *
8
8
  * @event update:modelValue The expression changed (only valid ones)
9
+ * @event blur Emitted when focus leaves the whole field
9
10
  */
10
- import { computed, ref, watch } from 'vue'
11
+ import { computed, provide, ref, watch } from 'vue'
12
+ import { FORM_KEY, useFieldValidation, useFocusLeave, type FormContext } from '../composables/_useFieldValidation'
13
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
11
14
  import MChip from './MChip.vue'
12
15
  import MIcon from './MIcon.vue'
13
16
  import MNumberField from './MNumberField.vue'
@@ -32,11 +35,51 @@ const props = withDefaults(
32
35
  variant?: 'filled' | 'outlined'
33
36
  /** Disables it */
34
37
  disabled?: boolean
38
+ /** Error state. A string is also the message */
39
+ error?: boolean | string
40
+ /** Error message shown when `error` is true */
41
+ errorLabel?: string
42
+ /** Marks it as required; inside an `MForm` or with `rules`, an expression must be set */
43
+ required?: boolean
44
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
45
+ rules?: ValidationRule | ValidationRule[]
46
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
47
+ validateOn?: ValidateOn
48
+ /** 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 */
49
+ validateDebounce?: number
50
+ /** The key of this field in its `MForm` */
51
+ name?: string
35
52
  }>(),
36
53
  { builder: true, nextRuns: 3, variant: 'filled', disabled: false },
37
54
  )
38
55
 
39
- const emit = defineEmits<{ 'update:modelValue': [expression: string] }>()
56
+ const emit = defineEmits<{ 'update:modelValue': [expression: string]; blur: [] }>()
57
+
58
+ const rootEl = ref<HTMLElement | null>(null)
59
+
60
+ const validation = useFieldValidation({
61
+ value: () => props.modelValue,
62
+ rules: () => props.rules,
63
+ required: () => props.required,
64
+ error: () => props.error,
65
+ errorLabel: () => props.errorLabel,
66
+ name: () => props.name,
67
+ validateOn: () => props.validateOn,
68
+ validateDebounce: () => props.validateDebounce,
69
+ disabled: () => props.disabled,
70
+ focus: () => { rootEl.value?.querySelector<HTMLElement>('input, button')?.focus() },
71
+ el: () => rootEl.value,
72
+ setValue: (v) => emit('update:modelValue', v as string),
73
+ })
74
+ /** Its own `disabled` or its MForm's */
75
+ const isDisabled = validation.disabled
76
+ const { hasError, message: errorMessage } = validation
77
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
78
+ validation.onBlur()
79
+ emit('blur')
80
+ })
81
+ // Its inner fields (every, time, the expression) are its own business, not the form's
82
+ provide(FORM_KEY, null as unknown as FormContext)
40
83
 
41
84
  const t = useLocale()
42
85
  const lang = computed(() => t.lang || undefined)
@@ -146,10 +189,17 @@ const FREQS = computed<{ value: Frequency; label: string }[]>(() => [
146
189
  // Monday first, as most calendars outside the US
147
190
  const WEEK = [1, 2, 3, 4, 5, 6, 0]
148
191
  const dayName = (d: number) => new Intl.DateTimeFormat(lang.value, { weekday: 'short' }).format(new Date(2024, 0, 7 + d))
192
+
193
+ defineExpose({
194
+ /** Checks its rules and shows the result; resolves to whether they pass */
195
+ validate: validation.validate,
196
+ /** Hides its validation error (until the next check) */
197
+ resetValidation: validation.resetValidation,
198
+ })
149
199
  </script>
150
200
 
151
201
  <template>
152
- <div class="m3-cron-field flex flex-col gap-3">
202
+ <div ref="rootEl" class="m3-cron-field flex flex-col gap-3" @focusout="onFocusOut">
153
203
  <template v-if="builder">
154
204
  <div class="flex flex-wrap gap-2" role="group" :aria-label="label ?? t.cronExpression">
155
205
  <MChip
@@ -157,7 +207,7 @@ const dayName = (d: number) => new Intl.DateTimeFormat(lang.value, { weekday: 's
157
207
  :key="f.value"
158
208
  type="filter"
159
209
  :selected="shown === f.value"
160
- :disabled="disabled"
210
+ :disabled="isDisabled"
161
211
  @click="pick(f.value)"
162
212
  >
163
213
  {{ f.label }}
@@ -172,7 +222,7 @@ const dayName = (d: number) => new Intl.DateTimeFormat(lang.value, { weekday: 's
172
222
  :min="1"
173
223
  :max="59"
174
224
  :variant="variant"
175
- :disabled="disabled"
225
+ :disabled="isDisabled"
176
226
  class="w-44"
177
227
  @update:model-value="setEvery"
178
228
  />
@@ -183,12 +233,12 @@ const dayName = (d: number) => new Intl.DateTimeFormat(lang.value, { weekday: 's
183
233
  :min="0"
184
234
  :max="59"
185
235
  :variant="variant"
186
- :disabled="disabled"
236
+ :disabled="isDisabled"
187
237
  class="w-44"
188
238
  @update:model-value="setMinute"
189
239
  />
190
240
  <template v-else>
191
- <MTimePicker :model-value="time" :label="t.cronTime" :minute-step="1" :disabled="disabled" class="w-44" @update:model-value="setTime" />
241
+ <MTimePicker :model-value="time" :label="t.cronTime" :minute-step="1" :disabled="isDisabled" class="w-44" @update:model-value="setTime" />
192
242
  <MNumberField
193
243
  v-if="shown === 'monthly'"
194
244
  :model-value="dayOfMonth"
@@ -196,7 +246,7 @@ const dayName = (d: number) => new Intl.DateTimeFormat(lang.value, { weekday: 's
196
246
  :min="1"
197
247
  :max="31"
198
248
  :variant="variant"
199
- :disabled="disabled"
249
+ :disabled="isDisabled"
200
250
  class="w-44"
201
251
  @update:model-value="setDay"
202
252
  />
@@ -222,8 +272,8 @@ const dayName = (d: number) => new Intl.DateTimeFormat(lang.value, { weekday: 's
222
272
  :label="label ?? t.cronExpression"
223
273
  :variant="variant"
224
274
  :disabled="disabled"
225
- :error="invalid"
226
- :error-label="t.cronInvalid"
275
+ :error="invalid ? t.cronInvalid : (errorMessage || hasError)"
276
+ :required="required"
227
277
  autocomplete="off"
228
278
  class="m3-cron-field__input"
229
279
  @update:model-value="onText"
@@ -5,6 +5,8 @@
5
5
  * @event update:modelValue Emitted with the picked date.
6
6
  */
7
7
  import { computed, onUnmounted, ref, watch } from 'vue'
8
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
9
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
8
10
  import MIcon from './MIcon.vue'
9
11
  import MIconButton from './MIconButton.vue'
10
12
  import MMonthYearButton from './_MMonthYearButton.vue'
@@ -34,10 +36,23 @@ const props = defineProps<{
34
36
  yearRange?: [number, number]
35
37
  /** Disables the picker */
36
38
  disabled?: boolean
37
- /** Error state */
38
- error?: boolean
39
+ /** Error state. A string is also the message (`:error="errors.x"`) */
40
+ error?: boolean | string
39
41
  /** Error message shown when error is true */
40
42
  errorLabel?: string
43
+ /** Marks as required — adds a red asterisk after the label; inside an `MForm` or with `rules`, also validates it */
44
+ required?: boolean
45
+ /**
46
+ * Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
47
+ * `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
48
+ */
49
+ rules?: ValidationRule | ValidationRule[]
50
+ /** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
51
+ validateOn?: ValidateOn
52
+ /** 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 */
53
+ validateDebounce?: number
54
+ /** The key of this field in its `MForm`'s values, errors and schema */
55
+ name?: string
41
56
  /** Hint text below the field */
42
57
  hint?: string
43
58
  /**
@@ -60,7 +75,26 @@ const localeStrings = useLocale()
60
75
  // to a hardcoded 'es-ES' regardless of what locale the app actually provided.
61
76
  const resolvedLocale = computed(() => props.locale ?? localeStrings.lang)
62
77
 
63
- const emit = defineEmits<{ 'update:modelValue': [string | null] }>()
78
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [string | null] }>()
79
+
80
+ const rootEl = ref<HTMLElement | null>(null)
81
+ const validation = useFieldValidation({
82
+ value: () => props.modelValue,
83
+ rules: () => props.rules,
84
+ required: () => props.required,
85
+ error: () => props.error,
86
+ errorLabel: () => props.errorLabel,
87
+ name: () => props.name,
88
+ validateOn: () => props.validateOn,
89
+ validateDebounce: () => props.validateDebounce,
90
+ disabled: () => props.disabled,
91
+ focus: () => { triggerEl.value?.querySelector('button')?.focus() },
92
+ el: () => triggerEl.value,
93
+ setValue: (v) => emit('update:modelValue', v as string | null),
94
+ })
95
+ /** Its own `disabled` or its MForm's */
96
+ const isDisabled = validation.disabled
97
+ const { hasError, message: errorMessage, messageId } = validation
64
98
 
65
99
  const open = ref(false)
66
100
  const triggerEl = ref<HTMLElement | null>(null)
@@ -165,26 +199,41 @@ watch(open, (v) => {
165
199
  onUnmounted(() => {
166
200
  document.removeEventListener('mousedown', onClickOutside)
167
201
  })
202
+
203
+ defineExpose({
204
+ /** Checks its rules and shows the result; resolves to whether they pass */
205
+ validate: validation.validate,
206
+ /** Hides its validation error (until the next check) */
207
+ resetValidation: validation.resetValidation,
208
+ })
209
+
210
+ // Last: it watches `open`, declared above
211
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
212
+ validation.onBlur()
213
+ emit('blur')
214
+ }, () => open.value)
168
215
  </script>
169
216
 
170
217
  <template>
171
- <div class="flex flex-col gap-1">
218
+ <div ref="rootEl" class="flex flex-col gap-1" @focusout="onFocusOut">
172
219
  <!-- Trigger -->
173
220
  <div ref="triggerEl" class="relative mt-2" :style="{ '--field-bg': resolvedFieldBg }">
174
221
  <button
222
+ :aria-invalid="hasError || undefined"
223
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
175
224
  type="button"
176
225
  class="flex h-14 w-full items-center gap-2 rounded-xs border bg-transparent px-4 text-left text-body-large transition-[border-color,border-width] duration-150"
177
226
  :class="[
178
- disabled ? 'pointer-events-none cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
179
- !disabled && (open
180
- ? error ? 'border-2 border-error' : 'border-2 border-primary'
181
- : error ? 'border-error' : 'border-outline hover:border-on-surface'),
227
+ isDisabled ? 'pointer-events-none cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
228
+ !isDisabled && (open
229
+ ? hasError ? 'border-2 border-error' : 'border-2 border-primary'
230
+ : hasError ? 'border-error' : 'border-outline hover:border-on-surface'),
182
231
  ]"
183
- @click="!disabled && (open = !open)"
232
+ @click="!isDisabled && (open = !open)"
184
233
  >
185
- <MIcon name="calendar_today" :size="24" class="shrink-0" :class="disabled ? 'text-on-surface/38' : 'text-on-surface-variant'" />
186
- <span v-overflow-title v-if="displayValue" class="min-w-0 truncate flex-1" :class="disabled ? 'text-on-surface/38' : 'text-on-surface'">{{ displayValue }}</span>
187
- <span v-overflow-title v-else class="min-w-0 truncate flex-1" :class="disabled ? 'text-on-surface/38' : 'text-on-surface-variant'">{{ placeholder || label || localeStrings.selectDate }}</span>
234
+ <MIcon name="calendar_today" :size="24" class="shrink-0" :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant'" />
235
+ <span v-overflow-title v-if="displayValue" class="min-w-0 truncate flex-1" :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface'">{{ displayValue }}</span>
236
+ <span v-overflow-title v-else class="min-w-0 truncate flex-1" :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant'">{{ placeholder || label || localeStrings.selectDate }}</span>
188
237
  <MIcon
189
238
  v-if="modelValue"
190
239
  name="close"
@@ -195,14 +244,14 @@ onUnmounted(() => {
195
244
  </button>
196
245
  <label
197
246
  v-if="label"
198
- :class="[OUTLINED_FLOATED_LABEL, 'transition-colors', disabled ? 'text-on-surface/38' : open ? (error ? 'text-error' : 'text-primary') : error ? 'text-error' : 'text-on-surface-variant']"
247
+ :class="[OUTLINED_FLOATED_LABEL, 'transition-colors', isDisabled ? 'text-on-surface/38' : open ? (hasError ? 'text-error' : 'text-primary') : hasError ? 'text-error' : 'text-on-surface-variant']"
199
248
  >
200
- {{ label }}
249
+ {{ label }}<span v-if="required" class="text-error">&nbsp;*</span>
201
250
  </label>
202
251
  </div>
203
252
 
204
- <p v-if="error && errorLabel" class="px-4 text-body-small text-error">{{ errorLabel }}</p>
205
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
253
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
254
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
206
255
 
207
256
  <!-- Calendar dropdown -->
208
257
  <Teleport to="body">
@@ -5,6 +5,8 @@
5
5
  * @event update:modelValue Emitted with the picked range.
6
6
  */
7
7
  import { computed, onUnmounted, ref, watch } from 'vue'
8
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
9
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
8
10
  import MIcon from './MIcon.vue'
9
11
  import MIconButton from './MIconButton.vue'
10
12
  import MMonthYearButton from './_MMonthYearButton.vue'
@@ -39,10 +41,23 @@ const props = defineProps<{
39
41
  yearRange?: [number, number]
40
42
  /** Disables the picker */
41
43
  disabled?: boolean
42
- /** Error state */
43
- error?: boolean
44
+ /** Error state. A string is also the message (`:error="errors.x"`) */
45
+ error?: boolean | string
44
46
  /** Error message shown when error is true */
45
47
  errorLabel?: string
48
+ /** Marks as required — adds a red asterisk after the label; inside an `MForm` or with `rules`, also validates it */
49
+ required?: boolean
50
+ /**
51
+ * Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
52
+ * `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
53
+ */
54
+ rules?: ValidationRule | ValidationRule[]
55
+ /** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
56
+ validateOn?: ValidateOn
57
+ /** 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 */
58
+ validateDebounce?: number
59
+ /** The key of this field in its `MForm`'s values, errors and schema */
60
+ name?: string
46
61
  /** Hint text below the field */
47
62
  hint?: string
48
63
  /**
@@ -69,7 +84,27 @@ const localeStrings = useLocale()
69
84
  // to a hardcoded 'es-ES' regardless of what locale the app actually provided.
70
85
  const resolvedLocale = computed(() => props.locale ?? localeStrings.lang)
71
86
 
72
- const emit = defineEmits<{ 'update:modelValue': [DateRange] }>()
87
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [DateRange] }>()
88
+
89
+ const rootEl = ref<HTMLElement | null>(null)
90
+ const validation = useFieldValidation({
91
+ value: () => props.modelValue,
92
+ rules: () => props.rules,
93
+ required: () => props.required,
94
+ error: () => props.error,
95
+ errorLabel: () => props.errorLabel,
96
+ name: () => props.name,
97
+ validateOn: () => props.validateOn,
98
+ validateDebounce: () => props.validateDebounce,
99
+ disabled: () => props.disabled,
100
+ focus: () => { triggerEl.value?.querySelector('button')?.focus() },
101
+ el: () => triggerEl.value,
102
+ setValue: (v) => emit('update:modelValue', v as DateRange),
103
+ isEmpty: (v) => !(v as DateRange | null)?.start || !(v as DateRange | null)?.end,
104
+ })
105
+ /** Its own `disabled` or its MForm's */
106
+ const isDisabled = validation.disabled
107
+ const { hasError, message: errorMessage, messageId } = validation
73
108
 
74
109
  const open = ref(false)
75
110
  const triggerEl = ref<HTMLElement | null>(null)
@@ -201,25 +236,40 @@ watch(open, (v) => {
201
236
  onUnmounted(() => {
202
237
  document.removeEventListener('mousedown', onOut)
203
238
  })
239
+
240
+ defineExpose({
241
+ /** Checks its rules and shows the result; resolves to whether they pass */
242
+ validate: validation.validate,
243
+ /** Hides its validation error (until the next check) */
244
+ resetValidation: validation.resetValidation,
245
+ })
246
+
247
+ // Last: it watches `open`, declared above
248
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
249
+ validation.onBlur()
250
+ emit('blur')
251
+ }, () => open.value)
204
252
  </script>
205
253
 
206
254
  <template>
207
- <div class="flex flex-col gap-1">
255
+ <div ref="rootEl" class="flex flex-col gap-1" @focusout="onFocusOut">
208
256
  <div ref="triggerEl" class="relative mt-2" :style="{ '--field-bg': resolvedFieldBg }">
209
257
  <button
258
+ :aria-invalid="hasError || undefined"
259
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
210
260
  type="button"
211
261
  class="flex h-14 w-full items-center gap-2 rounded-xs border bg-transparent px-4 text-left text-body-large transition-[border-color,border-width] duration-150"
212
262
  :class="[
213
- disabled ? 'pointer-events-none cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
214
- !disabled && (open
215
- ? error ? 'border-2 border-error' : 'border-2 border-primary'
216
- : error ? 'border-error' : 'border-outline hover:border-on-surface'),
263
+ isDisabled ? 'pointer-events-none cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
264
+ !isDisabled && (open
265
+ ? hasError ? 'border-2 border-error' : 'border-2 border-primary'
266
+ : hasError ? 'border-error' : 'border-outline hover:border-on-surface'),
217
267
  ]"
218
- @click="!disabled && (open = !open)"
268
+ @click="!isDisabled && (open = !open)"
219
269
  >
220
- <MIcon name="date_range" :size="24" class="shrink-0" :class="disabled ? 'text-on-surface/38' : 'text-on-surface-variant'" />
221
- <span v-overflow-title v-if="displayValue" class="min-w-0 truncate flex-1" :class="disabled ? 'text-on-surface/38' : 'text-on-surface'">{{ displayValue }}</span>
222
- <span v-overflow-title v-else class="min-w-0 truncate flex-1" :class="disabled ? 'text-on-surface/38' : 'text-on-surface-variant'">{{ label || placeholder || localeStrings.selectRange }}</span>
270
+ <MIcon name="date_range" :size="24" class="shrink-0" :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant'" />
271
+ <span v-overflow-title v-if="displayValue" class="min-w-0 truncate flex-1" :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface'">{{ displayValue }}</span>
272
+ <span v-overflow-title v-else class="min-w-0 truncate flex-1" :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant'">{{ label || placeholder || localeStrings.selectRange }}</span>
223
273
  <MIcon
224
274
  v-if="modelValue.start || modelValue.end"
225
275
  name="close"
@@ -230,14 +280,14 @@ onUnmounted(() => {
230
280
  </button>
231
281
  <label
232
282
  v-if="label"
233
- :class="[OUTLINED_FLOATED_LABEL, 'transition-colors', disabled ? 'text-on-surface/38' : open ? (error ? 'text-error' : 'text-primary') : error ? 'text-error' : 'text-on-surface-variant']"
283
+ :class="[OUTLINED_FLOATED_LABEL, 'transition-colors', isDisabled ? 'text-on-surface/38' : open ? (hasError ? 'text-error' : 'text-primary') : hasError ? 'text-error' : 'text-on-surface-variant']"
234
284
  >
235
- {{ label }}
285
+ {{ label }}<span v-if="required" class="text-error">&nbsp;*</span>
236
286
  </label>
237
287
  </div>
238
288
 
239
- <p v-if="error && errorLabel" class="px-4 text-body-small text-error">{{ errorLabel }}</p>
240
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
289
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
290
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
241
291
 
242
292
  <Teleport to="body">
243
293
  <Transition