@getcortexlabs/mavis-core 0.1.0-alpha.4 → 0.1.0-alpha.5
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/README.md +2 -2
- package/dist/capabilities.d.ts +119 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +142 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/combobox.d.ts +42 -0
- package/dist/combobox.d.ts.map +1 -0
- package/dist/combobox.js +159 -0
- package/dist/combobox.js.map +1 -0
- package/dist/contracts.d.ts +1201 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +242 -0
- package/dist/contracts.js.map +1 -0
- package/dist/date.d.ts +96 -0
- package/dist/date.d.ts.map +1 -0
- package/dist/date.js +235 -0
- package/dist/date.js.map +1 -0
- package/dist/experimental-regions.d.ts +142 -0
- package/dist/experimental-regions.d.ts.map +1 -0
- package/dist/experimental-regions.js +315 -0
- package/dist/experimental-regions.js.map +1 -0
- package/dist/experimental.d.ts +13 -1
- package/dist/experimental.d.ts.map +1 -1
- package/dist/experimental.js +153 -23
- package/dist/experimental.js.map +1 -1
- package/dist/files.d.ts +87 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +328 -0
- package/dist/files.js.map +1 -0
- package/dist/form.d.ts +39 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/form.js +170 -0
- package/dist/form.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/navigation.d.ts +22 -0
- package/dist/navigation.d.ts.map +1 -0
- package/dist/navigation.js +54 -0
- package/dist/navigation.js.map +1 -0
- package/dist/vue/combobox.d.ts +155 -0
- package/dist/vue/combobox.d.ts.map +1 -0
- package/dist/vue/combobox.js +195 -0
- package/dist/vue/combobox.js.map +1 -0
- package/dist/vue/component-props.d.ts +1989 -0
- package/dist/vue/component-props.d.ts.map +1 -0
- package/dist/vue/component-props.js +18 -0
- package/dist/vue/component-props.js.map +1 -0
- package/dist/vue/contracts.d.ts +18 -0
- package/dist/vue/contracts.d.ts.map +1 -0
- package/dist/vue/contracts.js +18 -0
- package/dist/vue/contracts.js.map +1 -0
- package/dist/vue/date-calendar.d.ts +64 -0
- package/dist/vue/date-calendar.d.ts.map +1 -0
- package/dist/vue/date-calendar.js +65 -0
- package/dist/vue/date-calendar.js.map +1 -0
- package/dist/vue/date-picker.d.ts +927 -0
- package/dist/vue/date-picker.d.ts.map +1 -0
- package/dist/vue/date-picker.js +319 -0
- package/dist/vue/date-picker.js.map +1 -0
- package/dist/vue/date-props.d.ts +254 -0
- package/dist/vue/date-props.d.ts.map +1 -0
- package/dist/vue/date-props.js +74 -0
- package/dist/vue/date-props.js.map +1 -0
- package/dist/vue/files.d.ts +53 -0
- package/dist/vue/files.d.ts.map +1 -0
- package/dist/vue/files.js +40 -0
- package/dist/vue/files.js.map +1 -0
- package/dist/vue/forms.d.ts +4 -0
- package/dist/vue/forms.d.ts.map +1 -0
- package/dist/vue/forms.js +15 -0
- package/dist/vue/forms.js.map +1 -0
- package/dist/vue/global-components.d.ts +35 -2
- package/dist/vue/global-components.d.ts.map +1 -1
- package/dist/vue/index.d.ts +12 -2
- package/dist/vue/index.d.ts.map +1 -1
- package/dist/vue/index.js +10 -1
- package/dist/vue/index.js.map +1 -1
- package/dist/vue/inline-content.d.ts +3 -1
- package/dist/vue/inline-content.d.ts.map +1 -1
- package/dist/vue/props.d.ts +1026 -495
- package/dist/vue/props.d.ts.map +1 -1
- package/dist/vue/props.js +17 -118
- package/dist/vue/props.js.map +1 -1
- package/dist/vue/workflow-props.d.ts +110 -0
- package/dist/vue/workflow-props.d.ts.map +1 -0
- package/dist/vue/workflow-props.js +37 -0
- package/dist/vue/workflow-props.js.map +1 -0
- package/dist/vue/workflows.d.ts +267 -0
- package/dist/vue/workflows.d.ts.map +1 -0
- package/dist/vue/workflows.js +200 -0
- package/dist/vue/workflows.js.map +1 -0
- package/package.json +1 -1
- package/src/capabilities.ts +145 -0
- package/src/combobox.ts +198 -0
- package/src/contracts.ts +295 -0
- package/src/date.ts +280 -0
- package/src/experimental-regions.ts +463 -0
- package/src/experimental.ts +158 -16
- package/src/files.ts +408 -0
- package/src/form.ts +190 -0
- package/src/index.ts +17 -0
- package/src/navigation.ts +61 -0
- package/src/vue/combobox.ts +238 -0
- package/src/vue/component-props.ts +24 -0
- package/src/vue/contracts.ts +33 -0
- package/src/vue/date-calendar.ts +76 -0
- package/src/vue/date-picker.ts +387 -0
- package/src/vue/date-props.ts +81 -0
- package/src/vue/files.ts +53 -0
- package/src/vue/forms.ts +20 -0
- package/src/vue/global-components.ts +44 -2
- package/src/vue/index.ts +13 -2
- package/src/vue/props.ts +34 -130
- package/src/vue/workflow-props.ts +41 -0
- package/src/vue/workflows.ts +257 -0
package/src/form.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { InputHandle } from './input.js'
|
|
2
|
+
|
|
3
|
+
export interface FormError {
|
|
4
|
+
field: string
|
|
5
|
+
message: string
|
|
6
|
+
}
|
|
7
|
+
export interface FormField {
|
|
8
|
+
name: string
|
|
9
|
+
label: string
|
|
10
|
+
target: () => InputHandle | undefined | null
|
|
11
|
+
enabled?: () => boolean
|
|
12
|
+
/** Open the containing section before resolving its mounted input handle. */
|
|
13
|
+
reveal?: () => void | Promise<void>
|
|
14
|
+
}
|
|
15
|
+
export interface FormValidationContext {
|
|
16
|
+
readonly current: boolean
|
|
17
|
+
onCancel(dispose: () => void): void
|
|
18
|
+
}
|
|
19
|
+
export interface FormState {
|
|
20
|
+
readonly errors: readonly FormError[]
|
|
21
|
+
readonly validating: boolean
|
|
22
|
+
}
|
|
23
|
+
export interface FormController {
|
|
24
|
+
readonly state: FormState
|
|
25
|
+
readonly disposed: boolean
|
|
26
|
+
register(field: FormField): () => void
|
|
27
|
+
label(field: string): string | undefined
|
|
28
|
+
has(field: string): boolean
|
|
29
|
+
setErrors(errors: readonly FormError[]): void
|
|
30
|
+
subscribe(listener: (state: FormState) => void): () => void
|
|
31
|
+
focusError(field: string): Promise<boolean>
|
|
32
|
+
focusFirstError(): Promise<boolean>
|
|
33
|
+
/** Invalidate pending validation when the host draft changes. */
|
|
34
|
+
invalidate(): void
|
|
35
|
+
validate(
|
|
36
|
+
task: (context: FormValidationContext) => Promise<readonly FormError[]>,
|
|
37
|
+
): Promise<'valid' | 'invalid' | 'cancelled' | 'busy'>
|
|
38
|
+
dispose(): void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function errorsCopy(errors: readonly FormError[]) {
|
|
42
|
+
return Object.freeze(
|
|
43
|
+
errors.map((error) => {
|
|
44
|
+
if (!error || !error.field?.trim() || !error.message?.trim())
|
|
45
|
+
throw new TypeError('Form errors require a field name and a non-empty message.')
|
|
46
|
+
return Object.freeze({ field: error.field, message: error.message })
|
|
47
|
+
}),
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Coordinates public focus handles and validation results; never owns the field values. */
|
|
52
|
+
export function createFormController(initial: readonly FormError[] = []): FormController {
|
|
53
|
+
const fields = new Map<string, FormField>()
|
|
54
|
+
const listeners = new Set<(state: FormState) => void>()
|
|
55
|
+
let state: FormState = Object.freeze({ errors: errorsCopy(initial), validating: false })
|
|
56
|
+
let disposed = false
|
|
57
|
+
let generation = 0
|
|
58
|
+
let focusGeneration = 0
|
|
59
|
+
let cancellations = new Set<() => void>()
|
|
60
|
+
const publish = (next = state) => {
|
|
61
|
+
state = Object.freeze(next)
|
|
62
|
+
for (const listener of [...listeners]) listener(state)
|
|
63
|
+
}
|
|
64
|
+
const invalidate = () => {
|
|
65
|
+
generation++
|
|
66
|
+
focusGeneration++
|
|
67
|
+
const release = cancellations
|
|
68
|
+
cancellations = new Set()
|
|
69
|
+
const failures: unknown[] = []
|
|
70
|
+
for (const cancel of release) {
|
|
71
|
+
try {
|
|
72
|
+
cancel()
|
|
73
|
+
} catch (error) {
|
|
74
|
+
failures.push(error)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (state.validating) publish({ ...state, validating: false })
|
|
78
|
+
if (failures.length) throw new AggregateError(failures, 'Form validation cancellation failed.')
|
|
79
|
+
}
|
|
80
|
+
const focus = async (name: string, request: number) => {
|
|
81
|
+
const field = fields.get(name)
|
|
82
|
+
if (disposed || !field || field.enabled?.() === false) return false
|
|
83
|
+
await field.reveal?.()
|
|
84
|
+
if (
|
|
85
|
+
disposed ||
|
|
86
|
+
request !== focusGeneration ||
|
|
87
|
+
fields.get(name) !== field ||
|
|
88
|
+
field.enabled?.() === false
|
|
89
|
+
)
|
|
90
|
+
return false
|
|
91
|
+
const target = field.target()
|
|
92
|
+
if (!target) return false
|
|
93
|
+
target.focus()
|
|
94
|
+
return true
|
|
95
|
+
}
|
|
96
|
+
const controller: FormController = {
|
|
97
|
+
get state() {
|
|
98
|
+
return state
|
|
99
|
+
},
|
|
100
|
+
get disposed() {
|
|
101
|
+
return disposed
|
|
102
|
+
},
|
|
103
|
+
register(field) {
|
|
104
|
+
if (disposed) throw new Error('Form controller is disposed.')
|
|
105
|
+
if (!field.name?.trim() || !field.label?.trim() || typeof field.target !== 'function')
|
|
106
|
+
throw new TypeError('Form fields require a name, label and public handle getter.')
|
|
107
|
+
if (fields.has(field.name)) throw new RangeError(`Duplicate form field: ${field.name}`)
|
|
108
|
+
fields.set(field.name, field)
|
|
109
|
+
publish()
|
|
110
|
+
return () => {
|
|
111
|
+
if (fields.get(field.name) !== field) return
|
|
112
|
+
fields.delete(field.name)
|
|
113
|
+
publish()
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
label(name) {
|
|
117
|
+
return fields.get(name)?.label
|
|
118
|
+
},
|
|
119
|
+
has(name) {
|
|
120
|
+
return fields.has(name)
|
|
121
|
+
},
|
|
122
|
+
setErrors(errors) {
|
|
123
|
+
if (disposed) return
|
|
124
|
+
const next = errorsCopy(errors)
|
|
125
|
+
invalidate()
|
|
126
|
+
publish({ errors: next, validating: false })
|
|
127
|
+
},
|
|
128
|
+
subscribe(listener) {
|
|
129
|
+
if (disposed) return () => {}
|
|
130
|
+
listeners.add(listener)
|
|
131
|
+
return () => {
|
|
132
|
+
listeners.delete(listener)
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
focusError(name) {
|
|
136
|
+
return focus(name, ++focusGeneration)
|
|
137
|
+
},
|
|
138
|
+
async focusFirstError() {
|
|
139
|
+
const request = ++focusGeneration
|
|
140
|
+
for (const error of state.errors) {
|
|
141
|
+
if (request !== focusGeneration || disposed) return false
|
|
142
|
+
if (await focus(error.field, request)) return true
|
|
143
|
+
}
|
|
144
|
+
return false
|
|
145
|
+
},
|
|
146
|
+
invalidate,
|
|
147
|
+
async validate(task) {
|
|
148
|
+
if (disposed) return 'cancelled'
|
|
149
|
+
if (state.validating) return 'busy'
|
|
150
|
+
const request = ++generation
|
|
151
|
+
const release = new Set<() => void>()
|
|
152
|
+
cancellations = release
|
|
153
|
+
const current = () => !disposed && generation === request
|
|
154
|
+
publish({ ...state, validating: true })
|
|
155
|
+
try {
|
|
156
|
+
const result = await task({
|
|
157
|
+
get current() {
|
|
158
|
+
return current()
|
|
159
|
+
},
|
|
160
|
+
onCancel(cancel) {
|
|
161
|
+
if (current()) release.add(cancel)
|
|
162
|
+
else cancel()
|
|
163
|
+
},
|
|
164
|
+
})
|
|
165
|
+
if (!current()) return 'cancelled'
|
|
166
|
+
const errors = errorsCopy(result)
|
|
167
|
+
focusGeneration++
|
|
168
|
+
publish({ errors, validating: false })
|
|
169
|
+
return state.errors.length ? 'invalid' : 'valid'
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (!current()) return 'cancelled'
|
|
172
|
+
throw error
|
|
173
|
+
} finally {
|
|
174
|
+
release.clear()
|
|
175
|
+
if (current() && state.validating) publish({ ...state, validating: false })
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
dispose() {
|
|
179
|
+
if (disposed) return
|
|
180
|
+
disposed = true
|
|
181
|
+
try {
|
|
182
|
+
invalidate()
|
|
183
|
+
} finally {
|
|
184
|
+
fields.clear()
|
|
185
|
+
listeners.clear()
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
return controller
|
|
190
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -66,3 +66,20 @@ export { checkChipLabels, chipColors, type ChipTone } from './chip.js'
|
|
|
66
66
|
export { checkEmptyStateTitle } from './empty-state.js'
|
|
67
67
|
|
|
68
68
|
export { fontFamily, shadowStyle, validateWeight } from './theme/typography.js'
|
|
69
|
+
|
|
70
|
+
export { componentContracts, describeContract } from './contracts.js'
|
|
71
|
+
export type {
|
|
72
|
+
ContractProps,
|
|
73
|
+
PropertyValue,
|
|
74
|
+
InputEvents,
|
|
75
|
+
ButtonEvents,
|
|
76
|
+
TextInputHandle,
|
|
77
|
+
} from './contracts.js'
|
|
78
|
+
export { targetCapabilities, componentNames, themeContract } from './capabilities.js'
|
|
79
|
+
export type { ComponentName, CapabilityStatus } from './capabilities.js'
|
|
80
|
+
|
|
81
|
+
export * from './date.js'
|
|
82
|
+
export * from './combobox.js'
|
|
83
|
+
export * from './files.js'
|
|
84
|
+
export * from './form.js'
|
|
85
|
+
export * from './navigation.js'
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { validateChoices, type ChoiceOption, type ChoiceValue } from './choice.js'
|
|
2
|
+
|
|
3
|
+
export interface StepOption extends ChoiceOption {
|
|
4
|
+
status?: 'pending' | 'complete' | 'error'
|
|
5
|
+
}
|
|
6
|
+
export function validateSteps(steps: readonly StepOption[], current: ChoiceValue | null) {
|
|
7
|
+
validateChoices(steps, current)
|
|
8
|
+
if (!steps.length) throw new RangeError('Steps require at least one item.')
|
|
9
|
+
if (current !== null && !steps.some((step) => step.value === current))
|
|
10
|
+
throw new RangeError('The current step must exist in steps.')
|
|
11
|
+
for (const step of steps)
|
|
12
|
+
if (step.status && !['pending', 'complete', 'error'].includes(step.status))
|
|
13
|
+
throw new RangeError(`Unknown step status: ${step.status}`)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type PaginationItem = { kind: 'page'; page: number } | { kind: 'gap'; key: string }
|
|
17
|
+
export function pageCount(total: number, pageSize: number) {
|
|
18
|
+
if (!Number.isSafeInteger(total) || total < 0)
|
|
19
|
+
throw new RangeError('total must be a non-negative safe integer.')
|
|
20
|
+
if (!Number.isSafeInteger(pageSize) || pageSize < 1)
|
|
21
|
+
throw new RangeError('pageSize must be a positive safe integer.')
|
|
22
|
+
return Math.ceil(total / pageSize)
|
|
23
|
+
}
|
|
24
|
+
/** 1-based pages. Empty results retain page 1 without inventing a result page. */
|
|
25
|
+
export function pagination(total: number, pageSize: number, page: number, siblingCount = 1) {
|
|
26
|
+
const count = pageCount(total, pageSize)
|
|
27
|
+
if (!Number.isSafeInteger(page) || page < 1)
|
|
28
|
+
throw new RangeError('page must be a positive safe integer.')
|
|
29
|
+
if (!Number.isInteger(siblingCount) || siblingCount < 0 || siblingCount > 2)
|
|
30
|
+
throw new RangeError('siblingCount must be 0, 1 or 2.')
|
|
31
|
+
// A changed total can temporarily make the parent's page stale. Report it without mutation.
|
|
32
|
+
const outOfRange = page > Math.max(1, count)
|
|
33
|
+
const pages = new Set<number>()
|
|
34
|
+
if (count > 0) {
|
|
35
|
+
pages.add(1)
|
|
36
|
+
pages.add(count)
|
|
37
|
+
}
|
|
38
|
+
const anchor = Math.min(page, count)
|
|
39
|
+
for (
|
|
40
|
+
let value = Math.max(1, anchor - siblingCount);
|
|
41
|
+
value <= Math.min(count, anchor + siblingCount);
|
|
42
|
+
value++
|
|
43
|
+
)
|
|
44
|
+
pages.add(value)
|
|
45
|
+
const items: PaginationItem[] = []
|
|
46
|
+
let previous = 0
|
|
47
|
+
for (const value of [...pages].sort((a, b) => a - b)) {
|
|
48
|
+
if (previous && value - previous === 2) items.push({ kind: 'page', page: previous + 1 })
|
|
49
|
+
else if (previous && value - previous > 2)
|
|
50
|
+
items.push({ kind: 'gap', key: `${previous}-${value}` })
|
|
51
|
+
items.push({ kind: 'page', page: value })
|
|
52
|
+
previous = value
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
count,
|
|
56
|
+
items,
|
|
57
|
+
outOfRange,
|
|
58
|
+
previous: count > 0 && page > 1 ? Math.min(page - 1, count) : null,
|
|
59
|
+
next: page < count ? page + 1 : null,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computed,
|
|
3
|
+
onBeforeUnmount,
|
|
4
|
+
ref,
|
|
5
|
+
shallowRef,
|
|
6
|
+
watch,
|
|
7
|
+
type ExtractPropTypes,
|
|
8
|
+
type PropType,
|
|
9
|
+
} from '@vue/runtime-core'
|
|
10
|
+
import { dimensionProperty, flexContract, viewContract } from '../contracts.js'
|
|
11
|
+
import { vueProperty, vueProps } from './contracts.js'
|
|
12
|
+
import { useCachedActivity } from './cached-activity.js'
|
|
13
|
+
import {
|
|
14
|
+
choiceKey,
|
|
15
|
+
isChoiceValue,
|
|
16
|
+
validateChoices,
|
|
17
|
+
type ChoiceOption,
|
|
18
|
+
type ChoiceValue,
|
|
19
|
+
} from '../choice.js'
|
|
20
|
+
import {
|
|
21
|
+
comboboxHighlight,
|
|
22
|
+
comboboxMove,
|
|
23
|
+
comboboxSelected,
|
|
24
|
+
createComboboxSearch,
|
|
25
|
+
type ComboboxSearch,
|
|
26
|
+
type ComboboxSnapshot,
|
|
27
|
+
} from '../combobox.js'
|
|
28
|
+
|
|
29
|
+
export const comboboxProps = {
|
|
30
|
+
...vueProps(viewContract),
|
|
31
|
+
...vueProps(flexContract),
|
|
32
|
+
width: vueProperty(dimensionProperty),
|
|
33
|
+
modelValue: { type: [String, Number] as PropType<ChoiceValue | null>, default: null },
|
|
34
|
+
query: { type: String, required: true as const },
|
|
35
|
+
selectedOption: { type: Object as PropType<ChoiceOption | null>, default: null },
|
|
36
|
+
search: { type: Function as PropType<ComboboxSearch>, required: true as const },
|
|
37
|
+
initialResults: { type: Object as PropType<ComboboxSnapshot>, default: undefined },
|
|
38
|
+
debounce: { type: Number, default: 200 },
|
|
39
|
+
minQueryLength: { type: Number, default: 1 },
|
|
40
|
+
maxResults: { type: Number, default: 50 },
|
|
41
|
+
label: { type: String, required: true as const },
|
|
42
|
+
placeholder: { type: String, default: 'Search options' },
|
|
43
|
+
helperText: { type: String, default: '' },
|
|
44
|
+
error: { type: String, default: '' },
|
|
45
|
+
disabled: Boolean,
|
|
46
|
+
clearable: { type: Boolean, default: true },
|
|
47
|
+
clearLabel: { type: String, default: 'Clear selection' },
|
|
48
|
+
closeLabel: { type: String, default: 'Close results' },
|
|
49
|
+
loadingText: { type: String, default: 'Searching…' },
|
|
50
|
+
emptyText: { type: String, default: 'No results' },
|
|
51
|
+
searchErrorText: { type: String, default: 'Could not load results.' },
|
|
52
|
+
retryLabel: { type: String, default: 'Try again' },
|
|
53
|
+
idleText: { type: String, default: 'Type to search.' },
|
|
54
|
+
limitedText: { type: String, default: 'Refine your search to see more results.' },
|
|
55
|
+
}
|
|
56
|
+
export const comboboxEmits = {
|
|
57
|
+
'update:modelValue': (value: ChoiceValue | null) => value === null || isChoiceValue(value),
|
|
58
|
+
'update:query': (value: string) => typeof value === 'string',
|
|
59
|
+
select: (option: ChoiceOption | null) => option === null || isChoiceValue(option.value),
|
|
60
|
+
focus: () => true,
|
|
61
|
+
blur: () => true,
|
|
62
|
+
}
|
|
63
|
+
type Props = Readonly<ExtractPropTypes<typeof comboboxProps>>
|
|
64
|
+
type Emit = {
|
|
65
|
+
(event: 'update:modelValue', value: ChoiceValue | null): void
|
|
66
|
+
(event: 'update:query', value: string): void
|
|
67
|
+
(event: 'select', value: ChoiceOption | null): void
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @internal Shared request ownership and controlled selection for the two Vue renderers. */
|
|
71
|
+
export function useCombobox(props: Props, emit: Emit) {
|
|
72
|
+
watch(
|
|
73
|
+
() => props.label,
|
|
74
|
+
(label) => {
|
|
75
|
+
if (!label.trim()) throw new RangeError('Combobox needs a nonempty label.')
|
|
76
|
+
},
|
|
77
|
+
{ immediate: true },
|
|
78
|
+
)
|
|
79
|
+
const open = ref(false)
|
|
80
|
+
const revision = ref(0)
|
|
81
|
+
const composing = ref(false)
|
|
82
|
+
const active = ref<string>()
|
|
83
|
+
const retained = shallowRef<ChoiceOption | null>(props.selectedOption)
|
|
84
|
+
const pendingSelection = shallowRef<ChoiceOption>()
|
|
85
|
+
const activity = useCachedActivity()
|
|
86
|
+
const makeSearch = () =>
|
|
87
|
+
createComboboxSearch({
|
|
88
|
+
search: (query, context) => props.search(query, context),
|
|
89
|
+
debounce: props.debounce,
|
|
90
|
+
minQueryLength: props.minQueryLength,
|
|
91
|
+
maxResults: props.maxResults,
|
|
92
|
+
initialResults: props.initialResults,
|
|
93
|
+
onChange(value) {
|
|
94
|
+
state.value = value
|
|
95
|
+
active.value = comboboxHighlight(value.options, active.value)
|
|
96
|
+
},
|
|
97
|
+
})
|
|
98
|
+
let search = makeSearch()
|
|
99
|
+
const state = shallowRef(search.state)
|
|
100
|
+
const selected = computed(() =>
|
|
101
|
+
comboboxSelected(
|
|
102
|
+
props.modelValue,
|
|
103
|
+
state.value.options,
|
|
104
|
+
props.selectedOption?.value === props.modelValue
|
|
105
|
+
? props.selectedOption
|
|
106
|
+
: pendingSelection.value?.value === props.modelValue
|
|
107
|
+
? pendingSelection.value
|
|
108
|
+
: retained.value,
|
|
109
|
+
),
|
|
110
|
+
)
|
|
111
|
+
watch(
|
|
112
|
+
selected,
|
|
113
|
+
(option) => {
|
|
114
|
+
if (option) retained.value = option
|
|
115
|
+
},
|
|
116
|
+
{ immediate: true },
|
|
117
|
+
)
|
|
118
|
+
watch(
|
|
119
|
+
() => [props.modelValue, props.selectedOption] as const,
|
|
120
|
+
() => {
|
|
121
|
+
validateChoices(props.selectedOption ? [props.selectedOption] : [], props.modelValue)
|
|
122
|
+
if (props.modelValue === null) {
|
|
123
|
+
retained.value = null
|
|
124
|
+
pendingSelection.value = undefined
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
{ immediate: true, deep: true },
|
|
128
|
+
)
|
|
129
|
+
watch(
|
|
130
|
+
() => props.query,
|
|
131
|
+
() => {
|
|
132
|
+
if (open.value && !composing.value) search.request(props.query)
|
|
133
|
+
else search.cancel()
|
|
134
|
+
},
|
|
135
|
+
{ flush: 'sync' },
|
|
136
|
+
)
|
|
137
|
+
watch(
|
|
138
|
+
() => [props.search, props.debounce, props.minQueryLength, props.maxResults],
|
|
139
|
+
() => {
|
|
140
|
+
search.dispose()
|
|
141
|
+
search = makeSearch()
|
|
142
|
+
state.value = search.state
|
|
143
|
+
if (open.value && !composing.value) search.request(props.query)
|
|
144
|
+
},
|
|
145
|
+
)
|
|
146
|
+
watch(
|
|
147
|
+
() => props.disabled || !activity.value,
|
|
148
|
+
(blocked) => {
|
|
149
|
+
if (blocked) close()
|
|
150
|
+
},
|
|
151
|
+
)
|
|
152
|
+
onBeforeUnmount(() => search.dispose())
|
|
153
|
+
function show() {
|
|
154
|
+
if (props.disabled || !activity.value) return
|
|
155
|
+
open.value = true
|
|
156
|
+
if (!composing.value) search.request(props.query)
|
|
157
|
+
}
|
|
158
|
+
function close() {
|
|
159
|
+
open.value = false
|
|
160
|
+
active.value = undefined
|
|
161
|
+
composing.value = false
|
|
162
|
+
search.cancel()
|
|
163
|
+
}
|
|
164
|
+
function edit(query: string) {
|
|
165
|
+
if (props.disabled) return
|
|
166
|
+
revision.value++
|
|
167
|
+
// Browsers may send a final input event after compositionend. It is the same
|
|
168
|
+
// accepted query, not a second request (including when debounce is zero).
|
|
169
|
+
if (
|
|
170
|
+
query === props.query &&
|
|
171
|
+
!composing.value &&
|
|
172
|
+
open.value &&
|
|
173
|
+
state.value.query === query &&
|
|
174
|
+
state.value.status !== 'idle'
|
|
175
|
+
)
|
|
176
|
+
return
|
|
177
|
+
search.cancel()
|
|
178
|
+
active.value = undefined
|
|
179
|
+
open.value = true
|
|
180
|
+
emit('update:query', query)
|
|
181
|
+
if (props.query === query && !composing.value) search.request(query)
|
|
182
|
+
// Only the accepted controlled query starts a request (the synchronous watch owns it).
|
|
183
|
+
}
|
|
184
|
+
function choose(option: ChoiceOption) {
|
|
185
|
+
const current = state.value.options.find((item) => item.value === option.value)
|
|
186
|
+
if (!open.value || props.disabled || composing.value || !current || current.disabled) return
|
|
187
|
+
pendingSelection.value = current
|
|
188
|
+
if (props.modelValue !== current.value) emit('update:modelValue', current.value)
|
|
189
|
+
emit('update:query', current.label)
|
|
190
|
+
emit('select', current)
|
|
191
|
+
close()
|
|
192
|
+
}
|
|
193
|
+
function clear() {
|
|
194
|
+
if (props.disabled) return
|
|
195
|
+
close()
|
|
196
|
+
emit('update:modelValue', null)
|
|
197
|
+
emit('update:query', '')
|
|
198
|
+
emit('select', null)
|
|
199
|
+
}
|
|
200
|
+
function cancel() {
|
|
201
|
+
close()
|
|
202
|
+
emit('update:query', selected.value?.label ?? '')
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
open,
|
|
206
|
+
composing,
|
|
207
|
+
active,
|
|
208
|
+
state,
|
|
209
|
+
selected,
|
|
210
|
+
revision,
|
|
211
|
+
show,
|
|
212
|
+
close,
|
|
213
|
+
edit,
|
|
214
|
+
choose,
|
|
215
|
+
clear,
|
|
216
|
+
cancel,
|
|
217
|
+
startComposition() {
|
|
218
|
+
composing.value = true
|
|
219
|
+
search.cancel()
|
|
220
|
+
active.value = undefined
|
|
221
|
+
},
|
|
222
|
+
endComposition(query: string) {
|
|
223
|
+
composing.value = false
|
|
224
|
+
edit(query)
|
|
225
|
+
},
|
|
226
|
+
move(direction: 1 | -1) {
|
|
227
|
+
if (!open.value) show()
|
|
228
|
+
active.value = comboboxMove(state.value.options, active.value, direction)
|
|
229
|
+
},
|
|
230
|
+
commit() {
|
|
231
|
+
const option = state.value.options.find((item) => choiceKey(item.value) === active.value)
|
|
232
|
+
if (option) choose(option)
|
|
233
|
+
},
|
|
234
|
+
retry() {
|
|
235
|
+
if (!props.disabled && open.value && !composing.value) search.request(props.query, true)
|
|
236
|
+
},
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ExtractPublicPropTypes } from '@vue/runtime-core'
|
|
2
|
+
import type { SheetName } from '../sheet.js'
|
|
3
|
+
import { componentProps as foundationProps } from './props.js'
|
|
4
|
+
import { datePickerProps, dateRangePickerProps } from './date-props.js'
|
|
5
|
+
import { comboboxProps } from './combobox.js'
|
|
6
|
+
import { filePickerProps } from './files.js'
|
|
7
|
+
import { formGroupProps, formErrorsProps, stepsProps, paginationProps } from './workflow-props.js'
|
|
8
|
+
|
|
9
|
+
/** Complete registry, kept separate from the primitives used to compose controls. */
|
|
10
|
+
export const componentProps = {
|
|
11
|
+
...foundationProps,
|
|
12
|
+
MDatePicker: datePickerProps,
|
|
13
|
+
MDateRangePicker: dateRangePickerProps,
|
|
14
|
+
MCombobox: comboboxProps,
|
|
15
|
+
MFilePicker: filePickerProps,
|
|
16
|
+
MFormGroup: formGroupProps,
|
|
17
|
+
MFormErrors: formErrorsProps,
|
|
18
|
+
MSteps: stepsProps,
|
|
19
|
+
MPagination: paginationProps,
|
|
20
|
+
}
|
|
21
|
+
export type ComponentProps = {
|
|
22
|
+
[Name in keyof typeof componentProps]: ExtractPublicPropTypes<(typeof componentProps)[Name]> &
|
|
23
|
+
(Name extends 'MSheet' ? SheetName : unknown)
|
|
24
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { PropType } from '@vue/runtime-core'
|
|
2
|
+
import type { Property, PropertyValue } from '../contracts.js'
|
|
3
|
+
|
|
4
|
+
type VueProperty<P extends Property> = { type: PropType<PropertyValue<P>> } & (P extends {
|
|
5
|
+
default: infer D
|
|
6
|
+
}
|
|
7
|
+
? { default: D extends undefined ? undefined : PropertyValue<P> }
|
|
8
|
+
: unknown) &
|
|
9
|
+
(P extends { required: true } ? { required: true } : unknown)
|
|
10
|
+
|
|
11
|
+
const constructors = { string: String, number: Number, boolean: Boolean, object: Object }
|
|
12
|
+
|
|
13
|
+
/** Vue owns coercion and warnings; portable metadata owns names, defaults and allowed values. */
|
|
14
|
+
export function vueProperty<P extends Property>(definition: P): VueProperty<P> {
|
|
15
|
+
const kinds = Array.isArray(definition.kind) ? definition.kind : [definition.kind]
|
|
16
|
+
const types = kinds.map((kind: keyof typeof constructors) => constructors[kind])
|
|
17
|
+
return {
|
|
18
|
+
type: types.length === 1 ? types[0] : types,
|
|
19
|
+
...('default' in definition ? { default: definition.default } : {}),
|
|
20
|
+
...(definition.required ? { required: true } : {}),
|
|
21
|
+
...(definition.values
|
|
22
|
+
? { validator: (value: unknown) => definition.values!.includes(value) }
|
|
23
|
+
: {}),
|
|
24
|
+
} as VueProperty<P>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function vueProps<P extends Record<string, Property>>(properties: P) {
|
|
28
|
+
return Object.fromEntries(
|
|
29
|
+
Object.entries(properties).map(([key, value]) => [key, vueProperty(value)]),
|
|
30
|
+
) as {
|
|
31
|
+
[K in keyof P]: VueProperty<P[K]>
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { computed, watch, type ExtractPropTypes } from '@vue/runtime-core'
|
|
2
|
+
import {
|
|
3
|
+
addCivilDays,
|
|
4
|
+
addCivilMonths,
|
|
5
|
+
calendarMonth,
|
|
6
|
+
civilWeekday,
|
|
7
|
+
dateAvailability,
|
|
8
|
+
dateLocales,
|
|
9
|
+
parseCivilDate,
|
|
10
|
+
type CivilDate,
|
|
11
|
+
} from '../date.js'
|
|
12
|
+
import { dateCalendarProps } from './date-props.js'
|
|
13
|
+
|
|
14
|
+
/** @internal Calendar arithmetic and live bounds shared by Web and native presentations. */
|
|
15
|
+
export function useDateCalendar(
|
|
16
|
+
props: ExtractPropTypes<typeof dateCalendarProps>,
|
|
17
|
+
change: (value: CivilDate) => void,
|
|
18
|
+
) {
|
|
19
|
+
const words = computed(() => dateLocales[props.locale])
|
|
20
|
+
const firstDay = computed(() => props.firstDayOfWeek ?? words.value.firstDayOfWeek)
|
|
21
|
+
const cells = computed(() => calendarMonth(props.activeDate, firstDay.value))
|
|
22
|
+
const title = computed(() => {
|
|
23
|
+
const parts = parseCivilDate(props.activeDate)!
|
|
24
|
+
return `${words.value.months[parts.month - 1]} ${String(parts.year).padStart(4, '0')}`
|
|
25
|
+
})
|
|
26
|
+
const weekdays = computed(() =>
|
|
27
|
+
Array.from({ length: 7 }, (_, index) => (index + firstDay.value) % 7),
|
|
28
|
+
)
|
|
29
|
+
const available = (date: CivilDate) => dateAvailability(date, props) === null
|
|
30
|
+
function clamp(value: CivilDate) {
|
|
31
|
+
if (props.min && value < props.min) return props.min
|
|
32
|
+
if (props.max && value > props.max) return props.max
|
|
33
|
+
return value
|
|
34
|
+
}
|
|
35
|
+
function move(value: CivilDate) {
|
|
36
|
+
change(clamp(value))
|
|
37
|
+
}
|
|
38
|
+
function canPage(amount: number) {
|
|
39
|
+
const next = addCivilMonths(props.activeDate, amount)
|
|
40
|
+
return (
|
|
41
|
+
next.slice(0, 7) !== props.activeDate.slice(0, 7) &&
|
|
42
|
+
(!props.min || next.slice(0, 7) >= props.min.slice(0, 7)) &&
|
|
43
|
+
(!props.max || next.slice(0, 7) <= props.max.slice(0, 7))
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
function page(amount: number) {
|
|
47
|
+
if (canPage(amount)) move(addCivilMonths(props.activeDate, amount))
|
|
48
|
+
}
|
|
49
|
+
function key(key: string, shift = false, rtl = false): boolean {
|
|
50
|
+
const active = props.activeDate
|
|
51
|
+
if (key === 'ArrowLeft') move(addCivilDays(active, rtl ? 1 : -1))
|
|
52
|
+
else if (key === 'ArrowRight') move(addCivilDays(active, rtl ? -1 : 1))
|
|
53
|
+
else if (key === 'ArrowUp') move(addCivilDays(active, -7))
|
|
54
|
+
else if (key === 'ArrowDown') move(addCivilDays(active, 7))
|
|
55
|
+
else if (key === 'Home')
|
|
56
|
+
move(addCivilDays(active, -((civilWeekday(active) - firstDay.value + 7) % 7)))
|
|
57
|
+
else if (key === 'End')
|
|
58
|
+
move(addCivilDays(active, 6 - ((civilWeekday(active) - firstDay.value + 7) % 7)))
|
|
59
|
+
else if (key === 'PageUp') move(addCivilMonths(active, shift ? -12 : -1))
|
|
60
|
+
else if (key === 'PageDown') move(addCivilMonths(active, shift ? 12 : 1))
|
|
61
|
+
else return false
|
|
62
|
+
return true
|
|
63
|
+
}
|
|
64
|
+
watch(
|
|
65
|
+
() => [props.min, props.max, props.activeDate],
|
|
66
|
+
() => {
|
|
67
|
+
const value = clamp(props.activeDate)
|
|
68
|
+
if (value !== props.activeDate) change(value)
|
|
69
|
+
},
|
|
70
|
+
)
|
|
71
|
+
const inRange = (date: CivilDate) =>
|
|
72
|
+
Boolean(
|
|
73
|
+
props.rangeStart && props.rangeEnd && date >= props.rangeStart && date <= props.rangeEnd,
|
|
74
|
+
)
|
|
75
|
+
return { words, title, firstDay, cells, weekdays, available, move, canPage, page, key, inRange }
|
|
76
|
+
}
|