Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export default () => (
| optionRender | Custom rendering options | (oriOption: FlattenOptionData\<BaseOptionType\> , info: { index: number }) => React.ReactNode | - |
| labelRender | Custom rendering label | (props: LabelInValueType) => React.ReactNode | - |
| maxCount | The max number of items can be selected | number | - |
| onBlurRemoveSpace | Whether to remove space when losing focus, only applies when `mode` is `tags` | boolean | true |
| onBlurAddValue | Whether to add the input value to the selected item when losing focus, only applies when `mode` is `tags` | boolean | true |

### Methods

Expand Down
15 changes: 12 additions & 3 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
defaultValue?: ValueType | null;
maxCount?: number;
onChange?: (value: ValueType, option: OptionType | OptionType[]) => void;

// >>> tags mode only
onBlurRemoveSpace?: boolean;
onBlurAddValue?: boolean;
Comment on lines +165 to +168
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议为新增的属性添加注释或文档说明

为了提高代码的可维护性和可读性,建议为新增的 onBlurRemoveSpaceonBlurAddValue 属性添加注释,解释其用途和使用方法,或在组件的文档中进行说明。

}

function isRawValue(value: DraftValueType): value is RawValueType {
Expand Down Expand Up @@ -211,6 +215,10 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
onChange,
maxCount,

// tags mode only
onBlurRemoveSpace = true,
onBlurAddValue = true,

...restProps
} = props;

Expand Down Expand Up @@ -575,15 +583,16 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp

// [Submit] Tag mode should flush input
if (info.source === 'submit') {
const formatted = (searchText || '').trim();
const formatted = onBlurRemoveSpace ? (searchText || '').trim() : searchText || '';
// prevent empty tags from appearing when you click the Enter button
if (formatted) {
if (formatted && onBlurAddValue) {
const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
triggerChange(newRawValues);
triggerSelect(formatted, true);
setSearchValue('');
}

setSearchValue('');

Comment on lines +586 to +595
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议优化空格处理逻辑

当前实现存在以下可以改进的地方:

  1. onBlurRemoveSpaces 为 true 时存在重复的 trim 操作
  2. 条件判断的顺序可能导致混淆

建议重构如下:

- const formatted = onBlurRemoveSpaces ? (searchText || '').trim() : searchText || '';
- // prevent empty tags from appearing when you click the Enter button
- if (formatted.trim() && onBlurAddValue) {
+ const formatted = searchText || '';
+ const valueToAdd = onBlurRemoveSpaces ? formatted.trim() : formatted;
+ // prevent empty tags from appearing when you click the Enter button
+ if (valueToAdd && onBlurAddValue) {
    const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
    triggerChange(newRawValues);
    triggerSelect(formatted, true);
  }

这样可以:

  • 避免重复的 trim 操作
  • 使空格处理逻辑更清晰
  • 保持与属性名称的语义一致性

Committable suggestion skipped: line range outside the PR's diff.

return;
}

Expand Down