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 all 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 | - |
| onBlurRemoveSpaces | Whether to remove spaces 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
34 changes: 34 additions & 0 deletions docs/examples/tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,40 @@ const Test: React.FC = () => {
{children}
</Select>
</div>
<h2>tags select with onBlurRemoveSpaces = false</h2>
<div>
<Select
placeholder="placeholder"
mode="tags"
style={{ width: 500 }}
disabled={disabled}
maxTagCount={maxTagCount}
maxTagTextLength={10}
value={value}
onBlurRemoveSpaces={false}
onChange={(val) => {
console.log('change:', val);
setValue(val);
}}
/>
</div>
<h2>tags select with onBlurAddValue = false</h2>
<div>
<Select
placeholder="placeholder"
mode="tags"
style={{ width: 500 }}
disabled={disabled}
maxTagCount={maxTagCount}
maxTagTextLength={10}
value={value}
onBlurAddValue={false}
onChange={(val) => {
console.log('change:', val);
setValue(val);
}}
/>
</div>
</div>
);
};
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
onBlurRemoveSpaces?: 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
onBlurRemoveSpaces = 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 = onBlurRemoveSpaces ? (searchText || '').trim() : searchText || '';
// prevent empty tags from appearing when you click the Enter button
if (formatted) {
if (formatted.trim() && 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
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
18 changes: 18 additions & 0 deletions tests/Tags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,22 @@ describe('Select.Tags', () => {
});
expect(onChange).not.toBeCalled();
});

it('should not add value when onBlurAddValue is false', () => {
const { container } = render(<Select mode="tags" onBlurAddValue={false} />);
const input = container.querySelector<HTMLInputElement>('input');
toggleOpen(container);
fireEvent.change(input, { target: { value: 'test' } });
keyDown(input, KeyCode.TAB);
// no selection item
expect(container.querySelectorAll('.rc-select-selection-item')).toHaveLength(0);
});

it('should not add value when onBlurRemoveSpaces is false', () => {
const { container } = render(<Select mode="tags" onBlurRemoveSpaces={false} />);
toggleOpen(container);
fireEvent.change(container.querySelector('input'), { target: { value: ' test ' } });
keyDown(container.querySelector('input'), KeyCode.ENTER);
expect(findSelection(container).textContent).toEqual(' test ');
});
});
Loading