From a5ad29fd3a28e91762f7ea9b94a8c0a822d6d7bd Mon Sep 17 00:00:00 2001 From: Roma Zuravliov Date: Sun, 13 Oct 2024 12:17:33 +0300 Subject: [PATCH] add task solution --- README.md | 2 +- src/App.tsx | 37 +++++++++++++++++++++++++++++++------ src/index.tsx | 4 ++-- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 415bb6e4e..e37fe2c8b 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,4 @@ Make the `App` a class component with `pressedKey` in the `state`. - Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline). - Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript). - Open one more terminal and run tests with `npm test` to ensure your solution is correct. -- Replace `` with your Github username in the [DEMO LINK](https://.github.io/react_keyboard/) and add it to the PR description. +- Replace `` with your Github username in the [DEMO LINK](https://romazh1988.github.io/react_keyboard/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..857d27f52 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,32 @@ -import React from 'react'; +import React, { Component } from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +interface AppState { + pressedKey: string; +} + +class App extends Component<{}, AppState> { + state: AppState = { + pressedKey: 'Nothing was pressed yet', + }; + + handleKeyUp = (event: KeyboardEvent) => { + this.setState({ pressedKey: `The last pressed key is [${event.key}]` }); + }; + + componentDidMount() { + document.addEventListener('keyup', this.handleKeyUp); + } + + componentWillUnmount() { + document.removeEventListener('keyup', this.handleKeyUp); + } + + render() { + return ( +
+

{this.state.pressedKey}

+
+ ); + } +} +export default App; diff --git a/src/index.tsx b/src/index.tsx index 7d27337cd..c042a9b0c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,5 @@ -import { createRoot } from 'react-dom/client'; -import { App } from './App'; +import React, { createRoot } from 'react-dom/client'; +import App from './App'; import './index.scss';