diff --git a/README.md b/README.md index 71e824daf..044e7ec5b 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://wiolip.github.io/react_goods-selector/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index 168716b91..caaf7fc6d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -15,58 +15,86 @@ export const goods = [ 'Garlic', ]; -export const App: React.FC = () => ( -
-

No goods selected

+type State = { + selectedGood: string; +}; -

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

+export class App extends React.Component<{}, State> { + state: State = { + selectedGood: 'Jam', + }; - - - - + handleGoodSelect = (event: React.MouseEvent) => { + const good = event.currentTarget.dataset.good; - - + if (good) { + this.setState({ selectedGood: good }); + } + }; - - - - - + className="delete ml-3" + onClick={this.handleClearSelection} + /> + )} + - - +
- - - Dumplings -
+ handleClearSelection = () => { + this.setState({ selectedGood: '' }); + }; + + render() { + const { selectedGood } = this.state; + + return ( +
+

+ {selectedGood ? `${selectedGood} is selected` : 'No goods selected'} + {selectedGood && ( + // eslint-disable-next-line jsx-a11y/control-has-associated-label -

- Jam -
- -
+ + {goods.map(good => ( + + - - - -
+ {good === selectedGood ? ( + + ) : ( + + )} + - Garlic -
-
-); + + {good} + + + ))} + + + + ); + } +}