diff --git a/README.md b/README.md index 71e824daf..f9dfce308 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://DDefty.github.io/react_goods-selector/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index 168716b91..5e542624b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -15,58 +15,97 @@ 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 React.Component<{}, State> { + state = { + selectedGood: 'Jam', + }; - - - - + // Select a good by reading the data-good attribute from the event target + handleSelectGood = (e: React.MouseEvent) => { + const good = e.currentTarget.dataset.good || ''; - - + this.setState({ selectedGood: good }); + }; - - - - - + className="delete ml-3" + onClick={this.handleClearSelection} + /> + ) : null} + - - +
- - - Dumplings -
+ // Clear the current selection + handleClearSelection = () => { + this.setState({ selectedGood: '' }); + }; + + // Remove (clear) selection — kept as a separate method per request + handleRemoveSelection = () => { + this.setState({ selectedGood: '' }); + }; + + render() { + return ( +
+

+ {this.state.selectedGood !== '' ? ( + <>{this.state.selectedGood} is selected + ) : ( + <>No goods selected + )} + {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} + {this.state.selectedGood !== '' ? ( -

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