Skip to content

Commit feb8200

Browse files
authored
fix(field): add preserveOriginalLabel prop to SearchSelect (#8919)
- Add preserveOriginalLabel prop to control whether to use the original label when option is selected - Fix the issue where highlighted search text appears in the input box when option is selected BREAKING CHANGE: None
1 parent afd86a0 commit feb8200

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/field/src/components/Select/SearchSelect/index.tsx

+25-2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ export interface SearchSelectProps<T = Record<string, any>>
8989

9090
/** 默认搜索关键词 */
9191
defaultSearchValue?: string;
92+
93+
/**
94+
* 在选择时保留选项的原始标签文本
95+
* 当设置为 true 时,选中后回填的内容将使用选项的原始 label,而不是经过 optionItemRender 处理后的内容
96+
* @default false
97+
*/
98+
preserveOriginalLabel?: boolean;
9299
}
93100

94101
const SearchSelect = <T,>(props: SearchSelectProps<T[]>, ref: any) => {
@@ -115,6 +122,7 @@ const SearchSelect = <T,>(props: SearchSelectProps<T[]>, ref: any) => {
115122
showSearch,
116123
fieldNames,
117124
defaultSearchValue,
125+
preserveOriginalLabel = false,
118126
...restProps
119127
} = props;
120128

@@ -163,6 +171,7 @@ const SearchSelect = <T,>(props: SearchSelectProps<T[]>, ref: any) => {
163171
return {
164172
...dataItem,
165173
...item,
174+
label: preserveOriginalLabel ? dataItem.label : item.label,
166175
};
167176
});
168177
}
@@ -284,9 +293,23 @@ const SearchSelect = <T,>(props: SearchSelectProps<T[]>, ref: any) => {
284293
const dataItem = optionList && optionList['data-item'];
285294
// 如果value值为空则是清空时产生的回调,直接传值就可以了
286295
if (!value || !dataItem) {
287-
onChange?.(value, optionList, ...rest);
296+
const changedValue = value
297+
? {
298+
...value,
299+
label: preserveOriginalLabel ? dataItem?.label : value.label,
300+
}
301+
: value;
302+
onChange?.(changedValue, optionList, ...rest);
288303
} else {
289-
onChange?.({ ...value, ...dataItem }, optionList, ...rest);
304+
onChange?.(
305+
{
306+
...value,
307+
...dataItem,
308+
label: preserveOriginalLabel ? dataItem.label : value.label,
309+
},
310+
optionList,
311+
...rest,
312+
);
290313
}
291314
return;
292315
}

packages/field/src/components/Select/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ const FieldSelect: ProFieldFC<
528528
fetchData(keyWord);
529529
}}
530530
resetData={resetData}
531+
preserveOriginalLabel
531532
optionItemRender={(item) => {
532533
if (typeof item.label === 'string' && keyWordsRef.current) {
533534
return (

0 commit comments

Comments
 (0)