Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(comp: radio): incomplete consideration of controlled mode #744

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/components/radio/demo/Group.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<IxRadioGroup v-model:value="value" @Change="onChang">
<IxRadioGroup v-model:value="value" @Change="onChange">
<IxRadio value="a">A</IxRadio>
<IxRadio value="b">B</IxRadio>
<IxRadio value="c">C</IxRadio>
Expand All @@ -11,5 +11,5 @@
import { ref } from 'vue'

const value = ref('a')
const onChang = console.log
const onChange = console.log
</script>
29 changes: 21 additions & 8 deletions packages/components/radio/src/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { RadioGroupContext } from './token'
import type { RadioProps } from './types'
import type { ComputedRef, StyleValue } from 'vue'
import type { ComputedRef, Ref, StyleValue } from 'vue'

import { computed, defineComponent, inject, normalizeClass, ref } from 'vue'

Expand Down Expand Up @@ -37,7 +37,11 @@ export default defineComponent({
const isButtoned = computed(() => props.buttoned ?? radioGroup?.props.buttoned)
const size = computed(() => props.size ?? radioGroup?.props.size ?? formContext?.size.value ?? config.size)
const mode = computed(() => props.mode ?? radioGroup?.props.mode ?? 'default')
const { isChecked, isDisabled, isFocused, handleChange, handleBlur, handleFocus } = useRadio(props, radioGroup)
const { isChecked, isDisabled, isFocused, handleChange, handleBlur, handleFocus } = useRadio(
props,
radioGroup,
elementRef,
)
const classes = computed(() => {
const buttoned = isButtoned.value
const prefixCls = mergedPrefixCls.value
Expand Down Expand Up @@ -92,7 +96,11 @@ export default defineComponent({
},
})

const useRadio = (props: RadioProps, radioGroup: RadioGroupContext | null) => {
const useRadio = (
props: RadioProps,
radioGroup: RadioGroupContext | null,
elementRef: Ref<HTMLInputElement | undefined>,
) => {
let isChecked: ComputedRef<boolean>
let isDisabled: ComputedRef<boolean>
const isFocused = ref(false)
Expand All @@ -114,10 +122,12 @@ const useRadio = (props: RadioProps, radioGroup: RadioGroupContext | null) => {
accessor.markAsBlurred()
}
handleChange = (evt: Event) => {
const checked = (evt.target as HTMLInputElement).checked
if (checked) {
if (elementRef.value) {
const checked = (evt.target as HTMLInputElement).checked
const value = props.value
accessor.setValue(value)
// 为了保持受控模式下保持原生input状态和数据一致
elementRef.value.checked = false
callEmit(props.onChange, checked)
callEmit(groupProps.onChange, value)
}
Expand All @@ -132,9 +142,12 @@ const useRadio = (props: RadioProps, radioGroup: RadioGroupContext | null) => {
accessor.markAsBlurred()
}
handleChange = (evt: Event) => {
const checked = (evt.target as HTMLInputElement).checked
accessor.setValue(checked)
callEmit(props.onChange, checked)
if (elementRef.value) {
const checked = (evt.target as HTMLInputElement).checked
accessor.setValue(checked)
elementRef.value.checked = false
callEmit(props.onChange, checked)
}
}
}

Expand Down