diff --git a/README.md b/README.md index 71e824daf..0ade32148 100644 --- a/README.md +++ b/README.md @@ -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 `` with your Github username in the [DEMO LINK](https://.github.io/react_goods-selector/) and add it to the PR description. +- Replace `` with your Github username in the [DEMO LINK](https://W3zzie.github.io/react_goods-selector/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index 168716b91..b8c26c5af 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import { Component } from 'react'; import 'bulma/css/bulma.css'; import './App.scss'; @@ -15,58 +15,76 @@ export const goods = [ 'Garlic', ]; -export const App: React.FC = () => ( -
-

No goods selected

+interface State { + selectedGood: string; +} -

- Jam is selected - {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} -

+export class App extends Component<{}, State> { + state = { + selectedGood: 'Jam', + }; - - - - + handleClick = (isCurrent: boolean, good: string) => { + const nextGood = isCurrent ? '' : good; + this.setState({ selectedGood: nextGood }); + }; - - + handleClearClick = () => { + this.setState({ selectedGood: ''}); + }; - - + className="delete ml-3" + onClick={this.handleClearClick} + /> + + )} - - +
- - - Dumplings -
+ render() { + const { selectedGood } = this.state; + + return ( +
+ {selectedGood.length === 0 ? ( +

No goods selected

+ ) : ( +

+ {selectedGood} is selected + {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} -

- Jam -
+ + {goods.map((good: string) => { + const isCurrent = good === selectedGood; + const buttonClass = 'button' + (isCurrent ? ' is-info' : ''); - - + return ( + + - - - -
- -
+ + - Garlic -
-
-); + + {good} + + + ); + })} + + + + ); + } +}