@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
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ValidationRule } from '../composables/validation'
|
|
3
|
+
|
|
4
|
+
/** The kinds of field a form schema can hold. `heading` and `divider` only lay the form out */
|
|
5
|
+
export type FormFieldType =
|
|
6
|
+
| 'text' | 'email' | 'password' | 'tel' | 'url' | 'textarea' | 'number'
|
|
7
|
+
| 'select' | 'autocomplete' | 'radio' | 'checkbox' | 'switch'
|
|
8
|
+
| 'date' | 'time' | 'slider' | 'rating' | 'phone'
|
|
9
|
+
| 'heading' | 'divider'
|
|
10
|
+
|
|
11
|
+
/** A choice of a select, autocomplete or radio group */
|
|
12
|
+
export interface FormFieldOption {
|
|
13
|
+
label: string
|
|
14
|
+
value: string | number | boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Shows a field only while another field's value matches */
|
|
18
|
+
export interface FormFieldCondition {
|
|
19
|
+
/** The other field's name */
|
|
20
|
+
field: string
|
|
21
|
+
/** Its value is this */
|
|
22
|
+
equals?: unknown
|
|
23
|
+
/** Its value isn't this */
|
|
24
|
+
notEquals?: unknown
|
|
25
|
+
/** Its value is one of these */
|
|
26
|
+
in?: unknown[]
|
|
27
|
+
/** It has a value (true) or is empty (false) */
|
|
28
|
+
filled?: boolean
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** One field of a form schema: plain JSON, so it can be stored, sent and built with MFormBuilder */
|
|
32
|
+
export interface FormFieldSchema {
|
|
33
|
+
type: FormFieldType
|
|
34
|
+
/** The value's key (required for every type but heading and divider) */
|
|
35
|
+
name?: string
|
|
36
|
+
label?: string
|
|
37
|
+
placeholder?: string
|
|
38
|
+
/** Help under the field */
|
|
39
|
+
hint?: string
|
|
40
|
+
/** A heading's text under its title */
|
|
41
|
+
description?: string
|
|
42
|
+
required?: boolean
|
|
43
|
+
disabled?: boolean
|
|
44
|
+
/** Its value before the user changes it */
|
|
45
|
+
default?: unknown
|
|
46
|
+
/** select, autocomplete, radio: the choices (a plain string is its own label and value) */
|
|
47
|
+
options?: (FormFieldOption | string)[]
|
|
48
|
+
/** number, slider: limits. date: earliest/latest as `YYYY-MM-DD` strings */
|
|
49
|
+
min?: number | string
|
|
50
|
+
max?: number | string
|
|
51
|
+
step?: number
|
|
52
|
+
/** text-like fields: length limits */
|
|
53
|
+
minLength?: number
|
|
54
|
+
maxLength?: number
|
|
55
|
+
/** text-like fields: a regular expression the value must match (as a string, without slashes) */
|
|
56
|
+
pattern?: string
|
|
57
|
+
/** The message when `pattern` doesn't match */
|
|
58
|
+
patternMessage?: string
|
|
59
|
+
/** textarea: visible lines */
|
|
60
|
+
rows?: number
|
|
61
|
+
/** How much of the row it takes on a wide form (stacked on a narrow one). Default `full` */
|
|
62
|
+
width?: 'full' | 'half' | 'third' | 'two-thirds'
|
|
63
|
+
/** A Material Symbol at the start of text-like fields */
|
|
64
|
+
icon?: string
|
|
65
|
+
/** Show it only when this (or all of these) holds */
|
|
66
|
+
visibleWhen?: FormFieldCondition | FormFieldCondition[]
|
|
67
|
+
/** More rules, in code (not JSON): validators, zod/valibot schemas, functions */
|
|
68
|
+
rules?: ValidationRule[]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A whole form: its fields in order, and its buttons' text */
|
|
72
|
+
export interface FormSchema {
|
|
73
|
+
title?: string
|
|
74
|
+
description?: string
|
|
75
|
+
fields: FormFieldSchema[]
|
|
76
|
+
/** The submit button's text */
|
|
77
|
+
submitLabel?: string
|
|
78
|
+
/** Show a reset button with this text */
|
|
79
|
+
resetLabel?: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Whether a field takes a value (headings and dividers don't) */
|
|
83
|
+
export const isValueField = (f: FormFieldSchema) => f.type !== 'heading' && f.type !== 'divider'
|
|
84
|
+
|
|
85
|
+
/** A field's options as `{ label, value }` */
|
|
86
|
+
export const normalizeOptions = (options: FormFieldSchema['options']): FormFieldOption[] =>
|
|
87
|
+
(options ?? []).map((o) => (typeof o === 'string' ? { label: o, value: o } : o))
|
|
88
|
+
|
|
89
|
+
/** What a field holds before anything is typed */
|
|
90
|
+
export function emptyValue(f: FormFieldSchema): unknown {
|
|
91
|
+
if (f.default !== undefined) return f.default
|
|
92
|
+
if (f.type === 'checkbox' || f.type === 'switch') return false
|
|
93
|
+
if (f.type === 'slider') return typeof f.min === 'number' ? f.min : 0
|
|
94
|
+
if (f.type === 'rating') return 0
|
|
95
|
+
if (f.type === 'number' || f.type === 'select' || f.type === 'autocomplete' || f.type === 'radio' || f.type === 'date' || f.type === 'time') return null
|
|
96
|
+
return ''
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Whether a field shows, given the form's values */
|
|
100
|
+
export function fieldVisible(f: FormFieldSchema, values: Record<string, unknown>): boolean {
|
|
101
|
+
if (!f.visibleWhen) return true
|
|
102
|
+
const list = Array.isArray(f.visibleWhen) ? f.visibleWhen : [f.visibleWhen]
|
|
103
|
+
return list.every((c) => {
|
|
104
|
+
const v = values[c.field]
|
|
105
|
+
if (c.filled !== undefined) {
|
|
106
|
+
const filled = !(v == null || v === '' || v === false || (Array.isArray(v) && !v.length))
|
|
107
|
+
if (filled !== c.filled) return false
|
|
108
|
+
}
|
|
109
|
+
if ('equals' in c && c.equals !== undefined && v !== c.equals) return false
|
|
110
|
+
if ('notEquals' in c && c.notEquals !== undefined && v === c.notEquals) return false
|
|
111
|
+
if (c.in && !c.in.includes(v)) return false
|
|
112
|
+
return true
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
</script>
|
|
116
|
+
|
|
117
|
+
<script setup lang="ts">
|
|
118
|
+
/**
|
|
119
|
+
* Renders a form from a JSON schema (`FormSchema`): text, number, select, radio, checkbox, switch, date, time,
|
|
120
|
+
* slider, rating and phone fields, headings and dividers, laid out in rows (`width`), with their rules
|
|
121
|
+
* (`required`, lengths, `pattern`, email/url by type) checked by MForm, and fields that show only when another
|
|
122
|
+
* field has some value (`visibleWhen`). Build schemas by hand, from a backend, or with MFormBuilder. A hidden field
|
|
123
|
+
* isn't validated and its value isn't submitted.
|
|
124
|
+
*
|
|
125
|
+
* @slot field-{name} Replaces one field. Scope: `{ field, value, update }` (call `update(value)`)
|
|
126
|
+
* @slot actions Replaces the buttons. Scope: `{ reset }`
|
|
127
|
+
* @event submit The form passed its checks: the values of the fields on show, by name
|
|
128
|
+
* @event invalid Submit was blocked: the messages by field name
|
|
129
|
+
*/
|
|
130
|
+
import { computed, ref, watch } from 'vue'
|
|
131
|
+
import MForm from './MForm.vue'
|
|
132
|
+
import MTextField from './MTextField.vue'
|
|
133
|
+
import MNumberField from './MNumberField.vue'
|
|
134
|
+
import MSelect from './MSelect.vue'
|
|
135
|
+
import MAutocomplete from './MAutocomplete.vue'
|
|
136
|
+
import MRadioGroup from './MRadioGroup.vue'
|
|
137
|
+
import MCheckbox from './MCheckbox.vue'
|
|
138
|
+
import MSwitch from './MSwitch.vue'
|
|
139
|
+
import MDatePicker from './MDatePicker.vue'
|
|
140
|
+
import MTimePicker from './MTimePicker.vue'
|
|
141
|
+
import MSlider from './MSlider.vue'
|
|
142
|
+
import MRating from './MRating.vue'
|
|
143
|
+
import MPhoneInput from './MPhoneInput.vue'
|
|
144
|
+
import MDivider from './MDivider.vue'
|
|
145
|
+
import MButton from './MButton.vue'
|
|
146
|
+
import { email, maxLength, minLength, pattern, url } from '../composables/validation'
|
|
147
|
+
import type { ValidateOn } from '../composables/validation'
|
|
148
|
+
import { useLocale } from '../composables/useLocale'
|
|
149
|
+
|
|
150
|
+
const props = withDefaults(
|
|
151
|
+
defineProps<{
|
|
152
|
+
/** The form to render */
|
|
153
|
+
schema: FormSchema
|
|
154
|
+
/** The values by field name (v-model). Missing ones start from each field's `default` */
|
|
155
|
+
modelValue?: Record<string, unknown>
|
|
156
|
+
/** When fields check their rules (MForm's) */
|
|
157
|
+
validateOn?: ValidateOn
|
|
158
|
+
/** Wait this long (ms) after typing stops before re-checking */
|
|
159
|
+
validateDebounce?: number
|
|
160
|
+
/** Errors from outside (a server's), by field name */
|
|
161
|
+
errors?: Record<string, string | string[] | null | undefined>
|
|
162
|
+
/** Disables every field and the buttons */
|
|
163
|
+
disabled?: boolean
|
|
164
|
+
/** Show the submit (and reset) buttons */
|
|
165
|
+
actions?: boolean
|
|
166
|
+
/** The submit button shows a spinner and can't be pressed */
|
|
167
|
+
loading?: boolean
|
|
168
|
+
}>(),
|
|
169
|
+
{ modelValue: undefined, actions: true },
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
const emit = defineEmits<{
|
|
173
|
+
'update:modelValue': [values: Record<string, unknown>]
|
|
174
|
+
submit: [values: Record<string, unknown>]
|
|
175
|
+
invalid: [errors: Record<string, string>]
|
|
176
|
+
}>()
|
|
177
|
+
|
|
178
|
+
const locale = useLocale()
|
|
179
|
+
const form = ref<InstanceType<typeof MForm>>()
|
|
180
|
+
|
|
181
|
+
/** Its own copy, so it works without v-model too */
|
|
182
|
+
const local = ref<Record<string, unknown>>({})
|
|
183
|
+
function seed() {
|
|
184
|
+
const next: Record<string, unknown> = { ...(props.modelValue ?? local.value) }
|
|
185
|
+
for (const f of props.schema.fields) if (f.name && isValueField(f) && !(f.name in next)) next[f.name] = emptyValue(f)
|
|
186
|
+
local.value = next
|
|
187
|
+
return next
|
|
188
|
+
}
|
|
189
|
+
seed()
|
|
190
|
+
watch(() => props.modelValue, (v) => { if (v && v !== local.value) seed() })
|
|
191
|
+
watch(() => props.schema.fields.map((f) => `${f.name}:${f.type}`).join(), () => {
|
|
192
|
+
const before = JSON.stringify(local.value)
|
|
193
|
+
const next = seed()
|
|
194
|
+
if (JSON.stringify(next) !== before) emit('update:modelValue', next)
|
|
195
|
+
})
|
|
196
|
+
// Fields the schema starts with: the parent's v-model gets their defaults
|
|
197
|
+
if (props.modelValue && Object.keys(local.value).some((k) => !(k in props.modelValue!))) emit('update:modelValue', local.value)
|
|
198
|
+
|
|
199
|
+
function set(name: string, value: unknown) {
|
|
200
|
+
local.value = { ...local.value, [name]: value }
|
|
201
|
+
emit('update:modelValue', local.value)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const visible = computed(() => props.schema.fields.filter((f) => fieldVisible(f, local.value)))
|
|
205
|
+
|
|
206
|
+
const span: Record<NonNullable<FormFieldSchema['width']>, string> = {
|
|
207
|
+
full: 'col-span-12',
|
|
208
|
+
half: 'col-span-12 @lg:col-span-6',
|
|
209
|
+
third: 'col-span-12 @lg:col-span-4',
|
|
210
|
+
'two-thirds': 'col-span-12 @lg:col-span-8',
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function rulesFor(f: FormFieldSchema): ValidationRule[] {
|
|
214
|
+
const out: ValidationRule[] = []
|
|
215
|
+
if (f.type === 'email') out.push(email())
|
|
216
|
+
if (f.type === 'url') out.push(url())
|
|
217
|
+
if (f.minLength != null) out.push(minLength(f.minLength))
|
|
218
|
+
if (f.maxLength != null) out.push(maxLength(f.maxLength))
|
|
219
|
+
if (f.pattern) {
|
|
220
|
+
try { out.push(pattern(new RegExp(f.pattern), f.patternMessage)) } catch { /* a bad expression is ignored */ }
|
|
221
|
+
}
|
|
222
|
+
return [...out, ...(f.rules ?? [])]
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The component and props for each kind of field */
|
|
226
|
+
function control(f: FormFieldSchema): { is: unknown; props: Record<string, unknown> } {
|
|
227
|
+
const common = {
|
|
228
|
+
name: f.name,
|
|
229
|
+
label: f.label,
|
|
230
|
+
required: f.required,
|
|
231
|
+
disabled: f.disabled,
|
|
232
|
+
rules: rulesFor(f),
|
|
233
|
+
hint: f.hint,
|
|
234
|
+
}
|
|
235
|
+
const text = { ...common, placeholder: f.placeholder, leadingIcon: f.icon }
|
|
236
|
+
switch (f.type) {
|
|
237
|
+
case 'textarea': return { is: MTextField, props: { ...text, multiline: true, rows: f.rows ?? 3, autoGrow: true } }
|
|
238
|
+
case 'number': return { is: MNumberField, props: { ...text, min: num(f.min), max: num(f.max), step: f.step } }
|
|
239
|
+
case 'select': return { is: MSelect, props: { ...text, options: normalizeOptions(f.options), clearable: !f.required } }
|
|
240
|
+
case 'autocomplete': return { is: MAutocomplete, props: { ...text, options: normalizeOptions(f.options), clearable: !f.required } }
|
|
241
|
+
case 'radio': return { is: MRadioGroup, props: { ...common, hint: undefined, options: normalizeOptions(f.options) } }
|
|
242
|
+
case 'checkbox': return { is: MCheckbox, props: { ...common, hint: undefined } }
|
|
243
|
+
case 'switch': return { is: MSwitch, props: { ...common, hint: undefined } }
|
|
244
|
+
case 'date': return { is: MDatePicker, props: { ...text, leadingIcon: undefined, min: str(f.min), max: str(f.max) } }
|
|
245
|
+
case 'time': return { is: MTimePicker, props: { ...text, leadingIcon: undefined } }
|
|
246
|
+
case 'slider': return { is: MSlider, props: { ...common, hint: undefined, min: num(f.min) ?? 0, max: num(f.max) ?? 100, step: f.step ?? 1, showValue: true } }
|
|
247
|
+
case 'rating': return { is: MRating, props: { ...common, label: undefined, hint: undefined, max: num(f.max) ?? 5 } }
|
|
248
|
+
case 'phone': return { is: MPhoneInput, props: { ...text, leadingIcon: undefined } }
|
|
249
|
+
default: return { is: MTextField, props: { ...text, type: f.type === 'text' ? undefined : f.type } }
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const num = (v: unknown) => (typeof v === 'number' ? v : typeof v === 'string' && v !== '' && !Number.isNaN(Number(v)) ? Number(v) : undefined)
|
|
253
|
+
const str = (v: unknown) => (typeof v === 'string' ? v : undefined)
|
|
254
|
+
|
|
255
|
+
function onSubmit() {
|
|
256
|
+
// The values of the fields on show, flat by name as the schema names them
|
|
257
|
+
const out: Record<string, unknown> = {}
|
|
258
|
+
for (const f of visible.value) if (f.name && isValueField(f)) out[f.name] = local.value[f.name]
|
|
259
|
+
emit('submit', out)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
defineExpose({
|
|
263
|
+
/** Checks every field on show (or the named ones) and shows their messages; resolves to whether they pass */
|
|
264
|
+
validate: (...args: Parameters<NonNullable<typeof form.value>['validate']>) => form.value!.validate(...args),
|
|
265
|
+
/** Clears the messages */
|
|
266
|
+
resetValidation: (names?: string[]) => form.value?.resetValidation(names),
|
|
267
|
+
/** The values, defaults included */
|
|
268
|
+
values: computed(() => local.value),
|
|
269
|
+
})
|
|
270
|
+
</script>
|
|
271
|
+
|
|
272
|
+
<template>
|
|
273
|
+
<MForm
|
|
274
|
+
ref="form"
|
|
275
|
+
class="m3-form-renderer @container"
|
|
276
|
+
:validate-on="validateOn"
|
|
277
|
+
:validate-debounce="validateDebounce"
|
|
278
|
+
:errors="errors"
|
|
279
|
+
:disabled="disabled"
|
|
280
|
+
@submit="onSubmit"
|
|
281
|
+
@invalid="emit('invalid', $event)"
|
|
282
|
+
>
|
|
283
|
+
<div v-if="schema.title || schema.description" class="mb-4">
|
|
284
|
+
<h2 v-if="schema.title" class="text-headline-small text-on-surface">{{ schema.title }}</h2>
|
|
285
|
+
<p v-if="schema.description" class="mt-1 text-body-medium text-on-surface-variant">{{ schema.description }}</p>
|
|
286
|
+
</div>
|
|
287
|
+
<div class="grid grid-cols-12 gap-x-4 gap-y-5">
|
|
288
|
+
<template v-for="(f, i) in visible" :key="f.name ?? `${f.type}-${i}`">
|
|
289
|
+
<div v-if="f.type === 'heading'" class="col-span-12 pt-2">
|
|
290
|
+
<h3 class="text-title-medium text-on-surface">{{ f.label }}</h3>
|
|
291
|
+
<p v-if="f.description" class="mt-0.5 text-body-medium text-on-surface-variant">{{ f.description }}</p>
|
|
292
|
+
</div>
|
|
293
|
+
<MDivider v-else-if="f.type === 'divider'" class="col-span-12" />
|
|
294
|
+
<div v-else :class="span[f.width ?? 'full']" :data-field="f.name">
|
|
295
|
+
<slot
|
|
296
|
+
:name="`field-${f.name}`"
|
|
297
|
+
:field="f"
|
|
298
|
+
:value="local[f.name!]"
|
|
299
|
+
:update="(v: unknown) => set(f.name!, v)"
|
|
300
|
+
>
|
|
301
|
+
<div v-if="f.type === 'rating'" class="flex flex-col gap-1">
|
|
302
|
+
<span class="text-body-medium text-on-surface-variant">{{ f.label }}<span v-if="f.required" aria-hidden="true"> *</span></span>
|
|
303
|
+
<component :is="control(f).is" v-bind="control(f).props" :model-value="local[f.name!]" :aria-label="f.label" @update:model-value="set(f.name!, $event)" />
|
|
304
|
+
</div>
|
|
305
|
+
<component
|
|
306
|
+
:is="control(f).is"
|
|
307
|
+
v-else
|
|
308
|
+
v-bind="control(f).props"
|
|
309
|
+
:model-value="local[f.name!]"
|
|
310
|
+
class="w-full"
|
|
311
|
+
@update:model-value="set(f.name!, $event)"
|
|
312
|
+
/>
|
|
313
|
+
</slot>
|
|
314
|
+
</div>
|
|
315
|
+
</template>
|
|
316
|
+
</div>
|
|
317
|
+
<div v-if="actions" class="mt-6 flex justify-end gap-2">
|
|
318
|
+
<slot name="actions" :reset="() => form?.reset()">
|
|
319
|
+
<MButton v-if="schema.resetLabel" type="reset" variant="text" :disabled="disabled">{{ schema.resetLabel }}</MButton>
|
|
320
|
+
<MButton type="submit" :loading="loading" :disabled="disabled">{{ schema.submitLabel ?? locale.formSubmit }}</MButton>
|
|
321
|
+
</slot>
|
|
322
|
+
</div>
|
|
323
|
+
</MForm>
|
|
324
|
+
</template>
|
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
* A JSON-aware editor built on top of MCodeEditor — validates as you type and adds format/minify shortcuts.
|
|
4
4
|
*
|
|
5
5
|
* @event update:modelValue Emitted with the parsed value whenever the text is valid JSON — powers v-model
|
|
6
|
+
* @event blur Emitted when focus leaves the editor
|
|
6
7
|
*/
|
|
7
|
-
import { ref, computed, watch } from 'vue'
|
|
8
|
+
import { ref, computed, inject, provide, watch } from 'vue'
|
|
9
|
+
import { FORM_KEY, useFieldValidation, useFocusLeave, type FormContext } from '../composables/_useFieldValidation'
|
|
10
|
+
import type { ValidateOn, ValidationRule } from '../composables/validation'
|
|
8
11
|
import MCodeEditor from './MCodeEditor.vue'
|
|
9
12
|
import MIcon from './MIcon.vue'
|
|
10
13
|
import { useLocale } from '../composables/useLocale'
|
|
@@ -17,6 +20,8 @@ export interface JsonEditorLabels {
|
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
const locale = useLocale()
|
|
23
|
+
// jsonRule is always there: `required` alone (no MForm, no rules) keeps today's behavior
|
|
24
|
+
const inForm = !!inject(FORM_KEY, null)
|
|
20
25
|
|
|
21
26
|
const props = withDefaults(
|
|
22
27
|
defineProps<{
|
|
@@ -24,6 +29,20 @@ const props = withDefaults(
|
|
|
24
29
|
modelValue: unknown
|
|
25
30
|
/** Read-only mode */
|
|
26
31
|
readonly?: boolean
|
|
32
|
+
/** Error state. A string is also the message */
|
|
33
|
+
error?: boolean | string
|
|
34
|
+
/** Error message shown when `error` is true */
|
|
35
|
+
errorLabel?: string
|
|
36
|
+
/** Inside an `MForm` or with `rules`, it can't be empty (null) */
|
|
37
|
+
required?: boolean
|
|
38
|
+
/** Validation rules (functions or a Standard Schema); see `MForm` */
|
|
39
|
+
rules?: ValidationRule | ValidationRule[]
|
|
40
|
+
/** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
|
|
41
|
+
validateOn?: ValidateOn
|
|
42
|
+
/** 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 */
|
|
43
|
+
validateDebounce?: number
|
|
44
|
+
/** The key of this field in its `MForm` */
|
|
45
|
+
name?: string
|
|
27
46
|
/** Min height */
|
|
28
47
|
minHeight?: string
|
|
29
48
|
/** Max height */
|
|
@@ -49,13 +68,38 @@ const l = computed<Required<JsonEditorLabels>>(() => ({
|
|
|
49
68
|
...props.labels,
|
|
50
69
|
}))
|
|
51
70
|
|
|
52
|
-
const emit = defineEmits<{ 'update:modelValue': [unknown] }>()
|
|
71
|
+
const emit = defineEmits<{ 'update:modelValue': [unknown]; blur: [] }>()
|
|
53
72
|
|
|
54
73
|
const rawText = ref(JSON.stringify(props.modelValue, null, 2))
|
|
55
74
|
const parseError = ref<string | null>(null)
|
|
56
75
|
|
|
57
76
|
const isValid = computed(() => !parseError.value)
|
|
58
77
|
|
|
78
|
+
// Text that doesn't parse is one more rule: it blocks an MForm submit
|
|
79
|
+
const jsonRule: ValidationRule = () => !parseError.value || l.value.invalid
|
|
80
|
+
const rootEl = ref<HTMLElement | null>(null)
|
|
81
|
+
const validation = useFieldValidation({
|
|
82
|
+
value: () => props.modelValue,
|
|
83
|
+
rules: () => [...(props.rules ? (Array.isArray(props.rules) ? props.rules : [props.rules]) : []), jsonRule],
|
|
84
|
+
required: () => props.required && (inForm || !!props.rules),
|
|
85
|
+
error: () => props.error,
|
|
86
|
+
errorLabel: () => props.errorLabel,
|
|
87
|
+
name: () => props.name,
|
|
88
|
+
validateOn: () => props.validateOn,
|
|
89
|
+
validateDebounce: () => props.validateDebounce,
|
|
90
|
+
disabled: () => props.readonly,
|
|
91
|
+
focus: () => { rootEl.value?.querySelector<HTMLElement>('.cm-content')?.focus() },
|
|
92
|
+
el: () => rootEl.value,
|
|
93
|
+
setValue: (v) => emit('update:modelValue', v),
|
|
94
|
+
})
|
|
95
|
+
const { hasError, message: errorMessage, messageId } = validation
|
|
96
|
+
const onFocusOut = useFocusLeave(() => rootEl.value, () => {
|
|
97
|
+
validation.onBlur()
|
|
98
|
+
emit('blur')
|
|
99
|
+
})
|
|
100
|
+
// The inner MCodeEditor edits text, not this value: keep it out of the form
|
|
101
|
+
provide(FORM_KEY, null as unknown as FormContext)
|
|
102
|
+
|
|
59
103
|
watch(() => props.modelValue, (val) => {
|
|
60
104
|
let current: unknown
|
|
61
105
|
try {
|
|
@@ -103,13 +147,21 @@ function minifyJson() {
|
|
|
103
147
|
// keep error state
|
|
104
148
|
}
|
|
105
149
|
}
|
|
150
|
+
|
|
151
|
+
defineExpose({
|
|
152
|
+
/** Checks its rules and shows the result; resolves to whether they pass */
|
|
153
|
+
validate: validation.validate,
|
|
154
|
+
/** Hides its validation error (until the next check) */
|
|
155
|
+
resetValidation: validation.resetValidation,
|
|
156
|
+
})
|
|
106
157
|
</script>
|
|
107
158
|
|
|
108
159
|
<template>
|
|
109
|
-
<div class="flex flex-col gap-0">
|
|
160
|
+
<div ref="rootEl" class="flex flex-col gap-0" @focusout="onFocusOut">
|
|
110
161
|
<MCodeEditor
|
|
111
162
|
:model-value="rawText"
|
|
112
163
|
language="json"
|
|
164
|
+
:error="hasError && !parseError"
|
|
113
165
|
:readonly="readonly"
|
|
114
166
|
:min-height="minHeight"
|
|
115
167
|
:max-height="maxHeight"
|
|
@@ -153,5 +205,7 @@ function minifyJson() {
|
|
|
153
205
|
<p v-if="parseError" class="px-3 py-1.5 text-body-small text-error">
|
|
154
206
|
{{ parseError }}
|
|
155
207
|
</p>
|
|
208
|
+
<!-- The parse error above already says what's wrong with the text -->
|
|
209
|
+
<p v-else-if="errorMessage" :id="messageId" role="alert" class="px-3 py-1.5 text-body-small text-error">{{ errorMessage }}</p>
|
|
156
210
|
</div>
|
|
157
211
|
</template>
|