@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,77 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
FormulaError, cellName, columnIndex, columnName, createSheetEvaluator, formatNumber, parseCellName, shiftFormula,
|
|
4
|
+
type SpreadsheetCell,
|
|
5
|
+
} from '../composables/spreadsheetFormula'
|
|
6
|
+
|
|
7
|
+
const sheet = (cells: SpreadsheetCell[][]) => createSheetEvaluator(cells)
|
|
8
|
+
const code = (v: unknown) => (v instanceof FormulaError ? v.code : v)
|
|
9
|
+
|
|
10
|
+
describe('A1 notation', () => {
|
|
11
|
+
it('columns and cells', () => {
|
|
12
|
+
expect([0, 25, 26, 51, 701, 702].map(columnName)).toEqual(['A', 'Z', 'AA', 'AZ', 'ZZ', 'AAA'])
|
|
13
|
+
expect(columnIndex('AA')).toBe(26)
|
|
14
|
+
expect(cellName(2, 1)).toBe('B3')
|
|
15
|
+
expect(parseCellName('$C$10')).toEqual({ row: 9, col: 2 })
|
|
16
|
+
expect(parseCellName('hello')).toBeNull()
|
|
17
|
+
})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe('formulas', () => {
|
|
21
|
+
it('arithmetic with precedence, unary minus, percent and power', () => {
|
|
22
|
+
const s = sheet([['=1+2*3', '=(1+2)*3', '=-2^2', '=50%*10', '=2^3^2', '=0.1+0.2']])
|
|
23
|
+
expect([0, 1, 2, 3, 4, 5].map((c) => s.value(0, c))).toEqual([7, 9, 4, 5, 64, 0.30000000000000004])
|
|
24
|
+
expect(formatNumber(0.1 + 0.2)).toBe('0.3')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('references, ranges and functions; text in a range is skipped', () => {
|
|
28
|
+
const s = sheet([
|
|
29
|
+
[1, 2, 'x', '=SUM(A1:C1)', '=AVERAGE(A1:B1)', '=COUNT(A1:C1)', '=COUNTA(A1:C1)'],
|
|
30
|
+
['=A1+B1', '=A2*10', '=MAX(A1:B2)', '=IF(A2>2,"big","small")', '=ROUND(2/3,2)', '=CONCAT(C1,"-",A1)', '=LEN("hello")'],
|
|
31
|
+
])
|
|
32
|
+
expect([3, 4, 5, 6].map((c) => s.value(0, c))).toEqual([3, 1.5, 2, 3])
|
|
33
|
+
expect([0, 1, 2, 3, 4, 5, 6].map((c) => s.value(1, c))).toEqual([3, 30, 30, 'big', 0.67, 'x-1', 5])
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('comparison, text joining, booleans and case-insensitive names', () => {
|
|
37
|
+
const s = sheet([['="a"&"b"', '=1<2', '=sum(1,2)', '="A"="a"', '=AND(TRUE,1>0)', '=NOT(FALSE)']])
|
|
38
|
+
expect([0, 1, 2, 3, 4, 5].map((c) => s.value(0, c))).toEqual(['ab', true, 3, true, true, true])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('errors: #DIV/0!, #NAME?, #VALUE!, a syntax error, and IFERROR', () => {
|
|
42
|
+
const s = sheet([['=1/0', '=NOPE(1)', '="a"*2', '=1+', '=IFERROR(A1,"none")', '=A1+1']])
|
|
43
|
+
expect([0, 1, 2, 3, 4, 5].map((c) => code(s.value(0, c)))).toEqual(['#DIV/0!', '#NAME?', '#VALUE!', '#ERROR!', 'none', '#DIV/0!'])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('a circular reference is #CIRC!, not a hang', () => {
|
|
47
|
+
const s = sheet([['=B1', '=A1', '=C1+1']])
|
|
48
|
+
expect(code(s.value(0, 0))).toBe('#CIRC!')
|
|
49
|
+
expect(code(s.value(0, 1))).toBe('#CIRC!')
|
|
50
|
+
expect(code(s.value(0, 2))).toBe('#CIRC!')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('SUMIF and COUNTIF with conditions', () => {
|
|
54
|
+
const s = sheet([['a', 'b', 'a'], [1, 2, 3], ['=SUMIF(A1:C1,"a",A2:C2)', '=COUNTIF(A2:C2,">1")', '=SUMIF(A2:C2,"<>2")']])
|
|
55
|
+
expect([0, 1, 2].map((c) => s.value(2, c))).toEqual([4, 2, 4])
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('custom functions', () => {
|
|
59
|
+
const s = createSheetEvaluator([['=DOUBLE(21)']], { functions: { DOUBLE: ([a]) => (a as number) * 2 } })
|
|
60
|
+
expect(s.value(0, 0)).toBe(42)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('a reference outside the sheet is #REF!', () => {
|
|
64
|
+
const s = createSheetEvaluator([['=Z99']], { rows: 10, cols: 5 })
|
|
65
|
+
expect(code(s.value(0, 0))).toBe('#REF!')
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('shiftFormula', () => {
|
|
70
|
+
it('moves relative references, keeps $-locked parts and function names', () => {
|
|
71
|
+
expect(shiftFormula('=A1+$B$2+C$3+$D4', 2, 1)).toBe('=B3+$B$2+D$3+$D6')
|
|
72
|
+
expect(shiftFormula('=SUM(A1:A3)*LOG10(B1)', 1, 0)).toBe('=SUM(A2:A4)*LOG10(B2)')
|
|
73
|
+
expect(shiftFormula('="A1"&A1', 1, 0)).toBe('="A1"&A2')
|
|
74
|
+
expect(shiftFormula('=A1', -1, 0)).toBe('=#REF!')
|
|
75
|
+
expect(shiftFormula('plain', 1, 1)).toBe('plain')
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -26,6 +26,10 @@ const props: Record<string, Record<string, unknown>> = {
|
|
|
26
26
|
MVirtualTable: { columns: [{ key: 'a', label: 'A' }], rows: [{ a: 1 }] },
|
|
27
27
|
MTreeTable: { columns: [{ key: 'a', label: 'A' }], rows: [] },
|
|
28
28
|
MTree: { nodes: [{ id: 1, label: 'a' }] },
|
|
29
|
+
MTrackingTimeline: { steps: [{ label: 'Placed' }, { label: 'Delivered' }], current: 0, eta: 0 },
|
|
30
|
+
MSpreadsheet: { modelValue: [[1, '=A1*2']], rows: 5, cols: 3 },
|
|
31
|
+
MFormRenderer: { schema: { fields: [{ type: 'text', name: 'a', label: 'A', required: true }, { type: 'heading', label: 'H' }] }, modelValue: {} },
|
|
32
|
+
MFormBuilder: { modelValue: { fields: [{ type: 'text', name: 'a', label: 'A' }] } },
|
|
29
33
|
MKbd: { keys: 'Ctrl+K' },
|
|
30
34
|
MOrderTicket: { number: 1, createdAt: 0, items: [{ id: 1, name: 'Pizza' }] },
|
|
31
35
|
MSeatMap: { layout: ['ss_ss', 'ss_ss'], modelValue: ['A1'] },
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { defaultLocale } from '../composables/useLocale'
|
|
3
|
+
import { email, isEmpty, max, maxLength, min, minLength, pattern, required, runRules, sameAs, url, type StandardSchemaV1 } from '../composables/validation'
|
|
4
|
+
|
|
5
|
+
const ctx = { locale: defaultLocale, values: {} as Record<string, unknown> }
|
|
6
|
+
const check = (rule: ReturnType<typeof required>, value: unknown, values = {}) => runRules([rule], value, { ...ctx, values })
|
|
7
|
+
|
|
8
|
+
/** A minimal Standard Schema, like zod's/valibot's */
|
|
9
|
+
function schema(test: (v: unknown) => string | null): StandardSchemaV1 {
|
|
10
|
+
return { '~standard': { version: 1, vendor: 'test', validate: (v) => { const m = test(v); return m ? { issues: [{ message: m }] } : { value: v } } } }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('validators', () => {
|
|
14
|
+
it('isEmpty', () => {
|
|
15
|
+
for (const v of [null, undefined, '', ' ', [], false]) expect(isEmpty(v)).toBe(true)
|
|
16
|
+
for (const v of [0, 'a', [1], true]) expect(isEmpty(v)).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('required uses the locale message, or a custom one', async () => {
|
|
20
|
+
expect(await check(required(), '')).toBe(defaultLocale.validationRequired)
|
|
21
|
+
expect(await check(required('Hey'), ' ')).toBe('Hey')
|
|
22
|
+
expect(await check(required(), 'x')).toBeNull()
|
|
23
|
+
expect(await check(required(), 0)).toBeNull()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('every other rule passes on an empty value', async () => {
|
|
27
|
+
for (const rule of [email(), url(), minLength(3), maxLength(1), min(5), max(1), pattern(/x/), sameAs('a')]) {
|
|
28
|
+
expect(await check(rule, '', { a: 'b' })).toBeNull()
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('email and url', async () => {
|
|
33
|
+
expect(await check(email(), 'a@b.co')).toBeNull()
|
|
34
|
+
expect(await check(email(), 'a@b')).toBe(defaultLocale.validationEmail)
|
|
35
|
+
expect(await check(url(), 'https://m3ui.dev/x')).toBeNull()
|
|
36
|
+
expect(await check(url(), 'ftp://x.y')).toBe(defaultLocale.validationUrl)
|
|
37
|
+
expect(await check(url(), 'nope')).toBe(defaultLocale.validationUrl)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('lengths fill in {min}/{max}, with item wording for arrays', async () => {
|
|
41
|
+
expect(await check(minLength(3), 'ab')).toBe('Enter at least 3 characters')
|
|
42
|
+
expect(await check(maxLength(2), 'abc')).toBe('Enter at most 2 characters')
|
|
43
|
+
expect(await check(minLength(2), ['a'])).toBe('Choose at least 2')
|
|
44
|
+
expect(await check(maxLength(1), ['a', 'b'])).toBe('Choose at most 1')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('min/max accept numeric strings', async () => {
|
|
48
|
+
expect(await check(min(18), '17')).toBe('Must be at least 18')
|
|
49
|
+
expect(await check(min(18), 18)).toBeNull()
|
|
50
|
+
expect(await check(max(10), '11')).toBe('Must be at most 10')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('pattern resets a global regex between calls', async () => {
|
|
54
|
+
const rule = pattern(/^\d+$/g)
|
|
55
|
+
expect(await check(rule, '12')).toBeNull()
|
|
56
|
+
expect(await check(rule, '12')).toBeNull()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('sameAs reads another field by name, or a getter', async () => {
|
|
60
|
+
expect(await check(sameAs('password'), 'x', { password: 'y' })).toBe(defaultLocale.validationSameAs)
|
|
61
|
+
expect(await check(sameAs('password'), 'x', { password: 'x' })).toBeNull()
|
|
62
|
+
expect(await check(sameAs(() => 'x'), 'x')).toBeNull()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('custom rules: false → generic message, string → that message, async works', async () => {
|
|
66
|
+
expect(await runRules([() => false], 'a', ctx)).toBe(defaultLocale.validationInvalid)
|
|
67
|
+
expect(await runRules([() => 'Nope'], 'a', ctx)).toBe('Nope')
|
|
68
|
+
expect(await runRules([async () => 'Later'], 'a', ctx)).toBe('Later')
|
|
69
|
+
expect(await runRules([() => true, () => 'Second'], 'a', ctx)).toBe('Second')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('a Standard Schema works as a rule', async () => {
|
|
73
|
+
const s = schema((v) => (String(v).length < 2 ? 'Too short' : null))
|
|
74
|
+
expect(await runRules([s], 'a', ctx)).toBe('Too short')
|
|
75
|
+
expect(await runRules([s], 'ab', ctx)).toBeNull()
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -9,8 +9,11 @@
|
|
|
9
9
|
*
|
|
10
10
|
* @event update:modelValue A place was picked, or the text changed after picking one (null)
|
|
11
11
|
* @event select A place was picked
|
|
12
|
+
* @event blur Focus left the field and its list
|
|
12
13
|
*/
|
|
13
|
-
import { computed, nextTick, onBeforeUnmount, ref, shallowRef, useId, watch, watchEffect } from 'vue'
|
|
14
|
+
import { computed, nextTick, onBeforeUnmount, provide, ref, shallowRef, useId, watch, watchEffect } from 'vue'
|
|
15
|
+
import { FORM_KEY, useFieldValidation, type FormContext } from '../composables/_useFieldValidation'
|
|
16
|
+
import type { ValidateOn, ValidationRule } from '../composables/validation'
|
|
14
17
|
import MIcon from './MIcon.vue'
|
|
15
18
|
import MIconButton from './MIconButton.vue'
|
|
16
19
|
import MSpinner from './MSpinner.vue'
|
|
@@ -44,11 +47,19 @@ const props = withDefaults(
|
|
|
44
47
|
minLength?: number
|
|
45
48
|
/** Helper text below the field */
|
|
46
49
|
hint?: string
|
|
47
|
-
/** Error state */
|
|
48
|
-
error?: boolean
|
|
50
|
+
/** Error state. A string is also the message */
|
|
51
|
+
error?: boolean | string
|
|
49
52
|
/** Error message shown when `error` is true */
|
|
50
53
|
errorLabel?: string
|
|
51
|
-
/**
|
|
54
|
+
/** Validation rules (functions or a Standard Schema), run on the picked place (null until one is picked); see `MForm` */
|
|
55
|
+
rules?: ValidationRule | ValidationRule[]
|
|
56
|
+
/** When the rules are checked and shown: on leaving the field (default), while typing, or only on submit */
|
|
57
|
+
validateOn?: ValidateOn
|
|
58
|
+
/** 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 */
|
|
59
|
+
validateDebounce?: number
|
|
60
|
+
/** The key of this field in its `MForm` */
|
|
61
|
+
name?: string
|
|
62
|
+
/** Marks it as required; inside an `MForm` or with `rules`, a place must be picked (typed text alone isn't enough) */
|
|
52
63
|
required?: boolean
|
|
53
64
|
/** Disables the field */
|
|
54
65
|
disabled?: boolean
|
|
@@ -59,6 +70,7 @@ const props = withDefaults(
|
|
|
59
70
|
const emit = defineEmits<{
|
|
60
71
|
'update:modelValue': [place: MapSearchResult | null]
|
|
61
72
|
select: [place: MapSearchResult]
|
|
73
|
+
blur: []
|
|
62
74
|
}>()
|
|
63
75
|
|
|
64
76
|
const t = useLocale()
|
|
@@ -78,6 +90,26 @@ let controller: AbortController | undefined
|
|
|
78
90
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
79
91
|
|
|
80
92
|
const field = ref<InstanceType<typeof MTextField>>()
|
|
93
|
+
|
|
94
|
+
// Validates the picked place, not the text: the inner MTextField is kept out of the MForm
|
|
95
|
+
const validation = useFieldValidation({
|
|
96
|
+
value: () => props.modelValue,
|
|
97
|
+
rules: () => props.rules,
|
|
98
|
+
required: () => props.required,
|
|
99
|
+
error: () => props.error,
|
|
100
|
+
errorLabel: () => props.errorLabel,
|
|
101
|
+
name: () => props.name,
|
|
102
|
+
validateOn: () => props.validateOn,
|
|
103
|
+
validateDebounce: () => props.validateDebounce,
|
|
104
|
+
disabled: () => props.disabled,
|
|
105
|
+
focus: () => field.value?.focus(),
|
|
106
|
+
el: () => wrapEl.value,
|
|
107
|
+
setValue: (v) => emit('update:modelValue', (v as MapSearchResult | null) ?? null),
|
|
108
|
+
})
|
|
109
|
+
/** Its own `disabled` or its MForm's */
|
|
110
|
+
const isDisabled = validation.disabled
|
|
111
|
+
const fieldError = computed(() => validation.message.value || validation.hasError.value)
|
|
112
|
+
provide(FORM_KEY, null as unknown as FormContext)
|
|
81
113
|
const wrapEl = ref<HTMLElement>()
|
|
82
114
|
const listEl = ref<HTMLElement>()
|
|
83
115
|
// The field's box (MTextField's first child), without its hint
|
|
@@ -181,6 +213,8 @@ function onFocusOut(e: FocusEvent) {
|
|
|
181
213
|
if (next && (wrapEl.value?.contains(next) || listEl.value?.contains(next))) return
|
|
182
214
|
focused.value = false
|
|
183
215
|
open.value = false
|
|
216
|
+
validation.onBlur()
|
|
217
|
+
emit('blur')
|
|
184
218
|
}
|
|
185
219
|
|
|
186
220
|
// Escape closes the list (and only that, inside a dialog)
|
|
@@ -231,6 +265,10 @@ defineExpose({
|
|
|
231
265
|
clear,
|
|
232
266
|
/** Focuses the field */
|
|
233
267
|
focus: () => field.value?.focus(),
|
|
268
|
+
/** Checks its rules and shows the result; resolves to whether they pass */
|
|
269
|
+
validate: validation.validate,
|
|
270
|
+
/** Hides its validation error (until the next check) */
|
|
271
|
+
resetValidation: validation.resetValidation,
|
|
234
272
|
})
|
|
235
273
|
</script>
|
|
236
274
|
|
|
@@ -243,17 +281,16 @@ defineExpose({
|
|
|
243
281
|
:variant="variant"
|
|
244
282
|
:leading-icon="leadingIcon"
|
|
245
283
|
:hint="hint"
|
|
246
|
-
:error="
|
|
247
|
-
:error-label="errorLabel"
|
|
284
|
+
:error="fieldError"
|
|
248
285
|
:required="required"
|
|
249
|
-
:disabled="
|
|
286
|
+
:disabled="isDisabled"
|
|
250
287
|
autocomplete="street-address"
|
|
251
288
|
@update:model-value="onText"
|
|
252
289
|
>
|
|
253
290
|
<!-- Always passed: MTextField reserves the room for #trailing only when the slot exists from the start -->
|
|
254
291
|
<template #trailing>
|
|
255
292
|
<MSpinner v-if="loading" :size="20" class="m-2" />
|
|
256
|
-
<MIconButton v-else-if="text" icon="close" :label="t.clearSearch" size="sm" :disabled="
|
|
293
|
+
<MIconButton v-else-if="text" icon="close" :label="t.clearSearch" size="sm" :disabled="isDisabled" @click="clear" />
|
|
257
294
|
</template>
|
|
258
295
|
</MTextField>
|
|
259
296
|
</div>
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
* @event debounced Emitted debounce ms after the user stops typing the search query (only when debounce > 0)
|
|
7
7
|
*/
|
|
8
8
|
import { computed, ref, useId, onMounted, onUnmounted, watch, nextTick } from 'vue'
|
|
9
|
+
import { useFieldValidation, useFocusLeave } from '../composables/_useFieldValidation'
|
|
10
|
+
import type { ValidateOn, ValidationRule } from '../composables/validation'
|
|
9
11
|
import MIcon from './MIcon.vue'
|
|
10
12
|
import MIconButton from './MIconButton.vue'
|
|
11
13
|
import { useFieldBg } from '../composables/useFieldBg'
|
|
@@ -37,10 +39,21 @@ const props = withDefaults(
|
|
|
37
39
|
mode?: 'docked' | 'modal'
|
|
38
40
|
/** Disables the field */
|
|
39
41
|
disabled?: boolean
|
|
40
|
-
/** Error state */
|
|
41
|
-
error?: boolean
|
|
42
|
+
/** Error state. A string is also the message (`:error="errors.x"`) */
|
|
43
|
+
error?: boolean | string
|
|
42
44
|
/** Error message shown when error is true */
|
|
43
45
|
errorLabel?: string
|
|
46
|
+
/**
|
|
47
|
+
* Validation rules: functions `(value, ctx) => true | 'message'` (may be async; see the built-in `required`,
|
|
48
|
+
* `email`, `minLength`…) or a Standard Schema (zod, valibot…). Checked in order; the first failure is shown
|
|
49
|
+
*/
|
|
50
|
+
rules?: ValidationRule | ValidationRule[]
|
|
51
|
+
/** When the rules are checked and shown: on leaving the field (default; live after that), while typing, or only on submit (`MForm`) */
|
|
52
|
+
validateOn?: ValidateOn
|
|
53
|
+
/** 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 */
|
|
54
|
+
validateDebounce?: number
|
|
55
|
+
/** The key of this field in its `MForm`'s values, errors and schema */
|
|
56
|
+
name?: string
|
|
44
57
|
/** Helper text below field */
|
|
45
58
|
hint?: string
|
|
46
59
|
/** 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]; debounced: [string] }>()
|
|
85
|
+
const emit = defineEmits<{ blur: []; 'update:modelValue': [unknown]; debounced: [string] }>()
|
|
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: () => { inputEl.value?.focus() },
|
|
99
|
+
el: () => inputEl.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()
|
|
@@ -148,7 +180,7 @@ const modalFilteredOptions = computed(() => {
|
|
|
148
180
|
})
|
|
149
181
|
|
|
150
182
|
function openDropdown() {
|
|
151
|
-
if (
|
|
183
|
+
if (isDisabled.value || open.value) return
|
|
152
184
|
if (props.mode === 'modal') {
|
|
153
185
|
modalOpen.value = true
|
|
154
186
|
modalSearch.value = ''
|
|
@@ -288,20 +320,20 @@ const triggerClasses = computed(() => {
|
|
|
288
320
|
]
|
|
289
321
|
|
|
290
322
|
if (props.variant === 'outlined') {
|
|
291
|
-
const border =
|
|
323
|
+
const border = isDisabled.value
|
|
292
324
|
? 'border-on-surface/12'
|
|
293
325
|
: open.value
|
|
294
|
-
? (
|
|
295
|
-
: (
|
|
326
|
+
? (hasError.value ? 'border-2 border-error' : 'border-2 border-primary')
|
|
327
|
+
: (hasError.value ? 'border-error' : 'border-outline hover:border-on-surface')
|
|
296
328
|
return [...base, 'h-14 rounded-xs border bg-transparent', border].join(' ')
|
|
297
329
|
}
|
|
298
330
|
|
|
299
|
-
const border =
|
|
331
|
+
const border = isDisabled.value
|
|
300
332
|
? 'border-on-surface/38'
|
|
301
333
|
: open.value
|
|
302
|
-
? (
|
|
303
|
-
: (
|
|
304
|
-
const bg =
|
|
334
|
+
? (hasError.value ? 'border-b-2 border-error' : 'border-b-2 border-primary')
|
|
335
|
+
: (hasError.value ? 'border-error' : 'border-on-surface-variant hover:border-on-surface')
|
|
336
|
+
const bg = isDisabled.value
|
|
305
337
|
? 'bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]'
|
|
306
338
|
: 'bg-surface-container-highest'
|
|
307
339
|
return [...base, 'h-14 rounded-t-xs border-b pt-6 pb-2', bg, border].join(' ')
|
|
@@ -317,16 +349,29 @@ const labelClasses = computed(() =>
|
|
|
317
349
|
floated: isFloated.value,
|
|
318
350
|
color: [
|
|
319
351
|
open.value
|
|
320
|
-
? (
|
|
321
|
-
: (
|
|
352
|
+
? (hasError.value ? 'text-error' : 'text-primary')
|
|
353
|
+
: (hasError.value ? 'text-error' : 'text-on-surface-variant'),
|
|
322
354
|
'peer-disabled:text-on-surface/38',
|
|
323
355
|
].join(' '),
|
|
324
356
|
}),
|
|
325
357
|
)
|
|
358
|
+
|
|
359
|
+
defineExpose({
|
|
360
|
+
/** Checks its rules and shows the result; resolves to whether they pass */
|
|
361
|
+
validate: validation.validate,
|
|
362
|
+
/** Hides its validation error (until the next check) */
|
|
363
|
+
resetValidation: validation.resetValidation,
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
// Last: it watches `open`, declared above
|
|
367
|
+
const onFocusOut = useFocusLeave(() => rootEl.value, () => {
|
|
368
|
+
validation.onBlur()
|
|
369
|
+
emit('blur')
|
|
370
|
+
}, () => open.value)
|
|
326
371
|
</script>
|
|
327
372
|
|
|
328
373
|
<template>
|
|
329
|
-
<div class="flex flex-col gap-1" v-bind="$attrs">
|
|
374
|
+
<div ref="rootEl" class="flex flex-col gap-1" v-bind="$attrs" @focusout="onFocusOut">
|
|
330
375
|
<div
|
|
331
376
|
ref="fieldEl"
|
|
332
377
|
class="relative"
|
|
@@ -339,7 +384,7 @@ const labelClasses = computed(() =>
|
|
|
339
384
|
class="pointer-events-none absolute left-3.5"
|
|
340
385
|
:class="[
|
|
341
386
|
variant === 'filled' ? 'top-4.5' : 'top-4',
|
|
342
|
-
|
|
387
|
+
isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant',
|
|
343
388
|
]"
|
|
344
389
|
>
|
|
345
390
|
<MIcon :name="leadingIcon" :size="24" />
|
|
@@ -349,10 +394,11 @@ const labelClasses = computed(() =>
|
|
|
349
394
|
<input
|
|
350
395
|
:id="id"
|
|
351
396
|
ref="inputEl"
|
|
397
|
+
:aria-describedby="errorMessage || hint ? messageId : undefined"
|
|
352
398
|
type="text"
|
|
353
399
|
:value="displayValue"
|
|
354
400
|
:placeholder="!label ? placeholder : undefined"
|
|
355
|
-
:disabled="
|
|
401
|
+
:disabled="isDisabled"
|
|
356
402
|
autocomplete="off"
|
|
357
403
|
role="combobox"
|
|
358
404
|
aria-autocomplete="list"
|
|
@@ -360,13 +406,13 @@ const labelClasses = computed(() =>
|
|
|
360
406
|
:aria-controls="open || modalOpen ? listId : undefined"
|
|
361
407
|
:aria-activedescendant="activeOption"
|
|
362
408
|
:aria-required="required || undefined"
|
|
363
|
-
:aria-invalid="
|
|
364
|
-
:aria-disabled="
|
|
409
|
+
:aria-invalid="hasError || undefined"
|
|
410
|
+
:aria-disabled="isDisabled"
|
|
365
411
|
v-overflow-title
|
|
366
412
|
:class="[
|
|
367
413
|
triggerClasses,
|
|
368
414
|
'peer outline-none text-on-surface cursor-text',
|
|
369
|
-
|
|
415
|
+
hasError ? 'caret-error' : 'caret-primary',
|
|
370
416
|
'disabled:pointer-events-none disabled:cursor-not-allowed disabled:text-on-surface/38',
|
|
371
417
|
]"
|
|
372
418
|
@focus="onInputFocus"
|
|
@@ -383,7 +429,7 @@ const labelClasses = computed(() =>
|
|
|
383
429
|
<!-- Clear button -->
|
|
384
430
|
<button
|
|
385
431
|
:aria-label="locale.clear"
|
|
386
|
-
v-if="clearable && hasValue && !
|
|
432
|
+
v-if="clearable && hasValue && !isDisabled"
|
|
387
433
|
type="button"
|
|
388
434
|
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"
|
|
389
435
|
@click.stop="emit('update:modelValue', null); closeDropdown()"
|
|
@@ -399,14 +445,14 @@ const labelClasses = computed(() =>
|
|
|
399
445
|
class="transition-transform duration-200"
|
|
400
446
|
:class="[
|
|
401
447
|
open || modalOpen ? 'rotate-180' : '',
|
|
402
|
-
|
|
448
|
+
isDisabled ? 'text-on-surface/38' : 'text-on-surface-variant',
|
|
403
449
|
]"
|
|
404
450
|
/>
|
|
405
451
|
</div>
|
|
406
452
|
</div>
|
|
407
453
|
|
|
408
|
-
<p v-if="
|
|
409
|
-
<p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
|
|
454
|
+
<p v-if="errorMessage" :id="messageId" role="alert" class="px-4 text-body-small text-error">{{ errorMessage }}</p>
|
|
455
|
+
<p v-else-if="hint" :id="messageId" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
|
|
410
456
|
</div>
|
|
411
457
|
|
|
412
458
|
<!-- Docked dropdown -->
|
|
@@ -4,10 +4,14 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @slot default Custom label content — overrides the label prop when provided
|
|
6
6
|
* @event update:modelValue Emitted with the new checked state.
|
|
7
|
+
* @event blur Emitted when the checkbox loses focus
|
|
7
8
|
*/
|
|
9
|
+
import { ref } from "vue";
|
|
8
10
|
import MIcon from "./MIcon.vue";
|
|
11
|
+
import { useFieldValidation } from "../composables/_useFieldValidation";
|
|
12
|
+
import type { ValidateOn, ValidationRule } from "../composables/validation";
|
|
9
13
|
|
|
10
|
-
withDefaults(
|
|
14
|
+
const props = withDefaults(
|
|
11
15
|
defineProps<{
|
|
12
16
|
/** Checked state (v-model) */
|
|
13
17
|
modelValue: boolean;
|
|
@@ -21,11 +25,45 @@ withDefaults(
|
|
|
21
25
|
color?: "primary" | "secondary" | "tertiary" | "error";
|
|
22
26
|
/** Accessible name for the input when there's no visible label (e.g. a list row's checkbox). */
|
|
23
27
|
ariaLabel?: string;
|
|
28
|
+
/** Error state. A string is also the message */
|
|
29
|
+
error?: boolean | string;
|
|
30
|
+
/** Error message shown when `error` is true */
|
|
31
|
+
errorLabel?: string;
|
|
32
|
+
/** Must be ticked, inside an `MForm` or with `rules` */
|
|
33
|
+
required?: boolean;
|
|
34
|
+
/** Validation rules (functions or a Standard Schema); see `MForm` */
|
|
35
|
+
rules?: ValidationRule | ValidationRule[];
|
|
36
|
+
/** When the rules are checked and shown: on leaving it (default), on change, or only on submit */
|
|
37
|
+
validateOn?: ValidateOn;
|
|
38
|
+
/** 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 */
|
|
39
|
+
validateDebounce?: number;
|
|
40
|
+
/** The key of this field in its `MForm` */
|
|
41
|
+
name?: string;
|
|
24
42
|
}>(),
|
|
25
43
|
{ indeterminate: false, disabled: false, color: "primary" },
|
|
26
44
|
);
|
|
27
45
|
|
|
28
|
-
const emit = defineEmits<{ "update:modelValue": [boolean] }>();
|
|
46
|
+
const emit = defineEmits<{ "update:modelValue": [boolean]; blur: [FocusEvent] }>();
|
|
47
|
+
|
|
48
|
+
const inputEl = ref<HTMLInputElement | null>(null);
|
|
49
|
+
|
|
50
|
+
const validation = useFieldValidation({
|
|
51
|
+
value: () => props.modelValue,
|
|
52
|
+
rules: () => props.rules,
|
|
53
|
+
required: () => props.required,
|
|
54
|
+
error: () => props.error,
|
|
55
|
+
errorLabel: () => props.errorLabel,
|
|
56
|
+
name: () => props.name,
|
|
57
|
+
validateOn: () => props.validateOn,
|
|
58
|
+
validateDebounce: () => props.validateDebounce,
|
|
59
|
+
disabled: () => props.disabled,
|
|
60
|
+
focus: () => { inputEl.value?.focus() },
|
|
61
|
+
el: () => inputEl.value,
|
|
62
|
+
setValue: (v) => emit("update:modelValue", v as boolean),
|
|
63
|
+
});
|
|
64
|
+
/** Its own `disabled` or its MForm's */
|
|
65
|
+
const isDisabled = validation.disabled;
|
|
66
|
+
const { hasError, message: errorMessage, messageId } = validation;
|
|
29
67
|
|
|
30
68
|
// Same 4 named colors + fallback shape as MRadio's checkedColor map. M3's real
|
|
31
69
|
// CheckboxTokens only define Primary (SelectedContainerColor) and Error (as an error
|
|
@@ -45,19 +83,33 @@ const checkedBgClasses: Record<string, string> = {
|
|
|
45
83
|
tertiary: "bg-tertiary",
|
|
46
84
|
error: "bg-error",
|
|
47
85
|
};
|
|
86
|
+
|
|
87
|
+
// CheckboxTokens' error state: Error container / OnError icon when ticked, Error outline when not
|
|
88
|
+
const errorCheckedClasses = "border-error bg-error text-on-error";
|
|
89
|
+
|
|
90
|
+
defineExpose({
|
|
91
|
+
/** Checks its rules and shows the result; resolves to whether they pass */
|
|
92
|
+
validate: validation.validate,
|
|
93
|
+
/** Hides its validation error (until the next check) */
|
|
94
|
+
resetValidation: validation.resetValidation,
|
|
95
|
+
});
|
|
48
96
|
</script>
|
|
49
97
|
|
|
50
98
|
<template>
|
|
51
99
|
<label
|
|
52
|
-
class="group inline-flex
|
|
53
|
-
:class="
|
|
100
|
+
class="group inline-flex gap-2 select-none"
|
|
101
|
+
:class="[isDisabled ? 'cursor-not-allowed' : 'cursor-pointer', errorMessage ? 'items-start' : 'items-center']"
|
|
54
102
|
>
|
|
55
103
|
<!--
|
|
56
104
|
CheckboxTokens.StateLayerSize = 40dp (was 48px), StateLayerShape = CornerFull. The
|
|
57
105
|
negative margin cancels the touch target's own overhang past the visible 18px box
|
|
58
106
|
(40-18)/2 = 11px per side, so it doesn't push the label text over.
|
|
59
107
|
-->
|
|
60
|
-
|
|
108
|
+
<!-- With a message under the text, the box lines up with the text's first line instead of the middle -->
|
|
109
|
+
<span
|
|
110
|
+
class="relative -m-[11px] inline-flex h-10 w-10 shrink-0 items-center justify-center rounded-full"
|
|
111
|
+
:style="errorMessage ? { marginTop: '-8px' } : undefined"
|
|
112
|
+
>
|
|
61
113
|
<!--
|
|
62
114
|
Input must stay nested INSIDE this `relative` span (a true sibling of the state-layer
|
|
63
115
|
/focus-ring spans below, satisfying `peer-*`'s sibling-combinator requirement, but not
|
|
@@ -72,9 +124,15 @@ const checkedBgClasses: Record<string, string> = {
|
|
|
72
124
|
type="checkbox"
|
|
73
125
|
class="peer sr-only"
|
|
74
126
|
:checked="modelValue"
|
|
75
|
-
:disabled="
|
|
127
|
+
:disabled="isDisabled"
|
|
128
|
+
ref="inputEl"
|
|
129
|
+
:name="name"
|
|
130
|
+
:required="required"
|
|
76
131
|
:aria-label="ariaLabel"
|
|
132
|
+
:aria-invalid="hasError || undefined"
|
|
133
|
+
:aria-describedby="errorMessage ? messageId : undefined"
|
|
77
134
|
@change="emit('update:modelValue', !modelValue)"
|
|
135
|
+
@blur="validation.onBlur(); emit('blur', $event)"
|
|
78
136
|
/>
|
|
79
137
|
<!--
|
|
80
138
|
Hover/press state layer (8%/12%), tinted with the checkbox's own indicator color
|
|
@@ -85,22 +143,24 @@ const checkedBgClasses: Record<string, string> = {
|
|
|
85
143
|
the small 18px glyph itself.
|
|
86
144
|
-->
|
|
87
145
|
<span
|
|
88
|
-
v-if="!
|
|
146
|
+
v-if="!isDisabled"
|
|
89
147
|
class="pointer-events-none absolute inset-0 rounded-full opacity-0 transition-opacity duration-150 group-hover:opacity-[0.08] group-active:opacity-[0.12]"
|
|
90
|
-
:class="modelValue || indeterminate ? checkedBgClasses[color] : 'bg-on-surface'"
|
|
148
|
+
:class="hasError ? 'bg-error' : modelValue || indeterminate ? checkedBgClasses[color] : 'bg-on-surface'"
|
|
91
149
|
/>
|
|
92
150
|
<!-- Keyboard-focus ring (FocusIndicatorColor = secondary) -->
|
|
93
151
|
<span
|
|
94
|
-
v-if="!
|
|
152
|
+
v-if="!isDisabled"
|
|
95
153
|
class="pointer-events-none absolute inset-0 rounded-full ring-0 ring-secondary transition-[box-shadow] duration-150 peer-focus-visible:ring-2"
|
|
96
154
|
/>
|
|
97
155
|
|
|
98
156
|
<span
|
|
99
157
|
class="relative inline-flex h-4.5 w-4.5 items-center justify-center rounded-[2px] border-2 transition-colors"
|
|
100
158
|
:class="
|
|
101
|
-
|
|
159
|
+
isDisabled
|
|
102
160
|
? (modelValue || indeterminate ? 'border-on-surface/38 bg-on-surface/38 text-surface' : 'border-on-surface/38 text-transparent')
|
|
103
|
-
:
|
|
161
|
+
: hasError
|
|
162
|
+
? (modelValue || indeterminate ? errorCheckedClasses : 'border-error text-transparent')
|
|
163
|
+
: (modelValue || indeterminate ? checkedClasses[color] : 'border-on-surface-variant text-transparent group-hover:border-on-surface group-active:border-on-surface')
|
|
104
164
|
"
|
|
105
165
|
>
|
|
106
166
|
<MIcon
|
|
@@ -111,12 +171,15 @@ const checkedBgClasses: Record<string, string> = {
|
|
|
111
171
|
/>
|
|
112
172
|
</span>
|
|
113
173
|
</span>
|
|
114
|
-
<span
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
174
|
+
<span v-if="label || $slots.default || errorMessage" class="flex flex-col">
|
|
175
|
+
<span
|
|
176
|
+
v-if="label || $slots.default"
|
|
177
|
+
class="text-body-large"
|
|
178
|
+
:class="isDisabled ? 'text-on-surface/38' : 'text-on-surface'"
|
|
179
|
+
>
|
|
180
|
+
<slot>{{ label }}</slot><span v-if="required" class="text-error"> *</span>
|
|
181
|
+
</span>
|
|
182
|
+
<span v-if="errorMessage" :id="messageId" role="alert" class="text-body-small text-error">{{ errorMessage }}</span>
|
|
120
183
|
</span>
|
|
121
184
|
</label>
|
|
122
185
|
</template>
|