Skip to content

Commit

Permalink
feat: support overriding default component behavior for the onBlur ev…
Browse files Browse the repository at this point in the history
…ent in tags mode
  • Loading branch information
小豪 committed Oct 20, 2024
1 parent ca86130 commit 14e0a33
Showing 1 changed file with 12 additions and 3 deletions.
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;
}

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

0 comments on commit 14e0a33

Please sign in to comment.