Skip to content

Commit

Permalink
ISSUE andrewagain#55 bug-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethhess0320 committed Nov 15, 2021
1 parent 1b6c151 commit d8aa277
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/component/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 (
<div className="component-app">
<div
tabIndex="0"
onKeyPress={this.handleKeyPress}
className="component-app"
>
<Display value={this.state.next || this.state.total || "0"} />
<ButtonPanel clickHandler={this.handleClick} />
</div>
Expand Down
19 changes: 19 additions & 0 deletions src/component/ValidKeys.js
Original file line number Diff line number Diff line change
@@ -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 },
];
3 changes: 3 additions & 0 deletions src/logic/calculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit d8aa277

Please sign in to comment.