From 537df67149f5839ebe8e2419ace14f5496c8749e Mon Sep 17 00:00:00 2001 From: Mykola Bilenkiy Date: Tue, 4 Feb 2025 00:24:52 +0200 Subject: [PATCH] develop --- README.md | 2 +- package-lock.json | 9 +++++---- package.json | 2 +- src/App.tsx | 39 ++++++++++++++++++++++++++++++++++----- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 415bb6e4e..4d4467bdf 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://cyberte4.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..97cbbef1b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,36 @@ import React from 'react'; +type State = { + key: string | null; +}; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +export class App extends React.Component<{}, State> { + state: State = { + key: '', + }; + + handleKeyChange = (event: KeyboardEvent) => { + this.setState({ key: event.key }); + }; + + componentDidMount(): void { + document.addEventListener('keyup', this.handleKeyChange); + } + + componentWillUnmount(): void { + document.removeEventListener('keyup', this.handleKeyChange); + } + + render() { + return ( +
+ {this.state.key ? ( +

+ The last pressed key is [{this.state.key}] +

+ ) : ( +

Nothing was pressed yet

+ )} +
+ ); + } +}