2020-10-31 15:20:38 +08:00
|
|
|
import { ref, computed, inject, WritableComputedRef } from 'vue'
|
2020-11-10 14:24:09 +08:00
|
|
|
import { elFormKey, elFormItemKey } from '@element-plus/form'
|
2020-10-31 15:20:38 +08:00
|
|
|
import { useGlobalConfig } from '@element-plus/utils/util'
|
|
|
|
import radioGroupKey from './token'
|
|
|
|
|
|
|
|
import type { ComputedRef } from 'vue'
|
2020-11-10 14:24:09 +08:00
|
|
|
import type { ElFormContext, ElFormItemContext } from '@element-plus/form'
|
2020-10-31 15:20:38 +08:00
|
|
|
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)
|
2020-08-09 15:44:11 +08:00
|
|
|
const focus = ref(false)
|
2020-10-31 15:20:38 +08:00
|
|
|
const isGroup = computed(() => radioGroup?.name === 'ElRadioGroup')
|
2020-11-01 14:18:56 +08:00
|
|
|
const elFormItemSize = computed(() => elFormItem.size || ELEMENT.size)
|
2020-10-31 15:20:38 +08:00
|
|
|
|
2020-08-09 15:44:11 +08:00
|
|
|
return {
|
|
|
|
isGroup,
|
|
|
|
focus,
|
2020-10-31 15:20:38 +08:00
|
|
|
radioGroup,
|
2020-08-09 15:44:11 +08:00
|
|
|
elForm,
|
|
|
|
ELEMENT,
|
2020-10-31 15:20:38 +08:00
|
|
|
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,
|
2020-08-09 15:44:11 +08:00
|
|
|
}
|
2020-10-31 15:20:38 +08:00
|
|
|
|
2020-08-09 15:44:11 +08:00
|
|
|
}
|