index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031
  1. import { computed, inject, ref, unref } from 'vue'
  2. import { formContextKey, formItemContextKey } from '@element-plus/tokens'
  3. import { buildProp } from '@kankan-components/utils'
  4. // import { componentSizes } from '@kankan-components/constants'
  5. import { useProp } from '../use-prop'
  6. import { useGlobalConfig } from '../use-global-config'
  7. import type { ComponentSize } from '@element-plus/constants'
  8. import type { MaybeRef } from '@vueuse/core'
  9. export const useSizeProp = buildProp({
  10. type: String,
  11. values: componentSizes,
  12. required: false,
  13. } as const)
  14. export const useSize = (fallback?: MaybeRef<ComponentSize | undefined>, ignore: Partial<Record<'prop' | 'form' | 'formItem' | 'global', boolean>> = {}) => {
  15. const emptyRef = ref(undefined)
  16. const size = ignore.prop ? emptyRef : useProp<ComponentSize>('size')
  17. const globalConfig = ignore.global ? emptyRef : useGlobalConfig('size')
  18. const form = ignore.form ? { size: undefined } : inject(formContextKey, undefined)
  19. const formItem = ignore.formItem ? { size: undefined } : inject(formItemContextKey, undefined)
  20. return computed((): ComponentSize => size.value || unref(fallback) || formItem?.size || form?.size || globalConfig.value || '')
  21. }
  22. export const useDisabled = (fallback?: MaybeRef<boolean | undefined>) => {
  23. const disabled = useProp<boolean>('disabled')
  24. const form = inject(formContextKey, undefined)
  25. return computed(() => disabled.value || unref(fallback) || form?.disabled || false)
  26. }