Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 17ab522

Browse files
authored
Tooltip: close field tooltip when ESC is pressed (#12553)
* Close field tooltip when ESC is pressed * Use `Key.ESCAPE`
1 parent e8bb241 commit 17ab522

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/components/views/elements/Field.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React, { InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes, RefObject, createRef } from "react";
17+
import React, {
18+
InputHTMLAttributes,
19+
SelectHTMLAttributes,
20+
TextareaHTMLAttributes,
21+
RefObject,
22+
createRef,
23+
KeyboardEvent,
24+
} from "react";
1825
import classNames from "classnames";
1926
import { debounce } from "lodash";
2027

2128
import { IFieldState, IValidationResult } from "./Validation";
2229
import Tooltip from "./Tooltip";
30+
import { Key } from "../../../Keyboard";
2331

2432
// Invoke validation from user input (when typing, etc.) at most once every N ms.
2533
const VALIDATION_THROTTLE_MS = 200;
@@ -232,6 +240,18 @@ export default class Field extends React.PureComponent<PropShapes, IState> {
232240
return this.props.inputRef ?? this._inputRef;
233241
}
234242

243+
private onKeyDown = (evt: KeyboardEvent<HTMLDivElement>): void => {
244+
// If the tooltip is displayed to show a feedback and Escape is pressed
245+
// The tooltip is hided
246+
if (this.state.feedbackVisible && evt.key === Key.ESCAPE) {
247+
evt.preventDefault();
248+
evt.stopPropagation();
249+
this.setState({
250+
feedbackVisible: false,
251+
});
252+
}
253+
};
254+
235255
public render(): React.ReactNode {
236256
/* eslint @typescript-eslint/no-unused-vars: ["error", { "ignoreRestSiblings": true }] */
237257
const {
@@ -318,7 +338,7 @@ export default class Field extends React.PureComponent<PropShapes, IState> {
318338
});
319339

320340
return (
321-
<div className={fieldClasses}>
341+
<div className={fieldClasses} onKeyDown={this.onKeyDown}>
322342
{prefixContainer}
323343
{fieldInput}
324344
<label htmlFor={this.id}>{this.props.label}</label>

test/components/views/elements/Field-test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ describe("Field", () => {
6969

7070
// Expect 'alert' role
7171
expect(screen.queryByRole("alert")).toBeInTheDocument();
72+
73+
// Close the feedback is Escape is pressed
74+
fireEvent.keyDown(screen.getByRole("textbox"), { key: "Escape" });
75+
expect(screen.queryByRole("alert")).toBeNull();
7276
});
7377

7478
it("Should mark the feedback as status if valid", async () => {
@@ -87,6 +91,10 @@ describe("Field", () => {
8791

8892
// Expect 'status' role
8993
expect(screen.queryByRole("status")).toBeInTheDocument();
94+
95+
// Close the feedback is Escape is pressed
96+
fireEvent.keyDown(screen.getByRole("textbox"), { key: "Escape" });
97+
expect(screen.queryByRole("status")).toBeNull();
9098
});
9199

92100
it("Should mark the feedback as tooltip if custom tooltip set", async () => {
@@ -106,6 +114,10 @@ describe("Field", () => {
106114

107115
// Expect 'tooltip' role
108116
expect(screen.queryByRole("tooltip")).toBeInTheDocument();
117+
118+
// Close the feedback is Escape is pressed
119+
fireEvent.keyDown(screen.getByRole("textbox"), { key: "Escape" });
120+
expect(screen.queryByRole("tooltip")).toBeNull();
109121
});
110122
});
111123
});

0 commit comments

Comments
 (0)