refactor(use-form-item): add mergedDisabled option

This commit is contained in:
07akioni 2021-07-30 23:59:41 +08:00
parent a8bfba0356
commit 1256df68cf
3 changed files with 61 additions and 60 deletions

View File

@ -28,6 +28,7 @@ export const formItemInjectionKey: InjectionKey<FormItemInjection> =
interface UseFormItemOptions<T> { interface UseFormItemOptions<T> {
defaultSize?: FormItemSize defaultSize?: FormItemSize
mergedSize?: (formItem: FormItemInjection | null) => T mergedSize?: (formItem: FormItemInjection | null) => T
mergedDisabled?: (formItem: FormItemInjection | null) => boolean
} }
interface UseFormItemProps<T> { interface UseFormItemProps<T> {
@ -46,7 +47,11 @@ export interface UseFormItem<T> {
export default function useFormItem<T extends AllowedSize = FormItemSize> ( export default function useFormItem<T extends AllowedSize = FormItemSize> (
props: UseFormItemProps<T>, props: UseFormItemProps<T>,
{ defaultSize = 'medium', mergedSize }: UseFormItemOptions<T> = {} {
defaultSize = 'medium',
mergedSize,
mergedDisabled
}: UseFormItemOptions<T> = {}
): UseFormItem<T> { ): UseFormItem<T> {
const NFormItem = inject(formItemInjectionKey, null) const NFormItem = inject(formItemInjectionKey, null)
provide(formItemInjectionKey, null) provide(formItemInjectionKey, null)
@ -65,7 +70,10 @@ export default function useFormItem<T extends AllowedSize = FormItemSize> (
return defaultSize as T return defaultSize as T
} }
) )
const mergedDisabledRef = computed(() => { const mergedDisabledRef = computed(
mergedDisabled
? () => mergedDisabled(NFormItem)
: () => {
const { disabled } = props const { disabled } = props
if (disabled !== undefined) { if (disabled !== undefined) {
return disabled return disabled
@ -74,7 +82,8 @@ export default function useFormItem<T extends AllowedSize = FormItemSize> (
return NFormItem.disabled.value return NFormItem.disabled.value
} }
return false return false
}) }
)
onBeforeUnmount(() => { onBeforeUnmount(() => {
if (NFormItem) { if (NFormItem) {
NFormItem.restoreValidation() NFormItem.restoreValidation()

View File

@ -93,30 +93,10 @@ export default defineComponent({
if (mergedSize !== undefined) return mergedSize.value if (mergedSize !== undefined) return mergedSize.value
} }
return 'medium' return 'medium'
} },
}) mergedDisabled (NFormItem) {
const { mergedSizeRef, mergedDisabledRef: mergedFormItemDisabledRef } = const { disabled } = props
formItem if (disabled !== undefined) return disabled
const NCheckboxGroup = inject(checkboxGroupInjectionKey, null)
const uncontrolledCheckedRef = ref(props.defaultChecked)
const controlledCheckedRef = toRef(props, 'checked')
const mergedCheckedRef = useMergedState(
controlledCheckedRef,
uncontrolledCheckedRef
)
const renderedCheckedRef = useMemo(() => {
if (NCheckboxGroup) {
const groupValueSet = NCheckboxGroup.valueSetRef.value
if (groupValueSet && props.value !== undefined) {
return groupValueSet.has(props.value)
}
return false
} else {
return mergedCheckedRef.value
}
})
const mergedDisabledRef = useMemo(() => {
if (props.disabled) return true
if (NCheckboxGroup) { if (NCheckboxGroup) {
if (NCheckboxGroup.disabledRef.value) return true if (NCheckboxGroup.disabledRef.value) return true
const { const {
@ -141,10 +121,30 @@ export default defineComponent({
return true return true
} }
} }
if (formItem) { if (NFormItem) {
return mergedFormItemDisabledRef.value return NFormItem.disabled.value
} }
return false return false
}
})
const { mergedDisabledRef, mergedSizeRef } = formItem
const NCheckboxGroup = inject(checkboxGroupInjectionKey, null)
const uncontrolledCheckedRef = ref(props.defaultChecked)
const controlledCheckedRef = toRef(props, 'checked')
const mergedCheckedRef = useMergedState(
controlledCheckedRef,
uncontrolledCheckedRef
)
const renderedCheckedRef = useMemo(() => {
if (NCheckboxGroup) {
const groupValueSet = NCheckboxGroup.valueSetRef.value
if (groupValueSet && props.value !== undefined) {
return groupValueSet.has(props.value)
}
return false
} else {
return mergedCheckedRef.value
}
}) })
const themeRef = useTheme( const themeRef = useTheme(
'Checkbox', 'Checkbox',

View File

@ -23,10 +23,7 @@ const radioProps = {
type: Boolean as PropType<boolean | undefined>, type: Boolean as PropType<boolean | undefined>,
default: undefined default: undefined
}, },
defaultChecked: { defaultChecked: Boolean,
type: Boolean,
default: false
},
disabled: { disabled: {
type: Boolean as PropType<boolean | undefined>, type: Boolean as PropType<boolean | undefined>,
default: undefined default: undefined
@ -96,10 +93,15 @@ function setup (props: ExtractPropTypes<typeof radioProps>): UseRadio {
return NFormItem.mergedSize.value return NFormItem.mergedSize.value
} }
return 'medium' return 'medium'
},
mergedDisabled (NFormItem) {
if (props.disabled) return true
if (NRadioGroup?.disabledRef.value) return true
if (NFormItem?.disabled.value) return true
return false
} }
}) })
const { mergedSizeRef, mergedDisabledRef: mergedFormItemDisabledRef } = const { mergedSizeRef, mergedDisabledRef } = formItem
formItem
const inputRef = ref<HTMLElement | null>(null) const inputRef = ref<HTMLElement | null>(null)
const labelRef = ref<HTMLElement | null>(null) const labelRef = ref<HTMLElement | null>(null)
const NRadioGroup = inject(radioGroupInjectionKey, null) const NRadioGroup = inject(radioGroupInjectionKey, null)
@ -118,16 +120,6 @@ function setup (props: ExtractPropTypes<typeof radioProps>): UseRadio {
if (name !== undefined) return name if (name !== undefined) return name
if (NRadioGroup) return NRadioGroup.nameRef.value if (NRadioGroup) return NRadioGroup.nameRef.value
}) })
const mergedDisabledRef = useMemo(() => {
if (props.disabled) return true
if (NRadioGroup) {
return NRadioGroup.disabledRef.value
}
if (formItem) {
return mergedFormItemDisabledRef.value
}
return false
})
const focusRef = ref(false) const focusRef = ref(false)
function doUpdateChecked (): void { function doUpdateChecked (): void {
if (NRadioGroup) { if (NRadioGroup) {