@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.
- package/dist/API.md +1224 -28
- package/dist/{MColorPickerModal-NtkihoK6.js → MColorPickerModal-H8nuX0Wu.js} +3 -4
- package/dist/{MColorPickerModal-NtkihoK6.js.map → MColorPickerModal-H8nuX0Wu.js.map} +1 -1
- package/dist/MDialog-B42Mhn8W.js +331 -0
- package/dist/MDialog-B42Mhn8W.js.map +1 -0
- package/dist/{MListItem-D2cNUyzP.js → MListItem-DiOAxaN7.js} +431 -325
- package/dist/MListItem-DiOAxaN7.js.map +1 -0
- package/dist/{MLoadingIndicator-DWUj0BAB.js → MLoadingIndicator-CuTZYFFj.js} +2 -2
- package/dist/{MLoadingIndicator-DWUj0BAB.js.map → MLoadingIndicator-CuTZYFFj.js.map} +1 -1
- package/dist/{MSeekBar-C4cn9RIF.js → MSeekBar-BCDQTx2t.js} +2 -2
- package/dist/{MSeekBar-C4cn9RIF.js.map → MSeekBar-BCDQTx2t.js.map} +1 -1
- package/dist/MSegmentedButton-BXzRsg1S.js +105 -0
- package/dist/MSegmentedButton-BXzRsg1S.js.map +1 -0
- package/dist/MSignaturePad-BTqGaPXA.js +313 -0
- package/dist/MSignaturePad-BTqGaPXA.js.map +1 -0
- package/dist/MSlider-D_O8J9YH.js +739 -0
- package/dist/MSlider-D_O8J9YH.js.map +1 -0
- package/dist/{MSpinner-DZkXZJpc.js → MSpinner-UCdtSOHP.js} +2 -2
- package/dist/{MSpinner-DZkXZJpc.js.map → MSpinner-UCdtSOHP.js.map} +1 -1
- package/dist/{MTextField-CNH-byir.js → MTextField-CYk9yFvi.js} +162 -115
- package/dist/MTextField-CYk9yFvi.js.map +1 -0
- package/dist/_useFieldValidation-B0B44M_t.js +191 -0
- package/dist/_useFieldValidation-B0B44M_t.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/code-editor.js +184 -103
- package/dist/code-editor.js.map +1 -1
- package/dist/components/MAddressField.vue.d.ts +18 -3
- package/dist/components/MAppBarWithSearch.vue.d.ts +1 -1
- package/dist/components/MAudioRecorder.vue.d.ts +2 -2
- package/dist/components/MAutocomplete.vue.d.ts +22 -3
- package/dist/components/MCheckbox.vue.d.ts +23 -1
- package/dist/components/MCodeEditor.vue.d.ts +23 -1
- package/dist/components/MColorPicker.vue.d.ts +24 -3
- package/dist/components/MConfirmDialog.vue.d.ts +2 -2
- package/dist/components/MCountrySelect.vue.d.ts +13 -2
- package/dist/components/MCreditCardInput.vue.d.ts +33 -0
- package/dist/components/MCronField.vue.d.ts +23 -1
- package/dist/components/MDataTable.vue.d.ts +1 -1
- package/dist/components/MDatePicker.vue.d.ts +24 -3
- package/dist/components/MDateRangePicker.vue.d.ts +24 -3
- package/dist/components/MFileUpload.vue.d.ts +23 -2
- package/dist/components/MForm.vue.d.ts +61 -0
- package/dist/components/MFormBuilder.vue.d.ts +22 -0
- package/dist/components/MFormRenderer.vue.d.ts +151 -0
- package/dist/components/MJsonEditor.vue.d.ts +23 -1
- package/dist/components/MMapRoute.vue.d.ts +176 -0
- package/dist/components/MMaskField.vue.d.ts +20 -2
- package/dist/components/MMentionField.vue.d.ts +13 -2
- package/dist/components/MMultiAutocomplete.vue.d.ts +22 -3
- package/dist/components/MMultiSelect.vue.d.ts +22 -3
- package/dist/components/MNavigationRail.vue.d.ts +1 -1
- package/dist/components/MNotificationCenter.vue.d.ts +2 -2
- package/dist/components/MNumberField.vue.d.ts +20 -2
- package/dist/components/MPdfSigner.vue.d.ts +499 -0
- package/dist/components/MPdfViewer.vue.d.ts +12 -0
- package/dist/components/MPhoneInput.vue.d.ts +19 -4
- package/dist/components/MPinInput.vue.d.ts +24 -3
- package/dist/components/MPosKeypad.vue.d.ts +3 -3
- package/dist/components/MRadioGroup.vue.d.ts +23 -1
- package/dist/components/MRating.vue.d.ts +23 -1
- package/dist/components/MRichTextEditor.vue.d.ts +23 -1
- package/dist/components/MSearchBar.vue.d.ts +1 -1
- package/dist/components/MSegmentedButton.vue.d.ts +23 -2
- package/dist/components/MSelect.vue.d.ts +22 -3
- package/dist/components/MSignaturePad.vue.d.ts +19 -0
- package/dist/components/MSlider.vue.d.ts +23 -1
- package/dist/components/MSpreadsheet.vue.d.ts +137 -0
- package/dist/components/MStack.vue.d.ts +1 -1
- package/dist/components/MSwitch.vue.d.ts +23 -1
- package/dist/components/MTable.vue.d.ts +1 -1
- package/dist/components/MTagInput.vue.d.ts +20 -2
- package/dist/components/MText.vue.d.ts +1 -1
- package/dist/components/MTextField.vue.d.ts +26 -3
- package/dist/components/MTimePicker.vue.d.ts +24 -3
- package/dist/components/MTimezoneSelect.vue.d.ts +13 -2
- package/dist/components/MTour.vue.d.ts +2 -2
- package/dist/components/MTrackingTimeline.vue.d.ts +75 -0
- package/dist/components/MWindow.vue.d.ts +2 -2
- package/dist/composables/_routeGeometry.d.ts +26 -0
- package/dist/composables/_useFieldValidation.d.ts +91 -0
- package/dist/composables/mapRouting.d.ts +35 -0
- package/dist/composables/pdfSign.d.ts +52 -0
- package/dist/composables/spreadsheetFormula.d.ts +55 -0
- package/dist/composables/useLocale.d.ts +177 -0
- package/dist/composables/validation.d.ts +82 -0
- package/dist/draw.js +2 -2
- package/dist/flow.js +1 -1
- package/dist/gantt.js +2 -2
- package/dist/index.d.ts +12 -0
- package/dist/locales/index.js +693 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +15507 -12417
- package/dist/m3ui.js.map +1 -1
- package/dist/map.d.ts +4 -0
- package/dist/map.js +1207 -745
- package/dist/map.js.map +1 -1
- package/dist/media.js +4 -4
- package/dist/pdf.d.ts +3 -0
- package/dist/pdf.js +1228 -687
- package/dist/pdf.js.map +1 -1
- package/dist/rich-text-editor.js +188 -146
- package/dist/rich-text-editor.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/{useLocale-DaVSTdm0.js → useLocale-DL96OFgr.js} +99 -1
- package/dist/useLocale-DL96OFgr.js.map +1 -0
- package/package.json +5 -1
- package/src/__tests__/MForm.test.ts +325 -0
- package/src/__tests__/MFormRenderer.test.ts +157 -0
- package/src/__tests__/MMapRoute.test.ts +418 -0
- package/src/__tests__/MPdfSigner.test.ts +175 -0
- package/src/__tests__/MPhoneInput.test.ts +5 -2
- package/src/__tests__/MRichTextEditor.test.ts +2 -1
- package/src/__tests__/MSpreadsheet.test.ts +223 -0
- package/src/__tests__/MTrackingTimeline.test.ts +66 -0
- package/src/__tests__/_fakeMaplibre.ts +4 -1
- package/src/__tests__/_osrmRoutes.ts +10 -0
- package/src/__tests__/attrsFallthrough.test.ts +11 -0
- package/src/__tests__/fieldsValidation.test.ts +229 -0
- package/src/__tests__/spreadsheetFormula.test.ts +77 -0
- package/src/__tests__/ssr.test.ts +4 -0
- package/src/__tests__/validation.test.ts +77 -0
- package/src/components/MAddressField.vue +45 -8
- package/src/components/MAutocomplete.vue +69 -23
- package/src/components/MCheckbox.vue +80 -17
- package/src/components/MCodeEditor.vue +59 -2
- package/src/components/MColorPicker.vue +65 -16
- package/src/components/MCountrySelect.vue +17 -3
- package/src/components/MCreditCardInput.vue +76 -7
- package/src/components/MCronField.vue +60 -10
- package/src/components/MDatePicker.vue +65 -16
- package/src/components/MDateRangePicker.vue +66 -16
- package/src/components/MFileUpload.vue +71 -8
- package/src/components/MForm.vue +208 -0
- package/src/components/MFormBuilder.vue +417 -0
- package/src/components/MFormRenderer.vue +324 -0
- package/src/components/MJsonEditor.vue +57 -3
- package/src/components/MMapRoute.vue +523 -0
- package/src/components/MMaskField.vue +55 -13
- package/src/components/MMentionField.vue +17 -2
- package/src/components/MMultiAutocomplete.vue +70 -23
- package/src/components/MMultiSelect.vue +70 -23
- package/src/components/MNumberField.vue +58 -16
- package/src/components/MPdfSigner.vue +433 -0
- package/src/components/MPdfViewer.vue +12 -0
- package/src/components/MPhoneInput.vue +60 -18
- package/src/components/MPinInput.vue +58 -10
- package/src/components/MRadioGroup.vue +65 -4
- package/src/components/MRating.vue +62 -6
- package/src/components/MRichTextEditor.vue +64 -4
- package/src/components/MSegmentedButton.vue +59 -2
- package/src/components/MSelect.vue +72 -26
- package/src/components/MSignaturePad.vue +71 -6
- package/src/components/MSlider.vue +61 -10
- package/src/components/MSpreadsheet.vue +992 -0
- package/src/components/MSwitch.vue +63 -8
- package/src/components/MTagInput.vue +67 -25
- package/src/components/MTextField.vue +68 -14
- package/src/components/MTimePicker.vue +65 -16
- package/src/components/MTimezoneSelect.vue +17 -3
- package/src/components/MTrackingTimeline.vue +257 -0
- package/src/composables/_routeGeometry.ts +140 -0
- package/src/composables/_useFieldValidation.ts +287 -0
- package/src/composables/mapRouting.ts +55 -0
- package/src/composables/pdfSign.ts +141 -0
- package/src/composables/spreadsheetFormula.ts +459 -0
- package/src/composables/useLocale.ts +277 -0
- package/src/composables/validation.ts +178 -0
- package/src/index.ts +12 -0
- package/src/locales/de.ts +98 -0
- package/src/locales/es.ts +105 -7
- package/src/locales/fr.ts +98 -0
- package/src/locales/ja.ts +98 -0
- package/src/locales/ko.ts +98 -0
- package/src/locales/pt.ts +98 -0
- package/src/locales/zh.ts +98 -0
- package/src/map.ts +4 -0
- package/src/pdf.ts +3 -0
- package/dist/MButton-OQ4wIERQ.js +0 -171
- package/dist/MButton-OQ4wIERQ.js.map +0 -1
- package/dist/MDialog-DYMnaBlt.js +0 -167
- package/dist/MDialog-DYMnaBlt.js.map +0 -1
- package/dist/MListItem-D2cNUyzP.js.map +0 -1
- package/dist/MProgressBar-BWly1UKN.js +0 -179
- package/dist/MProgressBar-BWly1UKN.js.map +0 -1
- package/dist/MSegmentedButton-BEB4gXLA.js +0 -64
- package/dist/MSegmentedButton-BEB4gXLA.js.map +0 -1
- package/dist/MSlider-BIkA6DiE.js +0 -690
- package/dist/MSlider-BIkA6DiE.js.map +0 -1
- package/dist/MTextField-CNH-byir.js.map +0 -1
- package/dist/useLocale-DaVSTdm0.js.map +0 -1
|
@@ -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 (
|
|
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
|
-
|
|
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 =
|
|
224
|
+
const border = isDisabled.value
|
|
193
225
|
? 'border-on-surface/12'
|
|
194
226
|
: highlighted.value
|
|
195
|
-
? (
|
|
196
|
-
: (
|
|
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 =
|
|
232
|
+
const border = isDisabled.value
|
|
201
233
|
? 'border-on-surface/38'
|
|
202
234
|
: highlighted.value
|
|
203
|
-
? (
|
|
204
|
-
: (
|
|
205
|
-
const bg =
|
|
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:
|
|
251
|
+
color: isDisabled.value
|
|
220
252
|
? 'text-on-surface/38'
|
|
221
253
|
: highlighted.value
|
|
222
|
-
? (
|
|
223
|
-
: (
|
|
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
|
-
|
|
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="
|
|
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="
|
|
260
|
-
:aria-disabled="
|
|
261
|
-
:class="[triggerClasses,
|
|
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,
|
|
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 && !
|
|
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
|
-
|
|
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="
|
|
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 (
|
|
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
|
-
|
|
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({
|
|
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,
|
|
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(() =>
|
|
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(() =>
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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 }}
|
|
605
|
+
<span v-if="label" class="text-label-large" :class="hasError ? 'text-error' : 'text-on-surface'">{{ label }}<span v-if="required" class="text-error"> *</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="
|
|
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="
|
|
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>
|