Files
element-plus/packages/components/radio/src/useRadio.ts
三咲智子 55348b30b6 style: use prettier (#3228)
* style: use prettier

* style: just prettier format, no code changes

* style: eslint fix
object-shorthand, prefer-const

* style: fix no-void

* style: no-console
2021-09-04 19:29:28 +08:00

62 lines
1.6 KiB
TypeScript

import { ref, computed, inject } from 'vue'
import { elFormKey, elFormItemKey } from '@element-plus/tokens'
import { useGlobalConfig } from '@element-plus/utils/util'
import radioGroupKey from './token'
import type { ComputedRef, WritableComputedRef } from 'vue'
import type { ElFormContext, ElFormItemContext } from '@element-plus/tokens'
import type { RadioGroupContext } from './token'
export const useRadio = () => {
const ELEMENT = useGlobalConfig()
const elForm = inject(elFormKey, {} as ElFormContext)
const elFormItem = inject(elFormItemKey, {} as ElFormItemContext)
const radioGroup = inject(radioGroupKey, {} as RadioGroupContext)
const focus = ref(false)
const isGroup = computed(() => radioGroup?.name === 'ElRadioGroup')
const elFormItemSize = computed(() => elFormItem.size || ELEMENT.size)
return {
isGroup,
focus,
radioGroup,
elForm,
ELEMENT,
elFormItemSize,
}
}
interface IUseRadioAttrsProps {
disabled?: boolean
label: string | number | boolean
}
interface IUseRadioAttrsState {
isGroup: ComputedRef<boolean>
radioGroup: RadioGroupContext
elForm: ElFormContext
model: WritableComputedRef<string | number | boolean>
}
export const useRadioAttrs = (
props: IUseRadioAttrsProps,
{ isGroup, radioGroup, elForm, model }: IUseRadioAttrsState
) => {
const isDisabled = computed(() => {
return isGroup.value
? radioGroup.disabled || props.disabled || elForm.disabled
: props.disabled || elForm.disabled
})
const tabIndex = computed(() => {
return isDisabled.value || (isGroup.value && model.value !== props.label)
? -1
: 0
})
return {
isDisabled,
tabIndex,
}
}