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

Fix TagInput adding a tag when Enter is pressed while composing #6767

Merged
merged 3 commits into from
Apr 19, 2024
Merged
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
3 changes: 2 additions & 1 deletion packages/core/src/components/tag-input/tagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ export class TagInput extends AbstractPureComponent<TagInputProps, TagInputState

let activeIndexToEmit = activeIndex;

if (event.key === "Enter" && value.length > 0) {
// do not add a new tag if the user is composing (e.g. for Japanese or Chinese)
if (event.key === "Enter" && !event.nativeEvent.isComposing && value.length > 0) {
this.addTags(value, "default");
} else if (selectionEnd === 0 && this.props.values.length > 0) {
// cursor at beginning of input allows interaction with tags.
Expand Down
20 changes: 17 additions & 3 deletions packages/core/test/tag-input/tagInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ describe("<TagInput>", () => {
assert.isTrue(onAdd.notCalled);
});

it("is not invoked on enter when input is composing", () => {
const onAdd = sinon.stub();
const wrapper = mountTagInput(onAdd);
pressEnterInInputWhenComposing(wrapper, "构成");
assert.isTrue(onAdd.notCalled);
});

it("is invoked on enter", () => {
const onAdd = sinon.stub();
const wrapper = mountTagInput(onAdd);
Expand Down Expand Up @@ -455,7 +462,7 @@ describe("<TagInput>", () => {
it("pressing backspace does not remove item", () => {
const onRemove = sinon.spy();
const wrapper = mount(<TagInput onRemove={onRemove} values={VALUES} />);
wrapper.find("input").simulate("keydown", createInputKeydownEventMetadata("text", "Backspace"));
wrapper.find("input").simulate("keydown", createInputKeydownEventMetadata("text", "Backspace", false));
assert.isTrue(onRemove.notCalled);
});
});
Expand Down Expand Up @@ -576,13 +583,20 @@ describe("<TagInput>", () => {
});

function pressEnterInInput(wrapper: ReactWrapper<any, any>, value: string) {
wrapper.find("input").prop("onKeyDown")?.(createInputKeydownEventMetadata(value, "Enter") as any);
wrapper.find("input").prop("onKeyDown")?.(createInputKeydownEventMetadata(value, "Enter", false) as any);
}

function pressEnterInInputWhenComposing(wrapper: ReactWrapper<any, any>, value: string) {
wrapper.find("input").prop("onKeyDown")?.(createInputKeydownEventMetadata(value, "Enter", true) as any);
}

function createInputKeydownEventMetadata(value: string, key: string) {
function createInputKeydownEventMetadata(value: string, key: string, isComposing: boolean) {
return {
currentTarget: { value },
key,
nativeEvent: {
isComposing,
},
// Enzyme throws errors if we don't mock the stopPropagation method.
stopPropagation: () => {
return;
Expand Down