From d8aa277f5e110301b6a23e1bf84c426efb8b14be Mon Sep 17 00:00:00 2001 From: Tara Black Date: Mon, 15 Nov 2021 11:34:25 -0600 Subject: [PATCH] ISSUE #55 bug-fix --- src/component/App.js | 15 ++++++++++++++- src/component/ValidKeys.js | 19 +++++++++++++++++++ src/logic/calculate.js | 3 +++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/component/ValidKeys.js diff --git a/src/component/App.js b/src/component/App.js index e0035d19c..249d811d1 100644 --- a/src/component/App.js +++ b/src/component/App.js @@ -2,6 +2,7 @@ import React from "react"; import Display from "./Display"; import ButtonPanel from "./ButtonPanel"; import calculate from "../logic/calculate"; +import { validKeys } from "./ValidKeys"; import "./App.css"; export default class App extends React.Component { @@ -19,9 +20,21 @@ export default class App extends React.Component { this.setState(calculate(this.state, buttonName)); }; + // ISSUE #55 doesn't support key presses + handleKeyPress = keyEvent => { + const key = validKeys.find(k => k.charCode === keyEvent.charCode); + if (key) { + this.setState(calculate(this.state, key.name)); + } + }; + render() { return ( -
+
diff --git a/src/component/ValidKeys.js b/src/component/ValidKeys.js new file mode 100644 index 000000000..5b43972ff --- /dev/null +++ b/src/component/ValidKeys.js @@ -0,0 +1,19 @@ +export const validKeys = [ + { name: "0", charCode: 48 }, + { name: "1", charCode: 49 }, + { name: "2", charCode: 50 }, + { name: "3", charCode: 51 }, + { name: "4", charCode: 52 }, + { name: "5", charCode: 53 }, + { name: "6", charCode: 54 }, + { name: "7", charCode: 55 }, + { name: "8", charCode: 56 }, + { name: "9", charCode: 57 }, + { name: ".", charCode: 46 }, + { name: "=", charCode: 13 }, + { name: "+", charCode: 43 }, + { name: "-", charCode: 45 }, + { name: "x", charCode: 42 }, + { name: "÷", charCode: 47 }, + { name: "%", charCode: 53 }, +]; diff --git a/src/logic/calculate.js b/src/logic/calculate.js index 5e6cc57f0..61726f105 100644 --- a/src/logic/calculate.js +++ b/src/logic/calculate.js @@ -11,6 +11,9 @@ import isNumber from "./isNumber"; * total:String the running total * next:String the next number to be operated on with the total * operation:String +, -, etc. + * * higherOder:Object + * next:String the next number to be operated on with the total + * operation:String x, ÷, etc. */ export default function calculate(obj, buttonName) { if (buttonName === "AC") {