2021-12-10 17:21:01 +08:00
|
|
|
import { ref, unref, inject, computed } from 'vue'
|
|
|
|
import { elFormItemKey, elFormKey } from '@element-plus/tokens'
|
|
|
|
import { buildProp, componentSize } from '@element-plus/utils/props'
|
2021-12-12 13:39:54 +08:00
|
|
|
import { useProp } from '../use-prop'
|
|
|
|
import { useGlobalConfig } from '../use-global-config'
|
2021-12-10 17:21:01 +08:00
|
|
|
import type { ComponentSize } from '@element-plus/utils/types'
|
|
|
|
import type { MaybeRef } from '@vueuse/core'
|
|
|
|
|
|
|
|
export const useSizeProp = buildProp({
|
|
|
|
type: String,
|
|
|
|
values: ['', ...componentSize],
|
|
|
|
default: '',
|
|
|
|
} as const)
|
|
|
|
|
|
|
|
export const useSize = (
|
2021-12-30 19:31:35 +08:00
|
|
|
fallback?: MaybeRef<ComponentSize | undefined>,
|
2021-12-10 17:21:01 +08:00
|
|
|
ignore: Partial<Record<'prop' | 'form' | 'formItem' | 'global', boolean>> = {}
|
|
|
|
) => {
|
|
|
|
const emptyRef = ref(undefined)
|
|
|
|
|
|
|
|
const size = ignore.prop ? emptyRef : useProp<ComponentSize>('size')
|
|
|
|
const globalConfig = ignore.global ? emptyRef : useGlobalConfig('size')
|
|
|
|
const form = ignore.form ? { size: undefined } : inject(elFormKey, undefined)
|
|
|
|
const formItem = ignore.formItem
|
|
|
|
? { size: undefined }
|
|
|
|
: inject(elFormItemKey, undefined)
|
|
|
|
|
|
|
|
return computed(
|
2021-12-30 19:31:35 +08:00
|
|
|
(): ComponentSize =>
|
2021-12-10 17:21:01 +08:00
|
|
|
size.value ||
|
|
|
|
unref(fallback) ||
|
|
|
|
formItem?.size ||
|
|
|
|
form?.size ||
|
|
|
|
globalConfig.value ||
|
2021-12-26 15:08:04 +08:00
|
|
|
'default'
|
2021-12-10 17:21:01 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useDisabled = (fallback?: MaybeRef<boolean | undefined>) => {
|
|
|
|
const disabled = useProp<boolean>('disabled')
|
|
|
|
const form = inject(elFormKey, undefined)
|
|
|
|
return computed(
|
|
|
|
() => disabled.value || unref(fallback) || form?.disabled || false
|
|
|
|
)
|
|
|
|
}
|