Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support overriding default component behavior for the onBlur event in tags mode #1081

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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;
Copy link
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('');

return;
}

Expand Down
Loading