From 10d892796c690f7d1d6c11ad6630baaab18169b5 Mon Sep 17 00:00:00 2001 From: Virineya Marchuk Date: Wed, 11 Dec 2024 22:10:13 +0200 Subject: [PATCH 1/2] solution --- README.md | 2 +- src/App.tsx | 34 +++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 415bb6e4e..36973565b 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://vtroni.github.io/react_keyboard/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..3be28f6ca 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]

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

The last pressed key is {this.state.pressedKey}

+ ) : ( +

Nothing was pressed yet

+ )} +
+ ); + } +} From aedbb6ab821b743866c0000edbabe5c9fafce981 Mon Sep 17 00:00:00 2001 From: Virineya Marchuk Date: Wed, 11 Dec 2024 22:24:05 +0200 Subject: [PATCH 2/2] solution --- src/App.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/App.tsx b/src/App.tsx index 3be28f6ca..96e915f40 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -21,7 +21,9 @@ export class App extends React.Component { return (
{this.state.pressedKey ? ( -

The last pressed key is {this.state.pressedKey}

+

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

) : (

Nothing was pressed yet

)}