Skip to content
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 @@ -18,7 +18,7 @@ that will suggest people matching an entered text.
- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- 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).
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_autocomplete/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://TetinaSobolieva.github.io/react_autocomplete/) and add it to the PR description.
- Don't remove the `data-qa` attributes. It is required for tests.

## Troubleshooting
Expand Down
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@faker-js/faker": "^8.4.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/lodash.debounce": "^4.0.9",
Expand Down
99 changes: 54 additions & 45 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import './App.scss';
import { peopleFromServer } from './data/people';
import { SuggestionList } from './component/SuggestionList';
import { Person } from './types/Person';

export const App: React.FC = () => {
const { name, born, died } = peopleFromServer[0];
const [inputValue, setInputValue] = useState('');
const [query, setQuery] = useState('');
const [isFocused, setIsFocused] = useState(false);
const [selectedPerson, setSelectedPerson] = useState<Person | null>(null);

useEffect(() => {
const timeout = setTimeout(() => {
setQuery(inputValue);
}, 300);

return () => clearTimeout(timeout);
}, [inputValue]);

const filteredPeople = peopleFromServer.filter(person =>
person.name.toLowerCase().includes(query.toLowerCase()),
);

return (
<div className="container">
<main className="section is-flex is-flex-direction-column">
<h1 className="title" data-cy="title">
{`${name} (${born} - ${died})`}
</h1>
{selectedPerson ? (
<h1 className="title" data-cy="title">
{`${selectedPerson.name} (${selectedPerson.born} - ${selectedPerson.died})`}
</h1>
) : (
<h1 className="title" data-cy="title">
No selected person
</h1>
)}

<div className="dropdown is-active">
<div className="dropdown-trigger">
Expand All @@ -19,55 +42,41 @@ export const App: React.FC = () => {
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
value={inputValue}
onChange={e => {
setInputValue(e.target.value);
setSelectedPerson(null);
}}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
/>
</div>

<div className="dropdown-menu" role="menu" data-cy="suggestions-list">
<div className="dropdown-content">
<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter Bernard Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter Antone Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-danger">Elisabeth Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter de Decker</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-danger">Petronella de Decker</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-danger">Elisabeth Hercke</p>
</div>
</div>
</div>
{isFocused && (
<SuggestionList
people={filteredPeople}
onSelect={person => {
setSelectedPerson(person);
setInputValue(person.name);
setIsFocused(false);
}}
/>
)}
</div>

<div
className="
{filteredPeople.length === 0 && (
<div
className="
notification
is-danger
is-light
mt-3
is-align-self-flex-start
"
role="alert"
data-cy="no-suggestions-message"
>
<p className="has-text-danger">No matching suggestions</p>
</div>
role="alert"
data-cy="no-suggestions-message"
>
<p className="has-text-danger">No matching suggestions</p>
</div>
)}
</main>
</div>
);
Expand Down
26 changes: 26 additions & 0 deletions src/component/PersonInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import classNames from 'classnames';
import { Person } from '../types/Person';

type Props = {
person: Person;
onSelect: (person: Person) => void;
};

export const PersonInfo: React.FC<Props> = ({ person, onSelect }) => {
return (
<div
className="dropdown-item"
data-cy="suggestion-item"
onMouseDown={() => onSelect(person)}
>
<p
className={classNames(
{ 'has-text-link': person.sex === 'f' },
{ 'has-text-danger': person.sex === 'm' },
)}
>
{person.name}
</p>
</div>
);
};
19 changes: 19 additions & 0 deletions src/component/SuggestionList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Person } from '../types/Person';
import { PersonInfo } from './PersonInfo';

type Props = {
people: Person[];
onSelect: (person: Person) => void;
};

export const SuggestionList: React.FC<Props> = ({ people, onSelect }) => {
return (
<div className="dropdown-menu" role="menu" data-cy="suggestions-list">
<div className="dropdown-content">
{people.map(person => (
<PersonInfo key={person.name} person={person} onSelect={onSelect} />
))}
</div>
</div>
);
};
Loading