1- import React from 'react' ;
1+ import React , { use } from 'react' ;
22import { Flex , Segmented } from 'antd' ;
33
44import useLocale from '../../hooks/useLocale' ;
5- import SemanticPreview from './SemanticPreview' ;
5+ import SemanticPreview , { SemanticActiveContext } from './SemanticPreview' ;
66
77export const locales = {
88 cn : {
@@ -67,14 +67,18 @@ const Block: React.FC<BlockProps> = ({
6767 ...props
6868} ) => {
6969 const divRef = React . useRef < HTMLDivElement > ( null ) ;
70+ const activeSemantic = use ( SemanticActiveContext ) ;
7071 // 多选模式下,优先使用 multipleProps 中的 defaultValue
7172 const multipleDefaultValue = ( multipleProps as any ) ?. defaultValue ;
7273 const initialValue = mode === 'single' ? defaultValue : multipleDefaultValue ;
7374 const [ value , setValue ] = React . useState ( initialValue ) ;
75+ const [ valueMode , setValueMode ] = React . useState ( mode ) ;
7476
75- React . useEffect ( ( ) => {
76- setValue ( mode === 'single' ? defaultValue : multipleDefaultValue ) ;
77- } , [ mode , defaultValue , multipleDefaultValue ] ) ;
77+ // 单选和多选的取值结构不同,切换模式时同步重置,避免把上一个模式的值渲染出去
78+ if ( valueMode !== mode ) {
79+ setValueMode ( mode ) ;
80+ setValue ( initialValue ) ;
81+ }
7882
7983 return (
8084 < Flex
@@ -98,12 +102,12 @@ const Block: React.FC<BlockProps> = ({
98102 { ...props }
99103 open
100104 placement = "bottomLeft"
101- value = { value }
105+ value = { activeSemantic === 'placeholder' ? null : value }
102106 onChange = { setValue }
103107 getPopupContainer = { ( ) => divRef . current }
104108 options = { options }
105109 { ...( mode === 'multiple' ? multipleProps : { } ) }
106- styles = { { popup : { zIndex : 1 } } }
110+ styles = { { popup : { zIndex : 1 } , clear : { opacity : 1 } , suffix : { marginInlineEnd : 24 } } }
107111 maxTagCount = { process . env . NODE_ENV === 'test' ? 1 : 'responsive' }
108112 placeholder = "Please select"
109113 allowClear
0 commit comments