@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
@@ -12,10 +12,14 @@ export interface SegmentedOption<V extends string | number = string | number> {
12
12
  * A row of joined segments for choosing between a small set of related options — single or multi select.
13
13
  *
14
14
  * @event update:modelValue Emitted on selection change — powers v-model
15
+ * @event blur Emitted when focus leaves the segments
15
16
  */
17
+ import { ref } from 'vue'
16
18
  import MIcon from './MIcon.vue'
19
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
20
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
17
21
 
18
- withDefaults(defineProps<{
22
+ const props = withDefaults(defineProps<{
19
23
  /** Selected value, or values with `multiSelect` (v-model); typed after the options' values */
20
24
  modelValue: M
21
25
  /** The options to choose from; see `SegmentedOption`. */
@@ -26,9 +30,55 @@ withDefaults(defineProps<{
26
30
  density?: 'default' | 'comfortable' | 'compact'
27
31
  /** Selected segment color */
28
32
  color?: 'primary' | 'secondary' | 'tertiary'
33
+ /** Error state. A string is also the message */
34
+ error?: boolean | string
35
+ /** Error message shown when `error` is true */
36
+ errorLabel?: string
37
+ /** Inside an `MForm` or with `rules`, a segment must be selected */
38
+ required?: boolean
39
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
40
+ rules?: ValidationRule | ValidationRule[]
41
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
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` */
46
+ name?: string
29
47
  }>(), { multiSelect: false, density: 'default', color: 'primary' })
30
48
 
31
- const emit = defineEmits<{ 'update:modelValue': [M] }>()
49
+ // Segments + their message: attributes go on the segments
50
+ defineOptions({ inheritAttrs: false })
51
+
52
+ const emit = defineEmits<{ 'update:modelValue': [M]; blur: [] }>()
53
+
54
+ const rootEl = ref<HTMLElement | null>(null)
55
+
56
+ const validation = useFieldValidation({
57
+ value: () => props.modelValue,
58
+ rules: () => props.rules,
59
+ required: () => props.required,
60
+ error: () => props.error,
61
+ errorLabel: () => props.errorLabel,
62
+ name: () => props.name,
63
+ validateOn: () => props.validateOn,
64
+ validateDebounce: () => props.validateDebounce,
65
+ disabled: () => false,
66
+ focus: () => { rootEl.value?.querySelector<HTMLButtonElement>('button:not(:disabled)')?.focus() },
67
+ el: () => rootEl.value,
68
+ setValue: (v) => emit('update:modelValue', v as M),
69
+ })
70
+ const { hasError, message: errorMessage, messageId } = validation
71
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
72
+ validation.onBlur()
73
+ emit('blur')
74
+ })
75
+
76
+ defineExpose({
77
+ /** Checks its rules and shows the result; resolves to whether they pass */
78
+ validate: validation.validate,
79
+ /** Hides its validation error (until the next check) */
80
+ resetValidation: validation.resetValidation,
81
+ })
32
82
 
33
83
  function isSelected(value: V, modelValue: M) {
34
84
  return Array.isArray(modelValue) ? modelValue.includes(value) : modelValue === value
@@ -50,8 +100,13 @@ function toggle(opt: SegmentedOption<V>, modelValue: M, multi: boolean) {
50
100
 
51
101
  <template>
52
102
  <div
103
+ ref="rootEl"
104
+ v-bind="$attrs"
53
105
  class="inline-flex overflow-hidden rounded-full border border-outline"
54
106
  role="group"
107
+ :aria-invalid="hasError || undefined"
108
+ :aria-describedby="errorMessage ? messageId : undefined"
109
+ @focusout="onFocusOut"
55
110
  >
56
111
  <button
57
112
  v-for="(opt, i) in options"
@@ -89,4 +144,6 @@ function toggle(opt: SegmentedOption<V>, modelValue: M, multi: boolean) {
89
144
  <span>{{ opt.label }}</span>
90
145
  </button>
91
146
  </div>
147
+ <!-- M3's segmented button has no error state: the message says so -->
148
+ <p v-if="errorMessage" :id="messageId" role="alert" class="mt-1 text-body-small text-error">{{ errorMessage }}</p>
92
149
  </template>
@@ -5,6 +5,8 @@
5
5
  * @event update:modelValue Emitted with the selected option's value (`null` when the clear button empties it).
6
6
  */
7
7
  import { computed, ref, useId, onMounted, onUnmounted, 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 { useFieldBg } from '../composables/useFieldBg'
@@ -40,10 +42,21 @@ const props = withDefaults(
40
42
  mode?: 'docked' | 'modal'
41
43
  /** Disables the select */
42
44
  disabled?: boolean
43
- /** Error state */
44
- error?: boolean
45
+ /** Error state. A string is also the message (`:error="errors.x"`) */
46
+ error?: boolean | string
45
47
  /** Error message shown when error is true */
46
48
  errorLabel?: string
49
+ /**
50
+ * Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
51
+ * `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
52
+ */
53
+ rules?: ValidationRule | ValidationRule[]
54
+ /** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
55
+ validateOn?: ValidateOn
56
+ /** 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 */
57
+ validateDebounce?: number
58
+ /** The key of this field in its `MForm`'s values, errors and schema */
59
+ name?: string
47
60
  /** Hint text below the field */
48
61
  hint?: string
49
62
  /** Marks as required — adds a red asterisk after the label */
@@ -69,7 +82,26 @@ const props = withDefaults(
69
82
  },
70
83
  )
71
84
 
72
- const emit = defineEmits<{ 'update:modelValue': [unknown] }>()
85
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [unknown] }>()
86
+
87
+ const rootEl = ref<HTMLElement | null>(null)
88
+ const validation = useFieldValidation({
89
+ value: () => props.modelValue,
90
+ rules: () => props.rules,
91
+ required: () => props.required,
92
+ error: () => props.error,
93
+ errorLabel: () => props.errorLabel,
94
+ name: () => props.name,
95
+ validateOn: () => props.validateOn,
96
+ validateDebounce: () => props.validateDebounce,
97
+ disabled: () => props.disabled,
98
+ focus: () => { fieldEl.value?.querySelector<HTMLElement>('[tabindex]')?.focus() },
99
+ el: () => fieldEl.value,
100
+ setValue: (v) => emit('update:modelValue', v as unknown),
101
+ })
102
+ /** Its own `disabled` or its MForm's */
103
+ const isDisabled = validation.disabled
104
+ const { hasError, message: errorMessage, messageId } = validation
73
105
 
74
106
  const locale = useLocale()
75
107
  const id = useId()
@@ -115,7 +147,7 @@ function computeDropPos() {
115
147
  }
116
148
 
117
149
  function toggle() {
118
- if (props.disabled) return
150
+ if (isDisabled.value) return
119
151
  if (props.mode === 'modal') {
120
152
  modalOpen.value = !modalOpen.value
121
153
  return
@@ -183,26 +215,26 @@ const triggerClasses = computed(() => {
183
215
  const pl = props.leadingIcon ? 'pl-12' : 'pl-4'
184
216
  const base = [
185
217
  'flex w-full min-w-0 items-center text-body-large outline-none transition-[border-color,border-width] duration-150',
186
- props.disabled ? 'cursor-not-allowed' : 'cursor-pointer',
218
+ isDisabled.value ? 'cursor-not-allowed' : 'cursor-pointer',
187
219
  pl,
188
220
  FIELD_PADDING_RIGHT[rightInset.value],
189
221
  ]
190
222
 
191
223
  if (props.variant === 'outlined') {
192
- const border = props.disabled
224
+ const border = isDisabled.value
193
225
  ? 'border-on-surface/12'
194
226
  : highlighted.value
195
- ? (props.error ? 'border-2 border-error' : 'border-2 border-primary')
196
- : (props.error ? 'border-error' : 'border-outline hover:border-on-surface')
227
+ ? (hasError.value ? 'border-2 border-error' : 'border-2 border-primary')
228
+ : (hasError.value ? 'border-error' : 'border-outline hover:border-on-surface')
197
229
  return [...base, 'h-14 rounded-xs border bg-transparent', border].join(' ')
198
230
  }
199
231
 
200
- const border = props.disabled
232
+ const border = isDisabled.value
201
233
  ? 'border-on-surface/38'
202
234
  : highlighted.value
203
- ? (props.error ? 'border-b-2 border-error' : 'border-b-2 border-primary')
204
- : (props.error ? 'border-error' : 'border-on-surface-variant hover:border-on-surface')
205
- const bg = props.disabled
235
+ ? (hasError.value ? 'border-b-2 border-error' : 'border-b-2 border-primary')
236
+ : (hasError.value ? 'border-error' : 'border-on-surface-variant hover:border-on-surface')
237
+ const bg = isDisabled.value
206
238
  ? 'bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]'
207
239
  : 'bg-surface-container-highest'
208
240
  return [...base, 'h-14 rounded-t-xs border-b pt-6 pb-2', bg, border].join(' ')
@@ -216,17 +248,30 @@ const labelClasses = computed(() =>
216
248
  leading: !!props.leadingIcon,
217
249
  right: rightInset.value,
218
250
  floated: isFloated.value,
219
- color: props.disabled
251
+ color: isDisabled.value
220
252
  ? 'text-on-surface/38'
221
253
  : highlighted.value
222
- ? (props.error ? 'text-error' : 'text-primary')
223
- : (props.error ? 'text-error' : 'text-on-surface-variant'),
254
+ ? (hasError.value ? 'text-error' : 'text-primary')
255
+ : (hasError.value ? 'text-error' : 'text-on-surface-variant'),
224
256
  }),
225
257
  )
258
+
259
+ defineExpose({
260
+ /** Checks its rules and shows the result; resolves to whether they pass */
261
+ validate: validation.validate,
262
+ /** Hides its validation error (until the next check) */
263
+ resetValidation: validation.resetValidation,
264
+ })
265
+
266
+ // Last: it watches `open`, declared above
267
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
268
+ validation.onBlur()
269
+ emit('blur')
270
+ }, () => open.value)
226
271
  </script>
227
272
 
228
273
  <template>
229
- <div class="flex flex-col gap-1" v-bind="$attrs">
274
+ <div ref="rootEl" class="flex flex-col gap-1" v-bind="$attrs" @focusout="onFocusOut">
230
275
  <div
231
276
  ref="fieldEl"
232
277
  class="relative"
@@ -239,7 +284,7 @@ const labelClasses = computed(() =>
239
284
  class="pointer-events-none absolute left-3.5"
240
285
  :class="[
241
286
  variant === 'filled' ? 'top-4.5' : 'top-4',
242
- disabled ? 'text-on-surface/38' : 'text-on-surface-variant',
287
+ isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant',
243
288
  ]"
244
289
  >
245
290
  <MIcon :name="leadingIcon" :size="24" />
@@ -248,7 +293,8 @@ const labelClasses = computed(() =>
248
293
  <!-- Custom trigger -->
249
294
  <div
250
295
  :id="id"
251
- :tabindex="disabled ? -1 : 0"
296
+ :tabindex="isDisabled ? -1 : 0"
297
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
252
298
  role="combobox"
253
299
  aria-haspopup="listbox"
254
300
  :aria-expanded="open || modalOpen"
@@ -256,15 +302,15 @@ const labelClasses = computed(() =>
256
302
  :aria-activedescendant="(open || modalOpen) && selectedIndex >= 0 ? optionId(selectedIndex) : undefined"
257
303
  :aria-labelledby="label ? labelId : undefined"
258
304
  :aria-required="required || undefined"
259
- :aria-invalid="error || undefined"
260
- :aria-disabled="disabled"
261
- :class="[triggerClasses, disabled ? 'pointer-events-none' : '']"
305
+ :aria-invalid="hasError || undefined"
306
+ :aria-disabled="isDisabled"
307
+ :class="[triggerClasses, isDisabled ? 'pointer-events-none' : '']"
262
308
  @click="toggle"
263
309
  @keydown="onKeydown"
264
310
  @focus="focused = true"
265
311
  @blur="focused = false"
266
312
  >
267
- <span v-if="hasValue" v-overflow-title :class="[FIELD_VALUE, disabled ? 'text-on-surface/38' : 'text-on-surface']">{{ selectedLabel }}</span>
313
+ <span v-if="hasValue" v-overflow-title :class="[FIELD_VALUE, isDisabled ? 'text-on-surface/38' : 'text-on-surface']">{{ selectedLabel }}</span>
268
314
  </div>
269
315
 
270
316
  <!-- Floating label -->
@@ -275,7 +321,7 @@ const labelClasses = computed(() =>
275
321
  <!-- Clear button -->
276
322
  <button
277
323
  :aria-label="locale.clear"
278
- v-if="clearable && hasValue && !disabled"
324
+ v-if="clearable && hasValue && !isDisabled"
279
325
  type="button"
280
326
  class="absolute right-9 top-1/2 -translate-y-1/2 flex h-6 w-6 cursor-pointer items-center justify-center rounded-full text-on-surface-variant transition-colors hover:bg-on-surface/8 hover:text-on-surface"
281
327
  @click.stop="emit('update:modelValue', null); open = false"
@@ -291,14 +337,14 @@ const labelClasses = computed(() =>
291
337
  class="transition-transform duration-200"
292
338
  :class="[
293
339
  open || modalOpen ? 'rotate-180' : '',
294
- disabled ? 'text-on-surface/38' : 'text-on-surface-variant',
340
+ isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant',
295
341
  ]"
296
342
  />
297
343
  </div>
298
344
  </div>
299
345
 
300
- <p v-if="error && errorLabel" class="px-4 text-body-small text-error">{{ errorLabel }}</p>
301
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
346
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
347
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
302
348
  </div>
303
349
 
304
350
  <!-- Docked dropdown -->
@@ -6,8 +6,13 @@
6
6
  * @event end Fires after a stroke ends, with a PNG data: URL of the canvas
7
7
  * @event change Fires alongside end, and also after undo() — same data: URL payload
8
8
  * @event begin Fires on the first pointerdown of a new stroke
9
+ *
10
+ * In an `MForm` it validates as a field whose value is the PNG data: URL (null while empty): `required` asks for a
11
+ * signature, and the form's values carry it under `name`.
9
12
  */
10
13
  import { computed, onMounted, ref, watch } from 'vue'
14
+ import { useFieldValidation } from '../composables/_useFieldValidation'
15
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
11
16
 
12
17
  const props = withDefaults(
13
18
  defineProps<{
@@ -25,6 +30,20 @@ const props = withDefaults(
25
30
  variant?: 'filled' | 'outlined'
26
31
  /** Disables drawing */
27
32
  disabled?: boolean
33
+ /** Error state. A string is also the message */
34
+ error?: boolean | string
35
+ /** Error message shown when `error` is true */
36
+ errorLabel?: string
37
+ /** Inside an `MForm` or with `rules`, a signature must be drawn */
38
+ required?: boolean
39
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
40
+ rules?: ValidationRule | ValidationRule[]
41
+ /** When the rules are checked and shown: after each stroke (default), or only on submit */
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` */
46
+ name?: string
28
47
  }>(),
29
48
  {
30
49
  width: 400,
@@ -37,8 +56,34 @@ const props = withDefaults(
37
56
  },
38
57
  )
39
58
 
59
+ // Pad + its message: attributes go on the pad
60
+ defineOptions({ inheritAttrs: false })
61
+
40
62
  const emit = defineEmits<{ begin: []; end: [string]; change: [string] }>()
41
63
 
64
+ /** The signature as a data: URL, null while empty: what validation checks and an MForm submits */
65
+ const signature = ref<string | null>(null)
66
+ const rootEl = ref<HTMLElement | null>(null)
67
+ const validation = useFieldValidation({
68
+ value: () => signature.value,
69
+ rules: () => props.rules,
70
+ required: () => props.required,
71
+ error: () => props.error,
72
+ errorLabel: () => props.errorLabel,
73
+ name: () => props.name,
74
+ validateOn: () => props.validateOn,
75
+ validateDebounce: () => props.validateDebounce,
76
+ disabled: () => props.disabled,
77
+ // A canvas takes no focus: bring it into view instead
78
+ focus: () => rootEl.value?.scrollIntoView({ block: 'center', behavior: 'smooth' }),
79
+ el: () => rootEl.value,
80
+ // MForm's reset(): back to empty
81
+ setValue: () => clear(),
82
+ })
83
+ /** Its own `disabled` or its MForm's */
84
+ const isDisabled = validation.disabled
85
+ const { hasError, message: errorMessage, messageId } = validation
86
+
42
87
  type Point = { x: number; y: number }
43
88
 
44
89
  const canvasEl = ref<HTMLCanvasElement | null>(null)
@@ -79,7 +124,7 @@ function pointFromEvent(e: PointerEvent): Point {
79
124
  }
80
125
 
81
126
  function onPointerDown(e: PointerEvent) {
82
- if (props.disabled) return
127
+ if (isDisabled.value) return
83
128
  drawing.value = true
84
129
  strokes.value.push([pointFromEvent(e)])
85
130
  emit('begin')
@@ -96,19 +141,25 @@ function onPointerUp() {
96
141
  if (!drawing.value) return
97
142
  drawing.value = false
98
143
  const dataUrl = toDataURL()
144
+ signature.value = dataUrl
99
145
  emit('end', dataUrl)
100
146
  emit('change', dataUrl)
147
+ // A finished stroke counts as having used the field
148
+ validation.onBlur()
101
149
  }
102
150
 
103
151
  function clear() {
104
152
  strokes.value = []
153
+ signature.value = null
105
154
  redraw()
106
155
  }
107
156
 
108
157
  function undo() {
109
158
  strokes.value = strokes.value.slice(0, -1)
110
159
  redraw()
111
- emit('change', toDataURL())
160
+ const dataUrl = toDataURL()
161
+ signature.value = empty.value ? null : dataUrl
162
+ emit('change', dataUrl)
112
163
  }
113
164
 
114
165
  function isEmpty(): boolean {
@@ -123,20 +174,31 @@ watch([() => props.color, () => props.background, () => props.lineWidth], redraw
123
174
 
124
175
  onMounted(redraw)
125
176
 
126
- defineExpose({ clear, undo, isEmpty, toDataURL })
177
+ defineExpose({
178
+ clear,
179
+ undo,
180
+ isEmpty,
181
+ toDataURL,
182
+ /** Checks its rules and shows the result; resolves to whether they pass */
183
+ validate: validation.validate,
184
+ /** Hides its validation error (until the next check) */
185
+ resetValidation: validation.resetValidation,
186
+ })
127
187
 
128
188
  const containerClasses = computed(() => {
129
189
  if (props.variant === 'outlined') {
130
- return 'rounded-sm border border-outline'
190
+ return ['rounded-sm border', hasError.value ? 'border-error' : 'border-outline']
131
191
  }
132
- return 'rounded-t-sm bg-surface-container-highest border-b border-on-surface-variant'
192
+ return ['rounded-t-sm bg-surface-container-highest border-b', hasError.value ? 'border-error' : 'border-on-surface-variant']
133
193
  })
134
194
  </script>
135
195
 
136
196
  <template>
137
197
  <div
198
+ ref="rootEl"
199
+ v-bind="$attrs"
138
200
  class="inline-block touch-none"
139
- :class="[containerClasses, disabled ? 'cursor-not-allowed opacity-[0.38]' : 'cursor-crosshair']"
201
+ :class="[containerClasses, isDisabled ? 'cursor-not-allowed opacity-[0.38]' : 'cursor-crosshair']"
140
202
  >
141
203
  <canvas
142
204
  ref="canvasEl"
@@ -144,10 +206,13 @@ const containerClasses = computed(() => {
144
206
  :height="height"
145
207
  role="img"
146
208
  aria-label="Signature pad"
209
+ :aria-invalid="hasError || undefined"
210
+ :aria-describedby="errorMessage ? messageId : undefined"
147
211
  @pointerdown="onPointerDown"
148
212
  @pointermove="onPointerMove"
149
213
  @pointerup="onPointerUp"
150
214
  @pointerleave="onPointerUp"
151
215
  />
152
216
  </div>
217
+ <p v-if="errorMessage" :id="messageId" role="alert" class="mt-1 text-body-small text-error">{{ errorMessage }}</p>
153
218
  </template>
@@ -5,8 +5,11 @@
5
5
  *
6
6
  * @event update:modelValue Emitted while dragging and on key presses, with the new value (a `[low, high]` pair
7
7
  * for the range variant).
8
+ * @event blur Emitted when focus leaves the slider (both thumbs, for a range)
8
9
  */
9
10
  import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
11
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
12
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
10
13
  import MIcon from './MIcon.vue'
11
14
  import { useLocale } from '../composables/useLocale'
12
15
 
@@ -24,6 +27,20 @@ const props = withDefaults(
24
27
  disabled?: boolean
25
28
  /** Label text */
26
29
  label?: string
30
+ /** Error state. A string is also the message */
31
+ error?: boolean | string
32
+ /** Error message shown when `error` is true */
33
+ errorLabel?: string
34
+ /** Marks it as required (asterisk); a slider always has a value, so use `rules` (e.g. `min(1)`) for limits */
35
+ required?: boolean
36
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
37
+ rules?: ValidationRule | ValidationRule[]
38
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
39
+ validateOn?: ValidateOn
40
+ /** 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 */
41
+ validateDebounce?: number
42
+ /** The key of this field in its `MForm` */
43
+ name?: string
27
44
  /** Show current value beside label */
28
45
  showValue?: boolean
29
46
  /** Show value tooltip while dragging */
@@ -62,11 +79,34 @@ const props = withDefaults(
62
79
  },
63
80
  )
64
81
 
65
- const emit = defineEmits<{ 'update:modelValue': [number | [number, number]] }>()
82
+ const emit = defineEmits<{ 'update:modelValue': [number | [number, number]]; blur: [] }>()
66
83
 
67
84
  const t = useLocale()
68
85
  const trackEl = ref<HTMLElement>()
69
86
  const thumbEls = ref<HTMLElement[]>([])
87
+ const rootEl = ref<HTMLElement | null>(null)
88
+
89
+ const validation = useFieldValidation({
90
+ value: () => props.modelValue,
91
+ rules: () => props.rules,
92
+ required: () => props.required,
93
+ error: () => props.error,
94
+ errorLabel: () => props.errorLabel,
95
+ name: () => props.name,
96
+ validateOn: () => props.validateOn,
97
+ validateDebounce: () => props.validateDebounce,
98
+ disabled: () => props.disabled,
99
+ focus: () => { (isRange.value ? thumbEls.value[0] : trackEl.value)?.focus() },
100
+ el: () => rootEl.value,
101
+ setValue: (v) => emit('update:modelValue', v as number | [number, number]),
102
+ })
103
+ /** Its own `disabled` or its MForm's */
104
+ const isDisabled = validation.disabled
105
+ const { hasError, message: errorMessage, messageId } = validation
106
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
107
+ validation.onBlur()
108
+ emit('blur')
109
+ })
70
110
  // Range: 0 = the start (low) thumb, 1 = the end (high) one
71
111
  const dragging = ref<false | 'single' | 0 | 1>(false)
72
112
  const focused = ref(false)
@@ -229,10 +269,10 @@ const DISABLED_INACTIVE = 'color-mix(in srgb, var(--color-on-surface) 12%, trans
229
269
 
230
270
  // `ct` (fill + corner dot) and `thumbCt` (thumb bar/icon) each fall back to the
231
271
  // base `color` prop, but can be overridden independently via `fillColor`/`thumbColor`.
232
- const ct = computed(() => props.disabled
272
+ const ct = computed(() => isDisabled.value
233
273
  ? { active: DISABLED_ACTIVE, inactive: DISABLED_INACTIVE }
234
274
  : { active: resolveActive(props.fillColor, props.color), inactive: 'var(--color-secondary-container)' })
235
- const thumbCt = computed(() => props.disabled
275
+ const thumbCt = computed(() => isDisabled.value
236
276
  ? { active: DISABLED_ACTIVE, inactive: DISABLED_INACTIVE }
237
277
  : { active: resolveActive(props.thumbColor, props.color), inactive: 'var(--color-secondary-container)' })
238
278
 
@@ -262,7 +302,7 @@ function rangeValue(idx: 0 | 1, v: number): [number, number] {
262
302
  }
263
303
 
264
304
  function onPointerDown(e: PointerEvent) {
265
- if (props.disabled) return
305
+ if (isDisabled.value) return
266
306
  e.preventDefault()
267
307
  const v = valueFromEvent(e)
268
308
  if (isRange.value) {
@@ -320,7 +360,7 @@ function onPointerUp() {
320
360
  const KEY_DELTA: Record<string, number> = { ArrowRight: 1, ArrowUp: 1, ArrowLeft: -1, ArrowDown: -1 }
321
361
 
322
362
  function onKeyDown(e: KeyboardEvent) {
323
- if (props.disabled) return
363
+ if (isDisabled.value) return
324
364
  const delta = KEY_DELTA[e.key]
325
365
  if (delta !== undefined) {
326
366
  e.preventDefault()
@@ -332,7 +372,7 @@ function onKeyDown(e: KeyboardEvent) {
332
372
 
333
373
  // Range: each thumb is a slider of its own, moved by its keys within the other thumb
334
374
  function onThumbKeyDown(idx: 0 | 1, e: KeyboardEvent) {
335
- if (props.disabled) return
375
+ if (isDisabled.value) return
336
376
  const { lo, hi } = val.value
337
377
  const cur = idx === 0 ? lo : hi
338
378
  const delta = KEY_DELTA[e.key]
@@ -550,12 +590,19 @@ const tooltipPct = computed(() => {
550
590
  return dragging.value === 0 ? pctLo.value : pctHi.value
551
591
  return pctLo.value
552
592
  })
593
+
594
+ defineExpose({
595
+ /** Checks its rules and shows the result; resolves to whether they pass */
596
+ validate: validation.validate,
597
+ /** Hides its validation error (until the next check) */
598
+ resetValidation: validation.resetValidation,
599
+ })
553
600
  </script>
554
601
 
555
602
  <template>
556
- <div class="flex select-none" :class="isVertical ? 'h-full flex-col items-center' : 'flex-col gap-1'">
603
+ <div ref="rootEl" class="flex select-none" :class="isVertical ? 'h-full flex-col items-center' : 'flex-col gap-1'" @focusout="onFocusOut">
557
604
  <div v-if="label || showValue" class="flex items-center justify-between">
558
- <span v-if="label" class="text-label-large text-on-surface">{{ label }}</span>
605
+ <span v-if="label" class="text-label-large" :class="hasError ? 'text-error' : 'text-on-surface'">{{ label }}<span v-if="required" class="text-error">&nbsp;*</span></span>
559
606
  <span v-if="showValue" class="tabular-nums text-label-large text-on-surface-variant">{{ displayValue }}</span>
560
607
  </div>
561
608
 
@@ -569,10 +616,12 @@ const tooltipPct = computed(() => {
569
616
  :aria-valuenow="isRange ? undefined : (modelValue as number)"
570
617
  :aria-valuemin="isRange ? undefined : min"
571
618
  :aria-valuemax="isRange ? undefined : max"
572
- :aria-disabled="disabled || undefined"
619
+ :aria-disabled="isDisabled || undefined"
620
+ :aria-invalid="hasError || undefined"
621
+ :aria-describedby="errorMessage ? messageId : undefined"
573
622
  :aria-orientation="isRange ? undefined : orientation"
574
623
  class="group relative touch-none cursor-pointer outline-none"
575
- :class="disabled && 'cursor-not-allowed'"
624
+ :class="isDisabled && 'cursor-not-allowed'"
576
625
  :style="isVertical
577
626
  ? { width: `${s.hitArea}px`, minHeight: '160px', height: '100%' }
578
627
  : { height: `${s.hitArea}px`, width: '100%' }
@@ -772,5 +821,7 @@ const tooltipPct = computed(() => {
772
821
  </Transition>
773
822
  </div>
774
823
  </div>
824
+ <!-- M3's slider has no error state: only the message says so -->
825
+ <p v-if="errorMessage" :id="messageId" role="alert" class="text-body-small text-error">{{ errorMessage }}</p>
775
826
  </div>
776
827
  </template>