Files
2026-01-05 12:47:14 +08:00

55 lines
1.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { computed } from 'vue'
import type { ExtractPropTypes, Ref } from 'vue'
import { useInject } from '../_hooks'
import { commonProps, makeObjectProp, makeStringProp } from '../_utils'
import type { ErrorMessage, FormLabelPosition, FormRules, FormStarPosition } from './types'
export const FORM_KEY = Symbol('Form')
export const formProps = {
...commonProps,
/**
* @description 表单数据对象(使用表单校验时_必填_)
*/
modelValue: makeObjectProp({}),
/**
* @description 统一配置每个 `FormItem` 的 `rules`
*/
rules: makeObjectProp<FormRules>({}),
/**
* @description 禁用表单下的所有数据录入组件
*/
disabled: Boolean,
/**
* @description 表单项 label 的位置
*/
labelPosition: makeStringProp<FormLabelPosition>('left'),
/**
* @description 必填表单项 label 的红色星标位置
*/
starPosition: makeStringProp<FormStarPosition>('left'),
}
export type FormProps = ExtractPropTypes<typeof formProps>
export const formEmits = {
validate: (msg: ErrorMessage) => msg instanceof Object,
}
export type FormEmits = typeof formEmits
export function useFormDisabled(disabled: Ref<boolean | undefined>) {
const { parent } = useInject<{ props: { disabled: boolean } }>(FORM_KEY)
return computed(() => {
if (disabled.value != null)
return disabled.value
if (parent?.props.disabled != null)
return parent.props.disabled
return false
})
}