Fixed a bug where escape would delete all typed content (#30452)

This commit is contained in:
Harshil Sharma 2025-03-13 19:01:16 +05:30 committed by GitHub
parent 3af8d50bbe
commit e2a7240e34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 2 deletions

View file

@ -14,7 +14,7 @@ import type Textbox from 'components/textbox/textbox';
import mergeObjects from 'packages/mattermost-redux/test/merge_objects';
import {renderWithContext, userEvent, screen} from 'tests/react_testing_utils';
import {Locations, StoragePrefixes} from 'utils/constants';
import Constants, {Locations, StoragePrefixes} from 'utils/constants';
import {TestHelper} from 'utils/test_helper';
import type {PostDraft} from 'types/store/draft';
@ -202,6 +202,37 @@ describe('components/avanced_text_editor/advanced_text_editor', () => {
const textbox = screen.getByTestId('post_textbox');
userEvent.type(textbox, 'something{esc}');
expect(textbox).not.toHaveFocus();
expect(mockedUpdateDraft).not.toHaveBeenCalled();
});
it('ESC should blur the input and reset draft when in editing mode', () => {
jest.useFakeTimers();
const props = {
...baseProps,
isInEditMode: true,
};
renderWithContext(
<AdvancedTextEditor
{...props}
/>,
mergeObjects(initialState, {
entities: {
roles: {
roles: {
user_roles: {permissions: [Permissions.CREATE_POST]},
},
},
},
}),
);
const textbox = screen.getByTestId('edit_textbox');
userEvent.type(textbox, 'something{esc}');
expect(textbox).not.toHaveFocus();
// save is called with a short delayed after pressing escape key
jest.advanceTimersByTime(Constants.SAVE_DRAFT_TIMEOUT + 50);
expect(mockedRemoveDraft).toHaveBeenCalled();
expect(mockedUpdateDraft).not.toHaveBeenCalled();
});
});

View file

@ -173,9 +173,9 @@ const useKeyHandler = (
}
if (Keyboard.isKeyPressed(e, KeyCodes.ESCAPE)) {
onCancel?.();
textboxRef.current?.blur();
if (isInEditMode) {
onCancel?.();
dispatch(unsetEditingPost());
}
}