Skip to content

Commit

Permalink
fix(comp: radio): incomplete consideration of controlled mode
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzaijiang committed Feb 10, 2022
1 parent 3001bbd commit cce9e9e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/components/button/style/mixin.less
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
border-color: @border;

> a:only-child {
color: currentcolor;
color: currentColor;

&::after {
position: absolute;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/icon/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
> svg {
width: 1em;
height: 1em;
fill: currentcolor;
fill: currentColor;
}

&-loading,
Expand Down
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

0 comments on commit cce9e9e

Please sign in to comment.