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

Tooltip: close field tooltip when ESC is pressed #12553

Merged
merged 2 commits into from
May 28, 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
23 changes: 21 additions & 2 deletions src/components/views/elements/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes, RefObject, createRef } from "react";
import React, {
InputHTMLAttributes,
SelectHTMLAttributes,
TextareaHTMLAttributes,
RefObject,
createRef,
KeyboardEvent,
} from "react";
import classNames from "classnames";
import { debounce } from "lodash";

Expand Down Expand Up @@ -232,6 +239,18 @@ export default class Field extends React.PureComponent<PropShapes, IState> {
return this.props.inputRef ?? this._inputRef;
}

private onKeyDown = (evt: KeyboardEvent<HTMLDivElement>): void => {
// If the tooltip is displayed to show a feedback and Escape is pressed
// The tooltip is hided
if (this.state.feedbackVisible && evt.key === "Escape") {
florianduros marked this conversation as resolved.
Show resolved Hide resolved
evt.preventDefault();
evt.stopPropagation();
this.setState({
feedbackVisible: false,
});
}
};

public render(): React.ReactNode {
/* eslint @typescript-eslint/no-unused-vars: ["error", { "ignoreRestSiblings": true }] */
const {
Expand Down Expand Up @@ -318,7 +337,7 @@ export default class Field extends React.PureComponent<PropShapes, IState> {
});

return (
<div className={fieldClasses}>
<div className={fieldClasses} onKeyDown={this.onKeyDown}>
{prefixContainer}
{fieldInput}
<label htmlFor={this.id}>{this.props.label}</label>
Expand Down
12 changes: 12 additions & 0 deletions test/components/views/elements/Field-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ describe("Field", () => {

// Expect 'alert' role
expect(screen.queryByRole("alert")).toBeInTheDocument();

// Close the feedback is Escape is pressed
fireEvent.keyDown(screen.getByRole("textbox"), { key: "Escape" });
expect(screen.queryByRole("alert")).toBeNull();
});

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

// Expect 'status' role
expect(screen.queryByRole("status")).toBeInTheDocument();

// Close the feedback is Escape is pressed
fireEvent.keyDown(screen.getByRole("textbox"), { key: "Escape" });
expect(screen.queryByRole("status")).toBeNull();
});

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

// Expect 'tooltip' role
expect(screen.queryByRole("tooltip")).toBeInTheDocument();

// Close the feedback is Escape is pressed
fireEvent.keyDown(screen.getByRole("textbox"), { key: "Escape" });
expect(screen.queryByRole("tooltip")).toBeNull();
});
});
});
Loading