From 9a2a905f53bd7bea7785353326a3cfd6ca526624 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Fri, 21 Jul 2023 09:34:51 +0300 Subject: [PATCH] add task solution --- src/App.tsx | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..926e44716 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,39 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +export class App extends React.Component { + state = { + pressedKey: null, + }; + + componentDidMount(): void { + document.addEventListener('keyup', this.handleKeyUp); + } + + componentWillUnmount(): void { + document.removeEventListener('keyup', this.handleKeyUp); + } + + handleKeyUp = (event: KeyboardEvent) => { + this.setState({ pressedKey: event.key }); + }; + + render() { + const { pressedKey } = this.state; + + let message; + + if (pressedKey) { + message = `The last pressed key is [${pressedKey}]`; + } else { + message = 'Nothing was pressed yet'; + } + + return ( +
+

+ {message} +

+
+ ); + } +}