@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,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
@@ -9,8 +9,11 @@
9
9
  * @event update:modelValue Emitted with the new list whenever files are added, removed or cleared (v-model)
10
10
  * @event select Emitted with the newly accepted files whenever files are added (drop or picker)
11
11
  * @event remove Emitted when a file is removed from the list with its remove button
12
+ * @event blur Emitted when focus leaves the drop zone
12
13
  */
13
14
  import { ref, computed, reactive, watch, onBeforeUnmount } from 'vue'
15
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
16
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
14
17
  import MIcon from './MIcon.vue'
15
18
  import MIconButton from './MIconButton.vue'
16
19
  import MSpinner from './MSpinner.vue'
@@ -46,6 +49,20 @@ const props = withDefaults(
46
49
  removeLabel?: string
47
50
  /** Optional v-model. Bind it to own the list: clear it, or update each entry's status/progress. Unbound, the component keeps the list itself. */
48
51
  modelValue?: UploadFile[]
52
+ /** Error state. A string is also the message */
53
+ error?: boolean | string
54
+ /** Error message shown when `error` is true */
55
+ errorLabel?: string
56
+ /** Inside an `MForm` or with `rules`, at least one file must be chosen */
57
+ required?: boolean
58
+ /** Validation rules on the file list (functions or a Standard Schema); `minLength`/`maxLength` count files. See `MForm` */
59
+ rules?: ValidationRule | ValidationRule[]
60
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
61
+ validateOn?: ValidateOn
62
+ /** 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 */
63
+ validateDebounce?: number
64
+ /** The key of this field in its `MForm` */
65
+ name?: string
49
66
  }>(),
50
67
  {
51
68
  multiple: false,
@@ -57,6 +74,7 @@ const emit = defineEmits<{
57
74
  select: [UploadFile[]]
58
75
  remove: [UploadFile]
59
76
  'update:modelValue': [UploadFile[]]
77
+ blur: []
60
78
  }>()
61
79
 
62
80
  defineSlots<{
@@ -91,6 +109,29 @@ onBeforeUnmount(() => {
91
109
  })
92
110
  const dragging = ref(false)
93
111
  const inputRef = ref<HTMLInputElement | null>(null)
112
+ const zoneEl = ref<HTMLElement | null>(null)
113
+
114
+ const validation = useFieldValidation({
115
+ value: () => files.value,
116
+ rules: () => props.rules,
117
+ required: () => props.required,
118
+ error: () => props.error,
119
+ errorLabel: () => props.errorLabel,
120
+ name: () => props.name,
121
+ validateOn: () => props.validateOn,
122
+ validateDebounce: () => props.validateDebounce,
123
+ disabled: () => props.disabled,
124
+ focus: () => { zoneEl.value?.focus() },
125
+ el: () => zoneEl.value,
126
+ setValue: (v) => setFiles((v as UploadFile[] | undefined) ?? []),
127
+ })
128
+ /** Its own `disabled` or its MForm's */
129
+ const isDisabled = validation.disabled
130
+ const { hasError, message: errorMessage, messageId } = validation
131
+ const onFocusOut = useFocusLeave(() => zoneEl.value, () => {
132
+ validation.onBlur()
133
+ emit('blur')
134
+ })
94
135
 
95
136
  const acceptList = computed(() =>
96
137
  props.accept ? props.accept.split(',').map((s) => s.trim()) : null,
@@ -139,7 +180,7 @@ function processFiles(fileList: FileList | File[]) {
139
180
 
140
181
  function onDrop(e: DragEvent) {
141
182
  dragging.value = false
142
- if (props.disabled || !e.dataTransfer?.files.length) return
183
+ if (isDisabled.value || !e.dataTransfer?.files.length) return
143
184
  processFiles(e.dataTransfer.files)
144
185
  }
145
186
 
@@ -158,26 +199,45 @@ function clear() {
158
199
  if (files.value.length) setFiles([])
159
200
  }
160
201
 
161
- defineExpose({ clear, files })
162
-
163
202
  function openPicker() {
164
- if (!props.disabled) inputRef.value?.click()
203
+ if (!isDisabled.value) inputRef.value?.click()
165
204
  }
205
+
206
+ defineExpose({
207
+ clear,
208
+ files,
209
+ /** Checks its rules and shows the result; resolves to whether they pass */
210
+ validate: validation.validate,
211
+ /** Hides its validation error (until the next check) */
212
+ resetValidation: validation.resetValidation,
213
+ })
166
214
  </script>
167
215
 
168
216
  <template>
169
217
  <div class="flex flex-col gap-3">
170
218
  <!-- Drop zone -->
219
+ <!-- A button for the keyboard too: Enter/Space open the picker, and a form can focus it on an error -->
171
220
  <div
172
- class="relative flex min-h-[160px] cursor-pointer flex-col items-center justify-center gap-3 rounded-lg border-2 border-dashed p-6 transition-colors duration-150"
221
+ ref="zoneEl"
222
+ role="button"
223
+ :tabindex="isDisabled ? -1 : 0"
224
+ :aria-disabled="isDisabled || undefined"
225
+ :aria-invalid="hasError || undefined"
226
+ :aria-describedby="errorMessage ? messageId : undefined"
227
+ class="relative flex min-h-[160px] cursor-pointer flex-col items-center justify-center gap-3 rounded-lg border-2 border-dashed p-6 outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-secondary"
173
228
  :class="[
174
- disabled
229
+ isDisabled
175
230
  ? 'cursor-not-allowed border-outline-variant/50 bg-surface-container/30 opacity-60'
176
231
  : dragging
177
232
  ? 'border-primary bg-primary-container/20'
178
- : 'border-outline-variant bg-surface-container-lowest hover:border-primary/60 hover:bg-surface-container',
233
+ : hasError
234
+ ? 'border-error bg-surface-container-lowest hover:bg-surface-container'
235
+ : 'border-outline-variant bg-surface-container-lowest hover:border-primary/60 hover:bg-surface-container',
179
236
  ]"
180
237
  @click="openPicker"
238
+ @keydown.enter.prevent="openPicker"
239
+ @keydown.space.prevent="openPicker"
240
+ @focusout="onFocusOut"
181
241
  @dragenter.prevent="dragging = true"
182
242
  @dragover.prevent="dragging = true"
183
243
  @dragleave.prevent="dragging = false"
@@ -208,10 +268,13 @@ function openPicker() {
208
268
  class="hidden"
209
269
  :accept="accept"
210
270
  :multiple="multiple"
211
- :disabled="disabled"
271
+ :disabled="isDisabled"
272
+ :name="name"
212
273
  @change="onFileInput"
213
274
  />
214
275
 
276
+ <p v-if="errorMessage" :id="messageId" role="alert" class="-mt-1 px-1 text-body-small text-error">{{ errorMessage }}</p>
277
+
215
278
  <!-- File list -->
216
279
  <TransitionGroup
217
280
  name="m3-file"