From ac3cf363b7817fa957275d7e8b6fa704ed4388b9 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Fri, 9 Nov 2018 16:37:34 -0500 Subject: [PATCH] MM-12944: force "Delete" focus on showing delete post modal (#2020) Focus handling is a mess. This doesn't really make it much better, other than making the behaviour explicit and constrained to within the modal dialog in question, fixing a regression introduced by MM-12688. --- .../delete_post_modal/delete_post_modal.jsx | 24 +++++++------------ .../delete_post_modal.test.jsx.snap | 2 ++ tests/components/delete_post_modal.test.jsx | 12 ++++++++++ 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/components/delete_post_modal/delete_post_modal.jsx b/components/delete_post_modal/delete_post_modal.jsx index 27ee9a01ca65..e77b96f2e5ae 100644 --- a/components/delete_post_modal/delete_post_modal.jsx +++ b/components/delete_post_modal/delete_post_modal.jsx @@ -17,30 +17,15 @@ export default class DeletePostModal extends React.PureComponent { teamName: PropTypes.string, post: PropTypes.object.isRequired, commentCount: PropTypes.number.isRequired, - - /** - * Does the post come from RHS mode - */ isRHS: PropTypes.bool.isRequired, - - /** - * Function called when modal is dismissed - */ onHide: PropTypes.func.isRequired, - actions: PropTypes.shape({ - - /** - * Function called for deleting post - */ deleteAndRemovePost: PropTypes.func.isRequired, }), } constructor(props) { super(props); - this.handleDelete = this.handleDelete.bind(this); - this.onHide = this.onHide.bind(this); this.state = { show: true, }; @@ -66,7 +51,13 @@ export default class DeletePostModal extends React.PureComponent { } } - onHide() { + handleEntered = () => { + if (this.deletePostBtn) { + this.deletePostBtn.focus(); + } + } + + onHide = () => { this.setState({show: false}); if (!UserAgent.isMobile()) { @@ -111,6 +102,7 @@ export default class DeletePostModal extends React.PureComponent { return ( { expect(wrapper).toMatchSnapshot(); }); + test('should focus delete button on enter', () => { + const wrapper = shallow( + + ); + + const deletePostBtn = {focus: jest.fn()}; + wrapper.instance().deletePostBtn = deletePostBtn; + + wrapper.instance().handleEntered(); + expect(deletePostBtn.focus).toHaveBeenCalled(); + }); + test('should match state when onHide is called', () => { const wrapper = shallow(