From 0e08a0085c9c50dbebc2f61e75a51c05ba5e0fde Mon Sep 17 00:00:00 2001 From: Eugene Kulish Date: Wed, 19 Jul 2023 15:03:55 +0300 Subject: [PATCH 1/3] add keyboard --- README.md | 2 +- src/App.tsx | 40 +++++++++++++++++++++++++++++++++++----- src/AppAsFC/AppAsFC.tsx | 29 +++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/AppAsFC/AppAsFC.tsx diff --git a/README.md b/README.md index f7a6277cf..bedb7fc6c 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://eugene-kulish.github.io/react_keyboard/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..f2184d540 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,37 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

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

+ {(this.state.key !== '') + ? (`The last pressed key is [${this.state.key}]`) + : ('Nothing was pressed yet')} +

+
+ + + + ); + } +} diff --git a/src/AppAsFC/AppAsFC.tsx b/src/AppAsFC/AppAsFC.tsx new file mode 100644 index 000000000..5ddb9aa28 --- /dev/null +++ b/src/AppAsFC/AppAsFC.tsx @@ -0,0 +1,29 @@ +import React, { useEffect, useState } from 'react'; + +export const AppAsFC: React.FC = () => { + const [key, setKey] = useState(''); + + useEffect(() => { + const handleDocumentKeyPress = (event: KeyboardEvent) => { + setKey(event.key); + }; + + document.addEventListener('keyup', handleDocumentKeyPress); + + return () => { + document.removeEventListener('keyup', handleDocumentKeyPress); + }; + }, []); + + return ( + <> +
+

+ {(key !== '') + ? (`The last pressed key is [${key}]`) + : ('Nothing was pressed yet')} +

+
+ + ); +}; From 3280a08921e025d227462f8746a4dc41997a384c Mon Sep 17 00:00:00 2001 From: Eugene Kulish Date: Wed, 19 Jul 2023 15:08:32 +0300 Subject: [PATCH 2/3] add keyboard --- src/App.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f2184d540..6daeb7465 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,5 @@ import React from 'react'; -import { AppAsFC } from './AppAsFC/AppAsFC'; - export class App extends React.Component { state = { key: '', @@ -29,8 +27,6 @@ export class App extends React.Component { : ('Nothing was pressed yet')}

- - ); } From 272d441fc73d079255da68f20b93ded329d15c31 Mon Sep 17 00:00:00 2001 From: Eugene Kulish Date: Wed, 19 Jul 2023 19:31:11 +0300 Subject: [PATCH 3/3] fix --- src/App.tsx | 16 +++++++--------- src/AppAsFC/AppAsFC.tsx | 16 +++++++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 6daeb7465..f9bb2e49d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -19,15 +19,13 @@ export class App extends React.Component { render() { return ( - <> -
-

- {(this.state.key !== '') - ? (`The last pressed key is [${this.state.key}]`) - : ('Nothing was pressed yet')} -

-
- +
+

+ {(this.state.key !== '') + ? `The last pressed key is [${this.state.key}]` + : 'Nothing was pressed yet'} +

+
); } } diff --git a/src/AppAsFC/AppAsFC.tsx b/src/AppAsFC/AppAsFC.tsx index 5ddb9aa28..c9ec0923c 100644 --- a/src/AppAsFC/AppAsFC.tsx +++ b/src/AppAsFC/AppAsFC.tsx @@ -16,14 +16,12 @@ export const AppAsFC: React.FC = () => { }, []); return ( - <> -
-

- {(key !== '') - ? (`The last pressed key is [${key}]`) - : ('Nothing was pressed yet')} -

-
- +
+

+ {(key !== '') + ? `The last pressed key is [${key}]` + : 'Nothing was pressed yet'} +

+
); };