Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math magician refracting using functions and hooks #4

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Screenshot

Not available for this branch
![screenshot](./images/Capture.PNG)

## Live Demo

Expand Down
Binary file added images/Capture.PNG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 22 additions & 26 deletions src/components/calculator.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import React from 'react';
import React, { useState } from 'react';
import calculate from '../logic/calculate';

class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = { total: 0, operation: '', next: '' };
}
const Calculator = () => {
const [state, setState] = useState({ total: 0, operation: '', next: '' });

handleClick = (items) => this.setState((e) => calculate(e, items.target.textContent));

render() {
const { total, operation, next } = this.state;
let idValue = '';
const display = `${total || operation || next ? `${total || ''} ${operation || ''} ${next || ''}` : '0'} `;
const buttonsArray = ['AC', '+/-', '%', '梅', 7, 8, 9, 'x', 4, 5, 6, '-', 1, 2, 3, '+', 0, '.', '='].map((keys) => {
if (keys === '梅' || keys === 'x' || keys === '-' || keys === '+' || keys === '=') idValue = 'operator';
else if (keys === 0) idValue = 'button-0';
else idValue = 'button';
return <button type="button" className="button" id={idValue} key={keys} onClick={this.handleClick}>{keys}</button>;
});
return (
<div className="calculator_container">
<div className="display"><div className="display_string">{display}</div></div>
<div className="button_container">{buttonsArray}</div>
</div>
);
}
}
const handleClick = (items) => {
const displayValue = calculate(state, items.target.textContent);
setState(displayValue);
};
let idValue = '';
const display = `${state.total || state.operation || state.next ? `${state.total || ''} ${state.operation || ''} ${state.next || ''}` : '0'} `;
const buttonsArray = ['AC', '+/-', '%', '梅', 7, 8, 9, 'x', 4, 5, 6, '-', 1, 2, 3, '+', 0, '.', '='].map((keys) => {
if (keys === '梅' || keys === 'x' || keys === '-' || keys === '+' || keys === '=') idValue = 'operator';
else if (keys === 0) idValue = 'button-0';
else idValue = 'button';
return <button type="button" className="button" id={idValue} key={keys} onClick={handleClick}>{keys}</button>;
});
return (
<div className="calculator_container">
<div className="display"><div className="display_string">{display}</div></div>
<div className="button_container">{buttonsArray}</div>
</div>
);
};

export default Calculator;