@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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@m3ui-vue/m3ui-vue",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Material 3 component library for Vue 3 + Tailwind CSS v4",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -217,6 +217,9 @@
|
|
|
217
217
|
},
|
|
218
218
|
"pdfjs-dist": {
|
|
219
219
|
"optional": true
|
|
220
|
+
},
|
|
221
|
+
"pdf-lib": {
|
|
222
|
+
"optional": true
|
|
220
223
|
}
|
|
221
224
|
},
|
|
222
225
|
"devDependencies": {
|
|
@@ -261,6 +264,7 @@
|
|
|
261
264
|
"jsbarcode": "^3.12.3",
|
|
262
265
|
"maplibre-gl": "^6.19.0",
|
|
263
266
|
"markdown-it": "^15.0.3",
|
|
267
|
+
"pdf-lib": "^1.17.1",
|
|
264
268
|
"pdfjs-dist": "^6.5.438",
|
|
265
269
|
"qrcode-generator": "^2.0.4",
|
|
266
270
|
"typescript": "~6.0.3",
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { mount, flushPromises } from '@vue/test-utils'
|
|
3
|
+
import { defineComponent, h, ref } from 'vue'
|
|
4
|
+
import MForm from '../components/MForm.vue'
|
|
5
|
+
import MTextField from '../components/MTextField.vue'
|
|
6
|
+
import { email, minLength, required, sameAs, type StandardSchemaV1 } from '../composables/validation'
|
|
7
|
+
import { defaultLocale } from '../composables/useLocale'
|
|
8
|
+
|
|
9
|
+
/** A minimal object Standard Schema: `checks` maps a path to a test returning a message or null */
|
|
10
|
+
function objectSchema(checks: Record<string, (v: any) => string | null>, transform?: (v: any) => any): StandardSchemaV1 {
|
|
11
|
+
return {
|
|
12
|
+
'~standard': {
|
|
13
|
+
version: 1,
|
|
14
|
+
vendor: 'test',
|
|
15
|
+
validate(value: any) {
|
|
16
|
+
const issues = Object.entries(checks).flatMap(([path, test]) => {
|
|
17
|
+
const v = path.split('.').reduce((o, k) => o?.[k], value)
|
|
18
|
+
const message = test(v)
|
|
19
|
+
return message ? [{ message, path: path.split('.') }] : []
|
|
20
|
+
})
|
|
21
|
+
return issues.length ? { issues } : { value: transform ? transform(value) : value }
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const errorText = (w: ReturnType<typeof mount>) => w.findAll('[role="alert"]').map((p) => p.text())
|
|
28
|
+
|
|
29
|
+
describe('MTextField validation', () => {
|
|
30
|
+
it('emits blur to the consumer (vee-validate/Vuelidate touch)', async () => {
|
|
31
|
+
const onBlur = vi.fn()
|
|
32
|
+
const w = mount(MTextField, { props: { modelValue: '', label: 'Name', onBlur } })
|
|
33
|
+
await w.find('input').trigger('blur')
|
|
34
|
+
expect(onBlur).toHaveBeenCalledTimes(1)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('a string error is also the message', () => {
|
|
38
|
+
const w = mount(MTextField, { props: { modelValue: '', label: 'Email', error: 'Taken' } })
|
|
39
|
+
expect(errorText(w)).toEqual(['Taken'])
|
|
40
|
+
expect(w.find('input').attributes('aria-invalid')).toBe('true')
|
|
41
|
+
expect(w.find('input').attributes('aria-describedby')).toBe(w.find('[role="alert"]').attributes('id'))
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('error + errorLabel keeps working as before', () => {
|
|
45
|
+
const w = mount(MTextField, { props: { modelValue: '', label: 'X', error: true, errorLabel: 'Bad' } })
|
|
46
|
+
expect(errorText(w)).toEqual(['Bad'])
|
|
47
|
+
const off = mount(MTextField, { props: { modelValue: '', label: 'X', error: false, errorLabel: 'Bad', hint: 'Hint' } })
|
|
48
|
+
expect(errorText(off)).toEqual([])
|
|
49
|
+
expect(off.text()).toContain('Hint')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('required alone (no MForm, no rules) does not validate', async () => {
|
|
53
|
+
const w = mount(MTextField, { props: { modelValue: '', label: 'X', required: true } })
|
|
54
|
+
await w.find('input').trigger('blur')
|
|
55
|
+
await flushPromises()
|
|
56
|
+
expect(errorText(w)).toEqual([])
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('rules show on blur, then live while typing', async () => {
|
|
60
|
+
const w = mount(MTextField, { props: { modelValue: 'a', label: 'X', rules: [minLength(3)], 'onUpdate:modelValue': (v: string) => w.setProps({ modelValue: v }) } })
|
|
61
|
+
await flushPromises()
|
|
62
|
+
expect(errorText(w)).toEqual([]) // not touched yet
|
|
63
|
+
await w.find('input').trigger('blur')
|
|
64
|
+
await flushPromises()
|
|
65
|
+
expect(errorText(w)).toEqual(['Enter at least 3 characters'])
|
|
66
|
+
await w.find('input').setValue('abc')
|
|
67
|
+
await flushPromises()
|
|
68
|
+
expect(errorText(w)).toEqual([])
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("validateOn: 'input' shows while typing, before any blur", async () => {
|
|
72
|
+
const w = mount(MTextField, { props: { modelValue: '', label: 'X', validateOn: 'input', rules: [email()], 'onUpdate:modelValue': (v: string) => w.setProps({ modelValue: v }) } })
|
|
73
|
+
await w.find('input').setValue('nope')
|
|
74
|
+
await flushPromises()
|
|
75
|
+
expect(errorText(w)).toEqual([defaultLocale.validationEmail])
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('async rules: only the latest run wins', async () => {
|
|
79
|
+
let resolveFirst!: (v: string) => void
|
|
80
|
+
const rule = vi.fn((v: string) => (v === 'slow' ? new Promise<string>((r) => (resolveFirst = r)) : true))
|
|
81
|
+
const w = mount(MTextField, { props: { modelValue: 'slow', label: 'X', rules: [rule] } })
|
|
82
|
+
const field = w.vm as any
|
|
83
|
+
const first = field.validate()
|
|
84
|
+
await w.setProps({ modelValue: 'fast' })
|
|
85
|
+
await flushPromises()
|
|
86
|
+
resolveFirst('Stale error')
|
|
87
|
+
await first
|
|
88
|
+
await flushPromises()
|
|
89
|
+
expect(errorText(w)).toEqual([])
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
function formWith(fields: (model: any) => any, formProps: Record<string, unknown> = {}) {
|
|
94
|
+
const onSubmit = vi.fn()
|
|
95
|
+
const onInvalid = vi.fn()
|
|
96
|
+
const model = ref<Record<string, string>>({})
|
|
97
|
+
const C = defineComponent({
|
|
98
|
+
setup: (_, { expose }) => {
|
|
99
|
+
const form = ref()
|
|
100
|
+
expose({ form, model })
|
|
101
|
+
return () => h(MForm, { ref: form, onSubmit, onInvalid, ...formProps }, () => fields(model))
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
const w = mount(C, { attachTo: document.body })
|
|
105
|
+
return { w, onSubmit, onInvalid, model }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const field = (model: any, name: string, extra: Record<string, unknown> = {}) =>
|
|
109
|
+
h(MTextField, {
|
|
110
|
+
name,
|
|
111
|
+
label: name,
|
|
112
|
+
modelValue: model.value[name] ?? '',
|
|
113
|
+
'onUpdate:modelValue': (v: string) => (model.value = { ...model.value, [name]: v }),
|
|
114
|
+
...extra,
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe('MForm', () => {
|
|
118
|
+
it('blocks submit, shows every error and focuses the first invalid field', async () => {
|
|
119
|
+
const { w, onSubmit, onInvalid } = formWith((m) => [
|
|
120
|
+
field(m, 'name', { required: true }),
|
|
121
|
+
field(m, 'email', { rules: [required(), email()] }),
|
|
122
|
+
])
|
|
123
|
+
await flushPromises()
|
|
124
|
+
expect(errorText(w)).toEqual([]) // nothing shown before submit
|
|
125
|
+
await w.find('form').trigger('submit')
|
|
126
|
+
await flushPromises()
|
|
127
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
128
|
+
expect(onInvalid).toHaveBeenCalledWith({ name: defaultLocale.validationRequired, email: defaultLocale.validationRequired })
|
|
129
|
+
expect(errorText(w)).toHaveLength(2)
|
|
130
|
+
expect(document.activeElement).toBe(w.findAll('input')[0].element)
|
|
131
|
+
w.unmount()
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('submits the values by name (dotted names nest) once valid', async () => {
|
|
135
|
+
const { w, onSubmit } = formWith((m) => [field(m, 'name', { required: true }), field(m, 'address.city')])
|
|
136
|
+
const inputs = w.findAll('input')
|
|
137
|
+
await inputs[0].setValue('Ana')
|
|
138
|
+
await inputs[1].setValue('Rosario')
|
|
139
|
+
await w.find('form').trigger('submit')
|
|
140
|
+
await flushPromises()
|
|
141
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
142
|
+
expect(onSubmit.mock.calls[0][0]).toEqual({ name: 'Ana', address: { city: 'Rosario' } })
|
|
143
|
+
w.unmount()
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('sameAs re-checks the confirmation when the other field changes', async () => {
|
|
147
|
+
const { w } = formWith((m) => [field(m, 'password'), field(m, 'confirm', { rules: [sameAs('password')] })])
|
|
148
|
+
const [pw, confirm] = w.findAll('input')
|
|
149
|
+
await pw.setValue('secret')
|
|
150
|
+
await confirm.setValue('secre')
|
|
151
|
+
await confirm.trigger('blur')
|
|
152
|
+
await flushPromises()
|
|
153
|
+
expect(errorText(w)).toEqual([defaultLocale.validationSameAs])
|
|
154
|
+
await pw.setValue('secre')
|
|
155
|
+
await flushPromises()
|
|
156
|
+
expect(errorText(w)).toEqual([])
|
|
157
|
+
w.unmount()
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('external errors by name show over the rules and do not block submit', async () => {
|
|
161
|
+
const { w, onSubmit } = formWith((m) => [field(m, 'email')], { errors: { email: ['Already registered'] } })
|
|
162
|
+
await flushPromises()
|
|
163
|
+
expect(errorText(w)).toEqual(['Already registered'])
|
|
164
|
+
await w.find('form').trigger('submit')
|
|
165
|
+
await flushPromises()
|
|
166
|
+
expect(onSubmit).toHaveBeenCalled()
|
|
167
|
+
w.unmount()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('a form schema validates the fields and submits its output', async () => {
|
|
171
|
+
const schema = objectSchema({ age: (v) => (Number(v) >= 18 ? null : 'Adults only') }, (v) => ({ ...v, age: Number(v.age) }))
|
|
172
|
+
const { w, onSubmit } = formWith((m) => [field(m, 'age')], { schema })
|
|
173
|
+
const input = w.find('input')
|
|
174
|
+
await input.setValue('12')
|
|
175
|
+
await w.find('form').trigger('submit')
|
|
176
|
+
await flushPromises()
|
|
177
|
+
expect(errorText(w)).toEqual(['Adults only'])
|
|
178
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
179
|
+
await input.setValue('20')
|
|
180
|
+
await flushPromises()
|
|
181
|
+
expect(errorText(w)).toEqual([]) // live after the first submit
|
|
182
|
+
await w.find('form').trigger('submit')
|
|
183
|
+
await flushPromises()
|
|
184
|
+
expect(onSubmit.mock.calls[0][0]).toEqual({ age: 20 })
|
|
185
|
+
w.unmount()
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
it("validateOn: 'submit' keeps quiet on blur", async () => {
|
|
189
|
+
const { w } = formWith((m) => [field(m, 'name', { required: true })], { validateOn: 'submit' })
|
|
190
|
+
await w.find('input').trigger('blur')
|
|
191
|
+
await flushPromises()
|
|
192
|
+
expect(errorText(w)).toEqual([])
|
|
193
|
+
w.unmount()
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('reset() restores the first values and hides the errors; valid tracks silently', async () => {
|
|
197
|
+
const { w, model } = formWith((m) => [field(m, 'name', { required: true })])
|
|
198
|
+
const form = (w.vm as any).form
|
|
199
|
+
await flushPromises()
|
|
200
|
+
expect(form.valid).toBe(false)
|
|
201
|
+
await w.find('input').setValue('Ana')
|
|
202
|
+
await flushPromises()
|
|
203
|
+
expect(form.valid).toBe(true)
|
|
204
|
+
await w.find('input').setValue('')
|
|
205
|
+
await w.find('input').trigger('blur')
|
|
206
|
+
await flushPromises()
|
|
207
|
+
expect(errorText(w)).toHaveLength(1)
|
|
208
|
+
await form.reset()
|
|
209
|
+
await flushPromises()
|
|
210
|
+
expect(model.value.name).toBe('')
|
|
211
|
+
expect(errorText(w)).toEqual([])
|
|
212
|
+
w.unmount()
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('disabled disables the controls and skips validation', async () => {
|
|
216
|
+
const { w, onSubmit, onInvalid } = formWith((m) => [field(m, 'name', { required: true })], { disabled: true })
|
|
217
|
+
expect(w.find('fieldset').attributes('disabled')).toBeDefined()
|
|
218
|
+
await w.find('form').trigger('submit')
|
|
219
|
+
await flushPromises()
|
|
220
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
221
|
+
expect(onInvalid).not.toHaveBeenCalled()
|
|
222
|
+
w.unmount()
|
|
223
|
+
})
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
describe('MForm extras (1.7.0)', () => {
|
|
227
|
+
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms))
|
|
228
|
+
|
|
229
|
+
it('validateDebounce: one check after typing stops, pending meanwhile', async () => {
|
|
230
|
+
const rule = vi.fn(async () => true)
|
|
231
|
+
const { w } = formWith((m) => [field(m, 'user', { rules: [rule] })], { validateDebounce: 40 })
|
|
232
|
+
const form = (w.vm as any).form
|
|
233
|
+
await flushPromises()
|
|
234
|
+
rule.mockClear()
|
|
235
|
+
const input = w.find('input')
|
|
236
|
+
await input.setValue('a')
|
|
237
|
+
await input.setValue('ab')
|
|
238
|
+
await input.setValue('abc')
|
|
239
|
+
expect(form.pending).toBe(true)
|
|
240
|
+
expect(rule).not.toHaveBeenCalled()
|
|
241
|
+
await wait(60)
|
|
242
|
+
await flushPromises()
|
|
243
|
+
expect(rule).toHaveBeenCalledTimes(1)
|
|
244
|
+
expect(rule.mock.calls[0][0]).toBe('abc')
|
|
245
|
+
expect(form.pending).toBe(false)
|
|
246
|
+
w.unmount()
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
it('validate() skips the wait', async () => {
|
|
250
|
+
const { w } = formWith((m) => [field(m, 'user', { rules: [() => 'Bad'], validateDebounce: 10_000 })])
|
|
251
|
+
await w.find('input').setValue('x')
|
|
252
|
+
expect(await (w.vm as any).form.validate()).toBe(false)
|
|
253
|
+
await flushPromises()
|
|
254
|
+
expect(errorText(w)).toEqual(['Bad'])
|
|
255
|
+
w.unmount()
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
it("an inline rules array doesn't re-check every field on each render of the parent", async () => {
|
|
259
|
+
const rule = vi.fn(() => true)
|
|
260
|
+
const { w } = formWith((m) => [field(m, 'a'), field(m, 'b', { rules: [rule] })])
|
|
261
|
+
await flushPromises()
|
|
262
|
+
rule.mockClear()
|
|
263
|
+
await w.findAll('input')[0].setValue('typing in a') // the parent re-renders: b gets a new rules array
|
|
264
|
+
await flushPromises()
|
|
265
|
+
expect(rule).not.toHaveBeenCalled()
|
|
266
|
+
w.unmount()
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
it('an outside error hides once its field changes, and a new errors object shows it again', async () => {
|
|
270
|
+
const errors = ref<Record<string, string>>({ email: 'Taken' })
|
|
271
|
+
const C = defineComponent({
|
|
272
|
+
setup() {
|
|
273
|
+
const v = ref('')
|
|
274
|
+
return () => h(MForm, { errors: errors.value }, () => h(MTextField, { name: 'email', label: 'Email', modelValue: v.value, 'onUpdate:modelValue': (x: string) => (v.value = x) }))
|
|
275
|
+
},
|
|
276
|
+
})
|
|
277
|
+
const w = mount(C)
|
|
278
|
+
await flushPromises()
|
|
279
|
+
expect(errorText(w)).toEqual(['Taken'])
|
|
280
|
+
await w.find('input').setValue('other@m3ui.dev')
|
|
281
|
+
await flushPromises()
|
|
282
|
+
expect(errorText(w)).toEqual([])
|
|
283
|
+
errors.value = { email: 'Taken' } // the next response
|
|
284
|
+
await flushPromises()
|
|
285
|
+
expect(errorText(w)).toEqual(['Taken'])
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
it('keepErrors keeps an outside error while its field changes', async () => {
|
|
289
|
+
const { w } = formWith((m) => [field(m, 'email')], { errors: { email: 'Taken' }, keepErrors: true })
|
|
290
|
+
await w.find('input').setValue('x')
|
|
291
|
+
await flushPromises()
|
|
292
|
+
expect(errorText(w)).toEqual(['Taken'])
|
|
293
|
+
w.unmount()
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
it('validate(names) checks only those fields (dotted ones included), and can focus the first error', async () => {
|
|
297
|
+
const { w } = formWith((m) => [
|
|
298
|
+
field(m, 'name', { required: true }),
|
|
299
|
+
field(m, 'address.city', { required: true }),
|
|
300
|
+
field(m, 'card', { required: true }),
|
|
301
|
+
])
|
|
302
|
+
const form = (w.vm as any).form
|
|
303
|
+
await flushPromises()
|
|
304
|
+
expect(await form.validate(['name', 'address'], { focus: true })).toBe(false)
|
|
305
|
+
await flushPromises()
|
|
306
|
+
expect(errorText(w)).toHaveLength(2) // name and address.city, not card
|
|
307
|
+
expect(document.activeElement).toBe(w.findAll('input')[0].element)
|
|
308
|
+
await w.findAll('input')[0].setValue('Ana')
|
|
309
|
+
await w.findAll('input')[1].setValue('Rosario')
|
|
310
|
+
expect(await form.validate(['name', 'address'])).toBe(true)
|
|
311
|
+
form.resetValidation(['card'])
|
|
312
|
+
expect(await form.validate()).toBe(false) // the whole form still checks card
|
|
313
|
+
w.unmount()
|
|
314
|
+
})
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
describe('required prop + required() in rules', () => {
|
|
318
|
+
it("shows the rules' own message, not the generic one", async () => {
|
|
319
|
+
const { w } = formWith((m) => [field(m, 'name', { required: true, rules: [required('Enter your name')] })])
|
|
320
|
+
await w.find('form').trigger('submit')
|
|
321
|
+
await flushPromises()
|
|
322
|
+
expect(errorText(w)).toEqual(['Enter your name'])
|
|
323
|
+
w.unmount()
|
|
324
|
+
})
|
|
325
|
+
})
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
2
|
+
import { flushPromises, mount } from '@vue/test-utils'
|
|
3
|
+
import { h, nextTick, ref } from 'vue'
|
|
4
|
+
import MFormRenderer, { fieldVisible, emptyValue } from '../components/MFormRenderer.vue'
|
|
5
|
+
import MFormBuilder from '../components/MFormBuilder.vue'
|
|
6
|
+
import type { FormSchema } from '../components/MFormRenderer.vue'
|
|
7
|
+
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
document.body.innerHTML = ''
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const schema: FormSchema = {
|
|
13
|
+
title: 'Book a table',
|
|
14
|
+
submitLabel: 'Book',
|
|
15
|
+
fields: [
|
|
16
|
+
{ type: 'text', name: 'name', label: 'Name', required: true, width: 'half' },
|
|
17
|
+
{ type: 'email', name: 'email', label: 'Email', width: 'half' },
|
|
18
|
+
{ type: 'number', name: 'guests', label: 'Guests', min: 1, max: 12, default: 2 },
|
|
19
|
+
{ type: 'heading', label: 'Extras' },
|
|
20
|
+
{ type: 'checkbox', name: 'birthday', label: 'It’s a birthday' },
|
|
21
|
+
{ type: 'text', name: 'cake', label: 'Message on the cake', visibleWhen: { field: 'birthday', equals: true }, maxLength: 5 },
|
|
22
|
+
{ type: 'select', name: 'zone', label: 'Zone', options: ['Inside', { label: 'Terrace', value: 'out' }] },
|
|
23
|
+
{ type: 'divider' },
|
|
24
|
+
],
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function setup(s: FormSchema = schema) {
|
|
28
|
+
const values = ref<Record<string, unknown>>({})
|
|
29
|
+
const submitted: Record<string, unknown>[] = []
|
|
30
|
+
const invalid: unknown[] = []
|
|
31
|
+
const wrapper = mount(() => h(MFormRenderer, {
|
|
32
|
+
schema: s,
|
|
33
|
+
modelValue: values.value,
|
|
34
|
+
'onUpdate:modelValue': (v: Record<string, unknown>) => (values.value = v),
|
|
35
|
+
onSubmit: (v: Record<string, unknown>) => submitted.push(v),
|
|
36
|
+
onInvalid: (e: unknown) => invalid.push(e),
|
|
37
|
+
}), { attachTo: document.body })
|
|
38
|
+
return { wrapper, values, submitted, invalid }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('MFormRenderer', () => {
|
|
42
|
+
it('renders each field with its label, defaults its values, lays out widths', async () => {
|
|
43
|
+
const { wrapper, values } = setup()
|
|
44
|
+
await nextTick()
|
|
45
|
+
expect(wrapper.text()).toContain('Book a table')
|
|
46
|
+
expect(wrapper.text()).toContain('Extras')
|
|
47
|
+
expect(values.value).toMatchObject({ name: '', email: '', guests: 2, birthday: false, zone: null })
|
|
48
|
+
expect(wrapper.find('[data-field="name"]').classes()).toContain('@lg:col-span-6')
|
|
49
|
+
expect(wrapper.find('button[type="submit"]').text()).toContain('Book')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('blocks submit on its rules (required, email, maxLength) and submits the values of the fields on show', async () => {
|
|
53
|
+
const { wrapper, values, submitted, invalid } = setup()
|
|
54
|
+
await wrapper.find('form').trigger('submit')
|
|
55
|
+
await flushPromises()
|
|
56
|
+
expect(submitted).toHaveLength(0)
|
|
57
|
+
expect(Object.keys(invalid[0] as object)).toContain('name')
|
|
58
|
+
|
|
59
|
+
await wrapper.find('[data-field="name"] input').setValue('Ana')
|
|
60
|
+
await wrapper.find('[data-field="email"] input').setValue('not-an-email')
|
|
61
|
+
await wrapper.find('form').trigger('submit')
|
|
62
|
+
await flushPromises()
|
|
63
|
+
expect(submitted).toHaveLength(0)
|
|
64
|
+
expect(Object.keys(invalid.at(-1) as object)).toEqual(['email'])
|
|
65
|
+
|
|
66
|
+
await wrapper.find('[data-field="email"] input').setValue('ana@example.com')
|
|
67
|
+
await wrapper.find('form').trigger('submit')
|
|
68
|
+
await flushPromises()
|
|
69
|
+
expect(submitted[0]).toEqual({ name: 'Ana', email: 'ana@example.com', guests: 2, birthday: false, zone: null })
|
|
70
|
+
expect(values.value.name).toBe('Ana')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('a field shows only while its condition holds, and is checked only then', async () => {
|
|
74
|
+
const { wrapper, submitted } = setup()
|
|
75
|
+
expect(wrapper.find('[data-field="cake"]').exists()).toBe(false)
|
|
76
|
+
await wrapper.find('[data-field="name"] input').setValue('Ana')
|
|
77
|
+
await wrapper.find('[data-field="birthday"] input').setValue(true)
|
|
78
|
+
await nextTick()
|
|
79
|
+
expect(wrapper.find('[data-field="cake"]').exists()).toBe(true)
|
|
80
|
+
await wrapper.find('[data-field="cake"] input').setValue('Happy birthday!')
|
|
81
|
+
await wrapper.find('form').trigger('submit')
|
|
82
|
+
await flushPromises()
|
|
83
|
+
expect(submitted).toHaveLength(0) // maxLength 5
|
|
84
|
+
await wrapper.find('[data-field="cake"] input').setValue('Yay')
|
|
85
|
+
await wrapper.find('form').trigger('submit')
|
|
86
|
+
await flushPromises()
|
|
87
|
+
expect(submitted[0]).toMatchObject({ birthday: true, cake: 'Yay' })
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('field-{name} slots replace a field', () => {
|
|
91
|
+
const wrapper = mount(MFormRenderer, {
|
|
92
|
+
props: { schema: { fields: [{ type: 'text', name: 'code', label: 'Code' }] } },
|
|
93
|
+
slots: { 'field-code': (p: { field: { label: string } }) => h('b', { class: 'mine' }, p.field.label) },
|
|
94
|
+
})
|
|
95
|
+
expect(wrapper.find('.mine').text()).toBe('Code')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('helpers: conditions and empty values', () => {
|
|
99
|
+
expect(fieldVisible({ type: 'text', visibleWhen: { field: 'a', in: [1, 2] } }, { a: 2 })).toBe(true)
|
|
100
|
+
expect(fieldVisible({ type: 'text', visibleWhen: [{ field: 'a', filled: true }, { field: 'b', notEquals: 'x' }] }, { a: 'y', b: 'x' })).toBe(false)
|
|
101
|
+
expect(emptyValue({ type: 'slider', min: 10 })).toBe(10)
|
|
102
|
+
expect(emptyValue({ type: 'switch' })).toBe(false)
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
describe('MFormBuilder', () => {
|
|
107
|
+
function setupBuilder(initial: FormSchema = { fields: [] }) {
|
|
108
|
+
const value = ref<FormSchema>(initial)
|
|
109
|
+
const wrapper = mount(() => h(MFormBuilder, {
|
|
110
|
+
modelValue: value.value,
|
|
111
|
+
'onUpdate:modelValue': (v: FormSchema) => (value.value = v),
|
|
112
|
+
}), { attachTo: document.body })
|
|
113
|
+
return { wrapper, value }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
it('adds fields from the palette with a name from the label, unique', async () => {
|
|
117
|
+
const { wrapper, value } = setupBuilder()
|
|
118
|
+
await wrapper.find('[data-type="email"]').trigger('click')
|
|
119
|
+
await wrapper.find('[data-type="email"]').trigger('click')
|
|
120
|
+
await wrapper.find('[data-type="select"]').trigger('click')
|
|
121
|
+
expect(value.value.fields.map((f) => f.name)).toEqual(['email', 'email2', 'dropdown'])
|
|
122
|
+
expect(value.value.fields[2]!.options).toHaveLength(3)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('the inspector edits the selected field: label (the name follows), required, options', async () => {
|
|
126
|
+
const { wrapper, value } = setupBuilder()
|
|
127
|
+
await wrapper.find('[data-type="text"]').trigger('click')
|
|
128
|
+
const inspector = () => wrapper.findAll('section').at(2)!
|
|
129
|
+
await inspector().findAll('input').at(0)!.setValue('Full name')
|
|
130
|
+
expect(value.value.fields[0]).toMatchObject({ label: 'Full name', name: 'fullName' })
|
|
131
|
+
// the required switch
|
|
132
|
+
await inspector().find('input[type="checkbox"]').setValue(true)
|
|
133
|
+
expect(value.value.fields[0]!.required).toBe(true)
|
|
134
|
+
|
|
135
|
+
await wrapper.find('[data-type="radio"]').trigger('click')
|
|
136
|
+
const options = inspector().find('textarea')
|
|
137
|
+
await options.setValue('Small\nLarge=L')
|
|
138
|
+
expect(value.value.fields[1]!.options).toEqual(['Small', { label: 'Large', value: 'L' }])
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('duplicates and deletes fields', async () => {
|
|
142
|
+
const { wrapper, value } = setupBuilder({ fields: [{ type: 'text', name: 'a', label: 'A' }] })
|
|
143
|
+
await wrapper.find('[data-field-id] button[aria-label="Duplicate"]').trigger('click')
|
|
144
|
+
expect(value.value.fields.map((f) => f.name)).toEqual(['a', 'a2'])
|
|
145
|
+
await wrapper.findAll('[data-field-id] button[aria-label="Delete"]')[0]!.trigger('click')
|
|
146
|
+
expect(value.value.fields.map((f) => f.name)).toEqual(['a2'])
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
it('preview renders the schema; JSON shows it', async () => {
|
|
150
|
+
const { wrapper } = setupBuilder({ fields: [{ type: 'text', name: 'city', label: 'City' }] })
|
|
151
|
+
const tabs = wrapper.findAll('[role="radio"], button').filter((b) => /Preview|JSON/.test(b.text()))
|
|
152
|
+
await tabs.find((b) => b.text().includes('Preview'))!.trigger('click')
|
|
153
|
+
expect(wrapper.find('form [data-field="city"]').exists()).toBe(true)
|
|
154
|
+
await tabs.find((b) => b.text().includes('JSON'))!.trigger('click')
|
|
155
|
+
expect(wrapper.find('pre').text()).toContain('"name": "city"')
|
|
156
|
+
})
|
|
157
|
+
})
|