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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task solution #1770

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ You are given an array of goods. Render them in a table with the ability to sele
- 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 `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_goods-selector/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://W3zzie.github.io/react_goods-selector/) and add it to the PR description.
112 changes: 65 additions & 47 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import { Component } from 'react';
import 'bulma/css/bulma.css';
import './App.scss';

Expand All @@ -15,58 +15,76 @@ export const goods = [
'Garlic',
];

export const App: React.FC = () => (
<main className="section container">
<h1 className="title">No goods selected</h1>
interface State {
selectedGood: string;
}

<h1 className="title is-flex is-align-items-center">
Jam is selected
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button data-cy="ClearButton" type="button" className="delete ml-3" />
</h1>
export class App extends Component<{}, State> {
state = {
selectedGood: 'Jam',
};

<table className="table">
<tbody>
<tr data-cy="Good">
<td>
<button data-cy="AddButton" type="button" className="button">
+
</button>
</td>
handleClick = (isCurrent: boolean, good: string) => {
const nextGood = isCurrent ? '' : good;
this.setState({ selectedGood: nextGood });
};

<td data-cy="GoodTitle" className="is-vcentered">
Dumplings
</td>
</tr>
handleClearClick = () => {
this.setState({ selectedGood: ''});
};

<tr data-cy="Good" className="has-background-success-light">
<td>
render() {
const { selectedGood } = this.state;

return (
<main className="section container">
{selectedGood.length === 0 ? (
<h1 className="title">No goods selected</h1>
) : (
<h1 className="title is-flex is-align-items-center">
{selectedGood} is selected
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
data-cy="RemoveButton"
data-cy="ClearButton"
type="button"
className="button is-info"
>
-
</button>
</td>
className="delete ml-3"
onClick={this.handleClearClick}
/>
</h1>
)}

<td data-cy="GoodTitle" className="is-vcentered">
Jam
</td>
</tr>
<table className="table">
<tbody>
{goods.map((good: string) => {
const isCurrent = good === selectedGood;
const buttonClass = 'button' + (isCurrent ? ' is-info' : '');

<tr data-cy="Good">
<td>
<button data-cy="AddButton" type="button" className="button">
+
</button>
</td>
return (
<tr
data-cy="Good"
className={isCurrent ? 'has-background-success-light' : ''}
key={good}
>
<td>
<button
data-cy={isCurrent ? 'RemoveButton' : 'AddButton'}
type="button"
className={buttonClass}
onClick={() => this.handleClick(isCurrent, good)}
>
{isCurrent ? '-' : '+'}
</button>
</td>

<td data-cy="GoodTitle" className="is-vcentered">
Garlic
</td>
</tr>
</tbody>
</table>
</main>
);
<td data-cy="GoodTitle" className="is-vcentered">
{good}
</td>
</tr>
);
})}
</tbody>
</table>
</main>
);
}
}
Loading