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: Do not respond to the Enter event when data is loading #257

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/Mentions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface MentionsProps extends BaseTextareaAttrs {
prefix?: string | string[];
prefixCls?: string;
value?: string;
loading?: boolean;
filterOption?: false | typeof defaultFilterOption;
validateSearch?: typeof defaultValidateSearch;
onChange?: (text: string) => void;
Expand Down Expand Up @@ -101,6 +102,7 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
options,
open,
allowClear,
loading,

// Events
validateSearch = defaultValidateSearch,
Expand Down Expand Up @@ -330,6 +332,9 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
} else if (which === KeyCode.ENTER) {
// Measure hit
event.preventDefault();
// loading skip
if (loading) return;
wanpan11 marked this conversation as resolved.
Show resolved Hide resolved

if (!mergedOptions.length) {
stopMeasure();
return;
Expand Down
22 changes: 22 additions & 0 deletions tests/Mentions.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe('Mentions', () => {
return render(createMentions(props));
}

beforeEach(() => {
jest.useFakeTimers();
Copy link
Member

Choose a reason for hiding this comment

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

把下面的 beforeEach 和 afterEach 挪上来把,反正也是 fakeTimer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

把下面的 beforeEach 和 afterEach 挪上来把,反正也是 fakeTimer

done

});

describe('focus test', () => {
beforeEach(() => {
jest.useFakeTimers();
Expand Down Expand Up @@ -135,6 +139,24 @@ describe('Mentions', () => {
});
expect(onChange).toHaveBeenCalledWith('bamboo');
});

it('Keyboard Enter event', () => {
const { container, rerender } = renderMentions();
simulateInput(container, '@lig');
fireEvent.keyDown(container.querySelector('textarea'), {
which: KeyCode.ENTER,
keyCode: KeyCode.ENTER,
});
expect(container.querySelector('textarea').value).toBe('@light ');

rerender(createMentions({ loading: true }));
simulateInput(container, '@lig');
fireEvent.keyDown(container.querySelector('textarea'), {
which: KeyCode.ENTER,
keyCode: KeyCode.ENTER,
});
expect(container.querySelector('textarea').value).toBe('@lig');
});
});

describe('support children Option', () => {
Expand Down
Loading