Skip to content

Commit 4c90f46

Browse files
chenjy29chenjy29
authored andcommitted
fix: 修复中文输入法拼音阶段选择选项后显示空白的问题
1 parent 35c1ad9 commit 4c90f46

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

components/vc-select/BaseSelect.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ export default defineComponent({
281281
const selectorRef = shallowRef<RefSelectorProps>(null);
282282
const listRef = shallowRef<RefOptionListProps>(null);
283283
const blurRef = ref<boolean>(false);
284+
const compositionStatus = ref<boolean>(false); // 基于BaseSelect定义当前是否处于输入法组合状态
285+
const onCompositionStart = () => {
286+
// 输入法组合开始时,设置compositionStatus为true
287+
compositionStatus.value = true;
288+
};
289+
const onCompositionEnd = () => {
290+
compositionStatus.value = false;
291+
};
284292

285293
/** Used for component focused management */
286294
const [mockFocused, setMockFocused, cancelSetMockFocused] = useDelayReset();
@@ -765,6 +773,7 @@ export default defineComponent({
765773
let clearNode: VueNode;
766774
const onClearMouseDown: MouseEventHandler = () => {
767775
onClear?.();
776+
onCompositionEnd();
768777

769778
onDisplayValuesChange([], {
770779
type: 'clear',
@@ -866,6 +875,9 @@ export default defineComponent({
866875
onSearchSubmit={onInternalSearchSubmit}
867876
onRemove={onSelectorRemove}
868877
tokenWithEnter={tokenWithEnter.value}
878+
compositionStatus={compositionStatus.value}
879+
onCompositionStart={onCompositionStart}
880+
onCompositionEnd={onCompositionEnd}
869881
/>
870882
);
871883
},

components/vc-select/Selector/SingleSelector.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const SingleSelector = defineComponent<SelectorProps>({
4444
setup(props) {
4545
const inputChanged = shallowRef(false);
4646

47+
// 本地输入状态管理
48+
const hasInput = shallowRef(false);
49+
4750
const combobox = computed(() => props.mode === 'combobox');
4851
const inputEditable = computed(() => combobox.value || props.showSearch);
4952

@@ -65,12 +68,38 @@ const SingleSelector = defineComponent<SelectorProps>({
6568
{ immediate: true },
6669
);
6770

71+
// 监听下拉框关闭(失焦)时重置输入状态
72+
watch(
73+
() => props.open,
74+
newOpen => {
75+
if (!newOpen) {
76+
hasInput.value = false;
77+
}
78+
},
79+
);
80+
81+
// 监听选择完成(values变化)时重置输入状态
82+
watch(
83+
() => props.values,
84+
() => {
85+
if (props.values && props.values.length > 0) {
86+
hasInput.value = false;
87+
}
88+
},
89+
{ deep: true },
90+
);
91+
6892
// Not show text when closed expect combobox mode
69-
const hasTextInput = computed(() =>
70-
props.mode !== 'combobox' && !props.open && !props.showSearch
93+
const hasTextInput = computed(() => {
94+
// 只有在本地输入状态为true时,才认为有文本输入
95+
if (!hasInput.value) {
96+
return false;
97+
}
98+
99+
return props.mode !== 'combobox' && !props.open && !props.showSearch
71100
? false
72-
: !!inputValue.value || props.compositionStatus,
73-
);
101+
: !!inputValue.value || props.compositionStatus;
102+
});
74103

75104
const title = computed(() => {
76105
const item = props.values[0];
@@ -92,6 +121,10 @@ const SingleSelector = defineComponent<SelectorProps>({
92121
};
93122
const handleInput = (e: Event) => {
94123
const composing = (e.target as any).composing;
124+
125+
// 用户输入时设置输入状态为true
126+
hasInput.value = true;
127+
95128
if (!composing) {
96129
inputChanged.value = true;
97130
props.onInputChange(e);
@@ -142,6 +175,7 @@ const SingleSelector = defineComponent<SelectorProps>({
142175
}
143176
return (
144177
<>
178+
{hasTextInput.value ? '输入中' : '输入完成'}
145179
<span class={`${prefixCls}-selection-search`}>
146180
<Input
147181
inputRef={inputRef}

components/vc-select/Selector/index.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type { CustomTagProps, DisplayValueType, Mode, RenderNode } from '../Base
1515
import { isValidateOpenKey } from '../utils/keyUtil';
1616
import useLock from '../hooks/useLock';
1717
import type { PropType } from 'vue';
18-
import { defineComponent, ref } from 'vue';
18+
import { defineComponent, ref, computed } from 'vue';
1919
import createRef from '../../_util/createRef';
2020
import PropTypes from '../../_util/vue-types';
2121
import type { VueNode } from '../../_util/type';
@@ -60,6 +60,9 @@ export interface SelectorProps {
6060
onSearchSubmit: (searchText: string) => void;
6161
onRemove: (value: DisplayValueType) => void;
6262
onInputKeyDown?: (e: KeyboardEvent) => void;
63+
compositionStatus?: boolean;
64+
onCompositionStart?: () => void;
65+
onCompositionEnd?: () => void;
6366

6467
/**
6568
* @private get real dom for trigger align.
@@ -115,6 +118,9 @@ const Selector = defineComponent<SelectorProps>({
115118
onSearchSubmit: Function,
116119
onRemove: Function,
117120
onInputKeyDown: { type: Function as PropType<EventHandler> },
121+
compositionStatus: { type: Boolean, default: false },
122+
onCompositionStart: { type: Function as PropType<() => void> },
123+
onCompositionEnd: { type: Function as PropType<() => void> },
118124

119125
/**
120126
* @private get real dom for trigger align.
@@ -124,7 +130,7 @@ const Selector = defineComponent<SelectorProps>({
124130
} as any,
125131
setup(props, { expose }) {
126132
const inputRef = createRef();
127-
const compositionStatus = ref(false);
133+
const compositionStatus = computed(() => props.compositionStatus || false);
128134

129135
// ====================== Input ======================
130136
const [getInputMouseDown, setInputMouseDown] = useLock(0);
@@ -174,11 +180,13 @@ const Selector = defineComponent<SelectorProps>({
174180
};
175181

176182
const onInputCompositionStart = () => {
177-
compositionStatus.value = true;
183+
// compositionStatus.value = true;
184+
props.onCompositionStart();
178185
};
179186

180187
const onInputCompositionEnd = (e: InputEvent) => {
181-
compositionStatus.value = false;
188+
// compositionStatus.value = false;
189+
props.onCompositionEnd();
182190
// Trigger search again to support `tokenSeparators` with typewriting
183191
if (props.mode !== 'combobox') {
184192
triggerOnSearch((e.target as HTMLInputElement).value);
@@ -244,6 +252,8 @@ const Selector = defineComponent<SelectorProps>({
244252
inputRef.current.focus();
245253
},
246254
blur: () => {
255+
console.log('onBlurrrrrrrr1111111');
256+
props.onCompositionEnd();
247257
inputRef.current.blur();
248258
},
249259
});

0 commit comments

Comments
 (0)