@m3ui-vue/m3ui-vue 1.6.2 → 1.8.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 (191) hide show
  1. package/dist/API.md +1224 -28
  2. package/dist/{MColorPickerModal-NtkihoK6.js → MColorPickerModal-H8nuX0Wu.js} +3 -4
  3. package/dist/{MColorPickerModal-NtkihoK6.js.map → MColorPickerModal-H8nuX0Wu.js.map} +1 -1
  4. package/dist/MDialog-B42Mhn8W.js +331 -0
  5. package/dist/MDialog-B42Mhn8W.js.map +1 -0
  6. package/dist/{MListItem-D2cNUyzP.js → MListItem-DiOAxaN7.js} +431 -325
  7. package/dist/MListItem-DiOAxaN7.js.map +1 -0
  8. package/dist/{MLoadingIndicator-DWUj0BAB.js → MLoadingIndicator-CuTZYFFj.js} +2 -2
  9. package/dist/{MLoadingIndicator-DWUj0BAB.js.map → MLoadingIndicator-CuTZYFFj.js.map} +1 -1
  10. package/dist/{MSeekBar-C4cn9RIF.js → MSeekBar-BCDQTx2t.js} +2 -2
  11. package/dist/{MSeekBar-C4cn9RIF.js.map → MSeekBar-BCDQTx2t.js.map} +1 -1
  12. package/dist/MSegmentedButton-BXzRsg1S.js +105 -0
  13. package/dist/MSegmentedButton-BXzRsg1S.js.map +1 -0
  14. package/dist/MSignaturePad-BTqGaPXA.js +313 -0
  15. package/dist/MSignaturePad-BTqGaPXA.js.map +1 -0
  16. package/dist/MSlider-D_O8J9YH.js +739 -0
  17. package/dist/MSlider-D_O8J9YH.js.map +1 -0
  18. package/dist/{MSpinner-DZkXZJpc.js → MSpinner-UCdtSOHP.js} +2 -2
  19. package/dist/{MSpinner-DZkXZJpc.js.map → MSpinner-UCdtSOHP.js.map} +1 -1
  20. package/dist/{MTextField-CNH-byir.js → MTextField-CYk9yFvi.js} +162 -115
  21. package/dist/MTextField-CYk9yFvi.js.map +1 -0
  22. package/dist/_useFieldValidation-B0B44M_t.js +191 -0
  23. package/dist/_useFieldValidation-B0B44M_t.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/MFormBuilder.vue.d.ts +22 -0
  44. package/dist/components/MFormRenderer.vue.d.ts +151 -0
  45. package/dist/components/MJsonEditor.vue.d.ts +23 -1
  46. package/dist/components/MMapRoute.vue.d.ts +176 -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/MPdfSigner.vue.d.ts +499 -0
  55. package/dist/components/MPdfViewer.vue.d.ts +12 -0
  56. package/dist/components/MPhoneInput.vue.d.ts +19 -4
  57. package/dist/components/MPinInput.vue.d.ts +24 -3
  58. package/dist/components/MPosKeypad.vue.d.ts +3 -3
  59. package/dist/components/MRadioGroup.vue.d.ts +23 -1
  60. package/dist/components/MRating.vue.d.ts +23 -1
  61. package/dist/components/MRichTextEditor.vue.d.ts +23 -1
  62. package/dist/components/MSearchBar.vue.d.ts +1 -1
  63. package/dist/components/MSegmentedButton.vue.d.ts +23 -2
  64. package/dist/components/MSelect.vue.d.ts +22 -3
  65. package/dist/components/MSignaturePad.vue.d.ts +19 -0
  66. package/dist/components/MSlider.vue.d.ts +23 -1
  67. package/dist/components/MSpreadsheet.vue.d.ts +137 -0
  68. package/dist/components/MStack.vue.d.ts +1 -1
  69. package/dist/components/MSwitch.vue.d.ts +23 -1
  70. package/dist/components/MTable.vue.d.ts +1 -1
  71. package/dist/components/MTagInput.vue.d.ts +20 -2
  72. package/dist/components/MText.vue.d.ts +1 -1
  73. package/dist/components/MTextField.vue.d.ts +26 -3
  74. package/dist/components/MTimePicker.vue.d.ts +24 -3
  75. package/dist/components/MTimezoneSelect.vue.d.ts +13 -2
  76. package/dist/components/MTour.vue.d.ts +2 -2
  77. package/dist/components/MTrackingTimeline.vue.d.ts +75 -0
  78. package/dist/components/MWindow.vue.d.ts +2 -2
  79. package/dist/composables/_routeGeometry.d.ts +26 -0
  80. package/dist/composables/_useFieldValidation.d.ts +91 -0
  81. package/dist/composables/mapRouting.d.ts +35 -0
  82. package/dist/composables/pdfSign.d.ts +52 -0
  83. package/dist/composables/spreadsheetFormula.d.ts +55 -0
  84. package/dist/composables/useLocale.d.ts +177 -0
  85. package/dist/composables/validation.d.ts +82 -0
  86. package/dist/draw.js +2 -2
  87. package/dist/flow.js +1 -1
  88. package/dist/gantt.js +2 -2
  89. package/dist/index.d.ts +12 -0
  90. package/dist/locales/index.js +693 -7
  91. package/dist/locales/index.js.map +1 -1
  92. package/dist/m3ui-vue.css +1 -1
  93. package/dist/m3ui.js +15507 -12417
  94. package/dist/m3ui.js.map +1 -1
  95. package/dist/map.d.ts +4 -0
  96. package/dist/map.js +1207 -745
  97. package/dist/map.js.map +1 -1
  98. package/dist/media.js +4 -4
  99. package/dist/pdf.d.ts +3 -0
  100. package/dist/pdf.js +1228 -687
  101. package/dist/pdf.js.map +1 -1
  102. package/dist/rich-text-editor.js +188 -146
  103. package/dist/rich-text-editor.js.map +1 -1
  104. package/dist/styles.css +1 -1
  105. package/dist/{useLocale-DaVSTdm0.js → useLocale-DL96OFgr.js} +99 -1
  106. package/dist/useLocale-DL96OFgr.js.map +1 -0
  107. package/package.json +5 -1
  108. package/src/__tests__/MForm.test.ts +325 -0
  109. package/src/__tests__/MFormRenderer.test.ts +157 -0
  110. package/src/__tests__/MMapRoute.test.ts +418 -0
  111. package/src/__tests__/MPdfSigner.test.ts +175 -0
  112. package/src/__tests__/MPhoneInput.test.ts +5 -2
  113. package/src/__tests__/MRichTextEditor.test.ts +2 -1
  114. package/src/__tests__/MSpreadsheet.test.ts +223 -0
  115. package/src/__tests__/MTrackingTimeline.test.ts +66 -0
  116. package/src/__tests__/_fakeMaplibre.ts +4 -1
  117. package/src/__tests__/_osrmRoutes.ts +10 -0
  118. package/src/__tests__/attrsFallthrough.test.ts +11 -0
  119. package/src/__tests__/fieldsValidation.test.ts +229 -0
  120. package/src/__tests__/spreadsheetFormula.test.ts +77 -0
  121. package/src/__tests__/ssr.test.ts +4 -0
  122. package/src/__tests__/validation.test.ts +77 -0
  123. package/src/components/MAddressField.vue +45 -8
  124. package/src/components/MAutocomplete.vue +69 -23
  125. package/src/components/MCheckbox.vue +80 -17
  126. package/src/components/MCodeEditor.vue +59 -2
  127. package/src/components/MColorPicker.vue +65 -16
  128. package/src/components/MCountrySelect.vue +17 -3
  129. package/src/components/MCreditCardInput.vue +76 -7
  130. package/src/components/MCronField.vue +60 -10
  131. package/src/components/MDatePicker.vue +65 -16
  132. package/src/components/MDateRangePicker.vue +66 -16
  133. package/src/components/MFileUpload.vue +71 -8
  134. package/src/components/MForm.vue +208 -0
  135. package/src/components/MFormBuilder.vue +417 -0
  136. package/src/components/MFormRenderer.vue +324 -0
  137. package/src/components/MJsonEditor.vue +57 -3
  138. package/src/components/MMapRoute.vue +523 -0
  139. package/src/components/MMaskField.vue +55 -13
  140. package/src/components/MMentionField.vue +17 -2
  141. package/src/components/MMultiAutocomplete.vue +70 -23
  142. package/src/components/MMultiSelect.vue +70 -23
  143. package/src/components/MNumberField.vue +58 -16
  144. package/src/components/MPdfSigner.vue +433 -0
  145. package/src/components/MPdfViewer.vue +12 -0
  146. package/src/components/MPhoneInput.vue +60 -18
  147. package/src/components/MPinInput.vue +58 -10
  148. package/src/components/MRadioGroup.vue +65 -4
  149. package/src/components/MRating.vue +62 -6
  150. package/src/components/MRichTextEditor.vue +64 -4
  151. package/src/components/MSegmentedButton.vue +59 -2
  152. package/src/components/MSelect.vue +72 -26
  153. package/src/components/MSignaturePad.vue +71 -6
  154. package/src/components/MSlider.vue +61 -10
  155. package/src/components/MSpreadsheet.vue +992 -0
  156. package/src/components/MSwitch.vue +63 -8
  157. package/src/components/MTagInput.vue +67 -25
  158. package/src/components/MTextField.vue +68 -14
  159. package/src/components/MTimePicker.vue +65 -16
  160. package/src/components/MTimezoneSelect.vue +17 -3
  161. package/src/components/MTrackingTimeline.vue +257 -0
  162. package/src/composables/_routeGeometry.ts +140 -0
  163. package/src/composables/_useFieldValidation.ts +287 -0
  164. package/src/composables/mapRouting.ts +55 -0
  165. package/src/composables/pdfSign.ts +141 -0
  166. package/src/composables/spreadsheetFormula.ts +459 -0
  167. package/src/composables/useLocale.ts +277 -0
  168. package/src/composables/validation.ts +178 -0
  169. package/src/index.ts +12 -0
  170. package/src/locales/de.ts +98 -0
  171. package/src/locales/es.ts +105 -7
  172. package/src/locales/fr.ts +98 -0
  173. package/src/locales/ja.ts +98 -0
  174. package/src/locales/ko.ts +98 -0
  175. package/src/locales/pt.ts +98 -0
  176. package/src/locales/zh.ts +98 -0
  177. package/src/map.ts +4 -0
  178. package/src/pdf.ts +3 -0
  179. package/dist/MButton-OQ4wIERQ.js +0 -171
  180. package/dist/MButton-OQ4wIERQ.js.map +0 -1
  181. package/dist/MDialog-DYMnaBlt.js +0 -167
  182. package/dist/MDialog-DYMnaBlt.js.map +0 -1
  183. package/dist/MListItem-D2cNUyzP.js.map +0 -1
  184. package/dist/MProgressBar-BWly1UKN.js +0 -179
  185. package/dist/MProgressBar-BWly1UKN.js.map +0 -1
  186. package/dist/MSegmentedButton-BEB4gXLA.js +0 -64
  187. package/dist/MSegmentedButton-BEB4gXLA.js.map +0 -1
  188. package/dist/MSlider-BIkA6DiE.js +0 -690
  189. package/dist/MSlider-BIkA6DiE.js.map +0 -1
  190. package/dist/MTextField-CNH-byir.js.map +0 -1
  191. package/dist/useLocale-DaVSTdm0.js.map +0 -1
@@ -4,8 +4,11 @@
4
4
  *
5
5
  * @slot actions Extra controls rendered in the header bar, before the built-in copy button
6
6
  * @event update:modelValue Emitted on every document change — powers v-model
7
+ * @event blur Emitted when focus leaves the editor
7
8
  */
8
9
  import { ref, watch, onMounted, onBeforeUnmount, computed } from 'vue'
10
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
11
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
9
12
  import MIcon from './MIcon.vue'
10
13
 
11
14
  type Language = 'javascript' | 'typescript' | 'json' | 'html' | 'css' | 'python' | 'vue' | 'c' | 'cpp' | 'rust' | 'asm' | 'plain'
@@ -28,6 +31,20 @@ const props = withDefaults(
28
31
  maxHeight?: string
29
32
  /** Placeholder text shown via CodeMirror when the editor is empty */
30
33
  placeholder?: string
34
+ /** Error state. A string is also the message */
35
+ error?: boolean | string
36
+ /** Error message shown when `error` is true */
37
+ errorLabel?: string
38
+ /** Inside an `MForm` or with `rules`, the code can't be blank */
39
+ required?: boolean
40
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
41
+ rules?: ValidationRule | ValidationRule[]
42
+ /** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
43
+ validateOn?: ValidateOn
44
+ /** 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 */
45
+ validateDebounce?: number
46
+ /** The key of this field in its `MForm` */
47
+ name?: string
31
48
  /** Word wrap */
32
49
  wrap?: boolean
33
50
  }>(),
@@ -42,10 +59,34 @@ const props = withDefaults(
42
59
  },
43
60
  )
44
61
 
45
- const emit = defineEmits<{ 'update:modelValue': [string] }>()
62
+ // Editor + its message: attributes go on the editor
63
+ defineOptions({ inheritAttrs: false })
64
+
65
+ const emit = defineEmits<{ 'update:modelValue': [string]; blur: [] }>()
46
66
 
47
67
  const containerRef = ref<HTMLElement | null>(null)
48
68
  let view: any = null
69
+ const rootEl = ref<HTMLElement | null>(null)
70
+
71
+ const validation = useFieldValidation({
72
+ value: () => props.modelValue,
73
+ rules: () => props.rules,
74
+ required: () => props.required,
75
+ error: () => props.error,
76
+ errorLabel: () => props.errorLabel,
77
+ name: () => props.name,
78
+ validateOn: () => props.validateOn,
79
+ validateDebounce: () => props.validateDebounce,
80
+ disabled: () => props.readonly,
81
+ focus: () => { view?.focus() },
82
+ el: () => rootEl.value,
83
+ setValue: (v) => emit('update:modelValue', v as string),
84
+ })
85
+ const { hasError, message: errorMessage, messageId } = validation
86
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
87
+ validation.onBlur()
88
+ emit('blur')
89
+ })
49
90
  let cmModules: any = null
50
91
  const copied = ref(false)
51
92
 
@@ -218,10 +259,25 @@ watch(() => props.modelValue, (val) => {
218
259
  watch([() => props.language, () => props.theme, () => props.readonly, () => props.lineNumbers, () => props.wrap, () => props.placeholder], createEditor)
219
260
 
220
261
  onBeforeUnmount(() => view?.destroy())
262
+
263
+ defineExpose({
264
+ /** Checks its rules and shows the result; resolves to whether they pass */
265
+ validate: validation.validate,
266
+ /** Hides its validation error (until the next check) */
267
+ resetValidation: validation.resetValidation,
268
+ })
221
269
  </script>
222
270
 
223
271
  <template>
224
- <div class="flex flex-col overflow-hidden rounded-lg border border-outline-variant">
272
+ <div
273
+ ref="rootEl"
274
+ v-bind="$attrs"
275
+ class="flex flex-col overflow-hidden rounded-lg border"
276
+ :class="hasError ? 'border-error' : 'border-outline-variant'"
277
+ :aria-invalid="hasError || undefined"
278
+ :aria-describedby="errorMessage ? messageId : undefined"
279
+ @focusout="onFocusOut"
280
+ >
225
281
  <!-- Header bar -->
226
282
  <div class="flex items-center justify-between border-b border-outline-variant bg-surface-container px-4 py-2">
227
283
  <span class="text-label-medium text-on-surface-variant">{{ langLabel }}</span>
@@ -246,6 +302,7 @@ onBeforeUnmount(() => view?.destroy())
246
302
  :style="{ minHeight, maxHeight }"
247
303
  />
248
304
  </div>
305
+ <p v-if="errorMessage" :id="messageId" role="alert" class="mt-1 px-4 text-body-small text-error">{{ errorMessage }}</p>
249
306
  </template>
250
307
 
251
308
  <style scoped>
@@ -7,6 +7,8 @@
7
7
  * @event update:modelValue Emitted with the picked color, live while dragging or typing.
8
8
  */
9
9
  import { nextTick, onUnmounted, ref, watch } from 'vue'
10
+ import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
11
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
10
12
  import MIcon from './MIcon.vue'
11
13
  import MColorPanel from './_MColorPanel.vue'
12
14
  import { useFieldBg } from '../composables/useFieldBg'
@@ -32,10 +34,23 @@ const props = withDefaults(defineProps<{
32
34
  eyedropper?: boolean
33
35
  /** Disables the picker */
34
36
  disabled?: boolean
35
- /** Error state */
36
- error?: boolean
37
+ /** Error state. A string is also the message (`:error="errors.x"`) */
38
+ error?: boolean | string
37
39
  /** Error message shown when error is true */
38
40
  errorLabel?: string
41
+ /** Marks as required — adds a red asterisk after the label; inside an `MForm` or with `rules`, also validates it */
42
+ required?: boolean
43
+ /**
44
+ * Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
45
+ * `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
46
+ */
47
+ rules?: ValidationRule | ValidationRule[]
48
+ /** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
49
+ validateOn?: ValidateOn
50
+ /** 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 */
51
+ validateDebounce?: number
52
+ /** The key of this field in its `MForm`'s values, errors and schema */
53
+ name?: string
39
54
  /** Hint text below the field */
40
55
  hint?: string
41
56
  /** Overrides the auto-detected background color used to cut the floating label out of the border */
@@ -48,7 +63,26 @@ const props = withDefaults(defineProps<{
48
63
  eyedropper: true,
49
64
  })
50
65
 
51
- const emit = defineEmits<{ 'update:modelValue': [string] }>()
66
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [string] }>()
67
+
68
+ const rootEl = ref<HTMLElement | null>(null)
69
+ const validation = useFieldValidation({
70
+ value: () => props.modelValue,
71
+ rules: () => props.rules,
72
+ required: () => props.required,
73
+ error: () => props.error,
74
+ errorLabel: () => props.errorLabel,
75
+ name: () => props.name,
76
+ validateOn: () => props.validateOn,
77
+ validateDebounce: () => props.validateDebounce,
78
+ disabled: () => props.disabled,
79
+ focus: () => { buttonEl.value?.focus() },
80
+ el: () => buttonEl.value,
81
+ setValue: (v) => emit('update:modelValue', v as string),
82
+ })
83
+ /** Its own `disabled` or its MForm's */
84
+ const isDisabled = validation.disabled
85
+ const { hasError, message: errorMessage, messageId } = validation
52
86
  const t = useLocale()
53
87
 
54
88
  const open = ref(false)
@@ -126,23 +160,38 @@ watch(open, async (v) => {
126
160
  onUnmounted(() => {
127
161
  document.removeEventListener('mousedown', onOut)
128
162
  })
163
+
164
+ defineExpose({
165
+ /** Checks its rules and shows the result; resolves to whether they pass */
166
+ validate: validation.validate,
167
+ /** Hides its validation error (until the next check) */
168
+ resetValidation: validation.resetValidation,
169
+ })
170
+
171
+ // Last: it watches `open`, declared above
172
+ const onFocusOut = useFocusLeave(() => rootEl.value, () => {
173
+ validation.onBlur()
174
+ emit('blur')
175
+ }, () => open.value)
129
176
  </script>
130
177
 
131
178
  <template>
132
- <div class="flex flex-col gap-1">
179
+ <div ref="rootEl" class="flex flex-col gap-1" @focusout="onFocusOut">
133
180
  <div ref="triggerEl" class="relative mt-2" :style="{ '--field-bg': resolvedFieldBg }">
134
181
  <button
135
182
  ref="buttonEl"
183
+ :aria-invalid="hasError || undefined"
184
+ :aria-describedby="errorMessage || hint ? messageId : undefined"
136
185
  type="button"
137
186
  aria-haspopup="dialog"
138
187
  :aria-expanded="open"
139
- :disabled="disabled"
188
+ :disabled="isDisabled"
140
189
  class="flex h-14 w-full items-center gap-3 rounded-xs border bg-transparent px-4 text-left text-body-large outline-none transition-[border-color,border-width] duration-150"
141
190
  :class="[
142
- disabled ? 'cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
143
- !disabled && (open
144
- ? error ? 'border-2 border-error' : 'border-2 border-primary'
145
- : error
191
+ isDisabled ? 'cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
192
+ !isDisabled && (open
193
+ ? hasError ? 'border-2 border-error' : 'border-2 border-primary'
194
+ : hasError
146
195
  ? 'border-error focus-visible:border-2'
147
196
  : 'border-outline hover:border-on-surface focus-visible:border-2 focus-visible:border-primary'),
148
197
  ]"
@@ -150,27 +199,27 @@ onUnmounted(() => {
150
199
  >
151
200
  <span
152
201
  class="m3-cp-swatch h-6 w-6 shrink-0 rounded-full"
153
- :class="disabled ? 'opacity-38' : ''"
202
+ :class="isDisabled ? 'opacity-38' : ''"
154
203
  :style="{ background: `linear-gradient(${modelValue}, ${modelValue}), repeating-conic-gradient(#d4d4d4 0 25%, #fff 0 50%) 0 0 / 8px 8px` }"
155
204
  />
156
- <span v-overflow-title class="min-w-0 truncate flex-1 font-mono" :class="disabled ? 'text-on-surface/38' : 'text-on-surface'">{{ modelValue }}</span>
205
+ <span v-overflow-title class="min-w-0 truncate flex-1 font-mono" :class="isDisabled ? 'text-on-surface/38' : 'text-on-surface'">{{ modelValue }}</span>
157
206
  <MIcon
158
207
  name="palette"
159
208
  :size="24"
160
209
  class="shrink-0 transition-colors"
161
- :class="disabled ? 'text-on-surface/38' : open ? (error ? 'text-error' : 'text-primary') : 'text-on-surface-variant'"
210
+ :class="isDisabled ? 'text-on-surface/38' : open ? (hasError ? 'text-error' : 'text-primary') : 'text-on-surface-variant'"
162
211
  />
163
212
  </button>
164
213
  <label
165
214
  v-if="label"
166
- :class="[OUTLINED_FLOATED_LABEL, 'transition-colors', disabled ? 'text-on-surface/38' : open ? (error ? 'text-error' : 'text-primary') : error ? 'text-error' : 'text-on-surface-variant']"
215
+ :class="[OUTLINED_FLOATED_LABEL, 'transition-colors', isDisabled ? 'text-on-surface/38' : open ? (hasError ? 'text-error' : 'text-primary') : hasError ? 'text-error' : 'text-on-surface-variant']"
167
216
  >
168
- {{ label }}
217
+ {{ label }}<span v-if="required" class="text-error">&nbsp;*</span>
169
218
  </label>
170
219
  </div>
171
220
 
172
- <p v-if="error && errorLabel" class="px-4 text-body-small text-error">{{ errorLabel }}</p>
173
- <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
221
+ <p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
222
+ <p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
174
223
 
175
224
  <Teleport to="body">
176
225
  <Transition name="m3-cp-drop">
@@ -7,6 +7,7 @@
7
7
  * @event update:modelValue A country was picked (its code), or cleared (null)
8
8
  */
9
9
  import { computed } from 'vue'
10
+ import type { ValidateOn, ValidationRule } from '../composables/validation'
10
11
  import MAutocomplete from './MAutocomplete.vue'
11
12
  import { useLocale } from '../composables/useLocale'
12
13
  import { flagEmoji } from '../composables/phoneNumber'
@@ -32,10 +33,18 @@ const props = withDefaults(
32
33
  clearable?: boolean
33
34
  /** Helper text below the field */
34
35
  hint?: string
35
- /** Error state */
36
- error?: boolean
36
+ /** Error state. A string is also the message */
37
+ error?: boolean | string
37
38
  /** Error message shown when `error` is true */
38
39
  errorLabel?: string
40
+ /** Validation rules (functions or a Standard Schema); see `MForm` */
41
+ rules?: ValidationRule | ValidationRule[]
42
+ /** When the rules are checked and shown: on leaving the field (default), while typing, or only on submit */
43
+ validateOn?: ValidateOn
44
+ /** 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 */
45
+ validateDebounce?: number
46
+ /** The key of this field in its `MForm` */
47
+ name?: string
39
48
  /** Marks it as required */
40
49
  required?: boolean
41
50
  /** Disables the field */
@@ -44,7 +53,7 @@ const props = withDefaults(
44
53
  { modelValue: null, flags: true, variant: 'filled', mode: 'docked', clearable: false },
45
54
  )
46
55
 
47
- const emit = defineEmits<{ 'update:modelValue': [code: string | null] }>()
56
+ const emit = defineEmits<{ blur: []; 'update:modelValue': [code: string | null] }>()
48
57
 
49
58
  const t = useLocale()
50
59
 
@@ -81,6 +90,11 @@ const options = computed(() => {
81
90
  :hint="hint"
82
91
  :error="error"
83
92
  :error-label="errorLabel"
93
+ :rules="rules"
94
+ :validate-on="validateOn"
95
+ :validate-debounce="validateDebounce"
96
+ :name="name"
97
+ @blur="emit('blur')"
84
98
  :required="required"
85
99
  :disabled="disabled"
86
100
  leading-icon="public"
@@ -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"