From c2c250f52f04e5b538f54bfebea794a88bb14831 Mon Sep 17 00:00:00 2001 From: Maksym Mohyla Date: Mon, 7 Oct 2024 15:02:00 +0300 Subject: [PATCH] last pressed key is shown --- README.md | 15 ++++++++------- package-lock.json | 9 +++++---- package.json | 2 +- src/App.tsx | 31 ++++++++++++++++++++++++++----- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 415bb6e4e..fa4dd1b03 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,18 @@ Make the `App` a class component with `pressedKey` in the `state`. - before any key was pressed show the `Nothing was pressed yet` message; - when a key is pressed show a `The last pressed key is [key]` message; - use `componentDidMount` to add `keyup` handler: - ```ts - // DON'T import KeyboardEvent from React, because it is a regular event - document.addEventListener('keyup', (event: KeyboardEvent) => { - console.log(event.key); - }); - ``` + ```ts + // DON'T import KeyboardEvent from React, because it is a regular event + document.addEventListener('keyup', (event: KeyboardEvent) => { + console.log(event.key); + }); + ``` - use `removeEventListener` to remove a global handler in `componentWillUnmount`. ## Instructions + - Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save. - 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://MaksymMohyla.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..80095cc50 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,28 @@ import React from 'react'; +import { render } from 'react-dom'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

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

{this.state.message}

+
+ ); + } +}