From 961a2ecfda317e6aa267e9a845ebf9f83ceffff6 Mon Sep 17 00:00:00 2001 From: Alona Sviechnikova Date: Sun, 15 Dec 2024 22:35:44 +0200 Subject: [PATCH] added solution --- README.md | 3 ++- package-lock.json | 9 +++++---- package.json | 2 +- src/App.tsx | 34 +++++++++++++++++++++++++++++----- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 415bb6e4e..a9c2aba30 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,5 @@ 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://alechsva.github.io/react_keyboard/) and add it to the PR description. + diff --git a/package-lock.json b/package-lock.json index 89cb98f0f..271fe6061 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ }, "devDependencies": { "@cypress/react18": "^2.0.1", - "@mate-academy/scripts": "^1.8.5", + "@mate-academy/scripts": "^1.9.12", "@mate-academy/students-ts-config": "*", "@mate-academy/stylelint-config": "*", "@types/node": "^20.14.10", @@ -1170,10 +1170,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.5", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.5.tgz", - "integrity": "sha512-mHRY2FkuoYCf5U0ahIukkaRo5LSZsxrTSgMJheFoyf3VXsTvfM9OfWcZIDIDB521kdPrScHHnRp+JRNjCfUO5A==", + "version": "1.9.12", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.9.12.tgz", + "integrity": "sha512-/OcmxMa34lYLFlGx7Ig926W1U1qjrnXbjFJ2TzUcDaLmED+A5se652NcWwGOidXRuMAOYLPU2jNYBEkKyXrFJA==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", diff --git a/package.json b/package.json index a727a0799..d272a7366 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "devDependencies": { "@cypress/react18": "^2.0.1", - "@mate-academy/scripts": "^1.8.5", + "@mate-academy/scripts": "^1.9.12", "@mate-academy/students-ts-config": "*", "@mate-academy/stylelint-config": "*", "@types/node": "^20.14.10", diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..9a9826638 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,31 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +interface AppState { + pressedKey: string; +} + +export class App extends React.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}

+
+ ); + } +}