fix(radio-group): doesn't trigger form item validation

This commit is contained in:
07akioni 2021-06-15 18:36:29 +08:00
parent 1e13babca3
commit 7fc659de7e
3 changed files with 41 additions and 3 deletions

View File

@ -1,5 +1,11 @@
# CHANGELOG # CHANGELOG
## Pending
### Fixes
- Fix `n-radio-group` doesn't trigger form item validation.
## 2.11.11 (2021-06-15) ## 2.11.11 (2021-06-15)
### Fixes ### Fixes

View File

@ -1,5 +1,11 @@
# CHANGELOG # CHANGELOG
## Pending
### Fixes
- 修复 `n-radio-group` 不触发 form-item 验证
## 2.11.11 (2021-06-15) ## 2.11.11 (2021-06-15)
### Fixes ### Fixes

View File

@ -139,7 +139,14 @@ export default defineComponent({
name: 'RadioGroup', name: 'RadioGroup',
props: radioGroupProps, props: radioGroupProps,
setup (props) { setup (props) {
const formItem = useFormItem(props) const selfElRef = ref<HTMLDivElement | null>(null)
const {
mergedSizeRef,
nTriggerFormChange,
nTriggerFormInput,
nTriggerFormBlur,
nTriggerFormFocus
} = useFormItem(props)
const { mergedClsPrefixRef } = useConfig(props) const { mergedClsPrefixRef } = useConfig(props)
const themeRef = useTheme( const themeRef = useTheme(
'Radio', 'Radio',
@ -149,7 +156,6 @@ export default defineComponent({
props, props,
mergedClsPrefixRef mergedClsPrefixRef
) )
const { mergedSizeRef } = formItem
const uncontrolledValueRef = ref(props.defaultValue) const uncontrolledValueRef = ref(props.defaultValue)
const controlledValueRef = toRef(props, 'value') const controlledValueRef = toRef(props, 'value')
const mergedValueRef = useMergedState( const mergedValueRef = useMergedState(
@ -172,6 +178,20 @@ export default defineComponent({
call(_onUpdateValue, value) call(_onUpdateValue, value)
} }
uncontrolledValueRef.value = value uncontrolledValueRef.value = value
nTriggerFormChange()
nTriggerFormInput()
}
function handleFocusin (e: FocusEvent): void {
const { value: selfEl } = selfElRef
if (!selfEl) return
if (selfEl.contains(e.relatedTarget as HTMLElement | null)) return
nTriggerFormFocus()
}
function handleFocusout (e: FocusEvent): void {
const { value: selfEl } = selfElRef
if (!selfEl) return
if (selfEl.contains(e.relatedTarget as HTMLElement | null)) return
nTriggerFormBlur()
} }
provide(radioGroupInjectionKey, { provide(radioGroupInjectionKey, {
mergedClsPrefixRef, mergedClsPrefixRef,
@ -182,8 +202,11 @@ export default defineComponent({
doUpdateValue doUpdateValue
}) })
return { return {
selfElRef,
mergedClsPrefix: mergedClsPrefixRef, mergedClsPrefix: mergedClsPrefixRef,
mergedValue: mergedValueRef, mergedValue: mergedValueRef,
handleFocusout,
handleFocusin,
cssVars: computed(() => { cssVars: computed(() => {
const { value: size } = mergedSizeRef const { value: size } = mergedSizeRef
const { const {
@ -224,7 +247,7 @@ export default defineComponent({
} }
}, },
render () { render () {
const { mergedValue, mergedClsPrefix } = this const { mergedValue, mergedClsPrefix, handleFocusin, handleFocusout } = this
const { children, isButtonGroup } = mapSlot( const { children, isButtonGroup } = mapSlot(
flatten(getSlot(this)), flatten(getSlot(this)),
mergedValue, mergedValue,
@ -232,6 +255,9 @@ export default defineComponent({
) )
return ( return (
<div <div
onFocusin={handleFocusin}
onFocusout={handleFocusout}
ref="selfElRef"
class={[ class={[
`${mergedClsPrefix}-radio-group`, `${mergedClsPrefix}-radio-group`,
isButtonGroup && `${mergedClsPrefix}-radio-group--button-group` isButtonGroup && `${mergedClsPrefix}-radio-group--button-group`