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

Develop #756

Open
wants to merge 2 commits 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 @@ -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://YuriiMykhailenko.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
4 changes: 4 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
iframe {
display: none;
}

.dropdown-item:hover{
background-color: rgb(117, 172, 243);
}
85 changes: 26 additions & 59 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,40 @@
import React from 'react';
import './App.scss';

import React, { useMemo, useState } from 'react';

import { peopleFromServer } from './data/people';
import { Person } from './types/Person';
import { DropDownMenu } from './Components/DropDownMenu';

export const App: React.FC = () => {
const { name, born, died } = peopleFromServer[0];
const [chosenPerson, setChosenPerson] = useState<Person | null>(null);
const [appliedQuery, setAppliedQuery] = useState('');
Comment on lines 9 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export const App: FC = () => {


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename appliedQuery to searchQuery for clearer intent. This enhances readability and aligns better with related variables in your codebase.

const filteredPeople = useMemo(
() =>
peopleFromServer.filter(person => {
const normalisedQuery = appliedQuery.toLowerCase();
const normalisedPersonName = person.name.toLowerCase();

return normalisedPersonName.includes(normalisedQuery);
}),
[appliedQuery],
);

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

<div className="dropdown is-active">
<div className="dropdown-trigger">
<input
type="text"
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
/>
</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>
</div>

<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>
<DropDownMenu
changeChosenPerson={setChosenPerson}
changeAppliedQuery={setAppliedQuery}
people={filteredPeople}
/>
</main>
</div>
);
Expand Down
94 changes: 94 additions & 0 deletions src/Components/DropDownMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Person } from '../types/Person';

import React, { FC, useCallback, useState } from 'react';
import debounce from 'lodash.debounce';
import cn from 'classnames';

interface Props {
people: Person[];
delay?: number;
changeChosenPerson: (person: Person | null) => void;
changeAppliedQuery: (value: string) => void;
}

export const DropDownMenu: FC<Props> = ({
people,
delay = 300,
changeChosenPerson,
changeAppliedQuery,
}) => {
const [query, setQuery] = useState('');
const [isFocused, setIsFocused] = useState(false);

const applyQuery = useCallback(debounce(changeAppliedQuery, delay), []);

Check warning on line 23 in src/Components/DropDownMenu.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can include changeAppliedQuery and delay in the dependency array, this ensures that the debounce function updates correctly if changeAppliedQuery or delay changes.

Suggested change
const applyQuery = useCallback(debounce(changeAppliedQuery, delay), []);
const applyQuery = useCallback(debounce(changeAppliedQuery, delay), [changeAppliedQuery, delay]);


const handlePersonSelection = (person: Person) => {
changeChosenPerson(person);
setQuery(person.name);
setIsFocused(false);
};

const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value);
changeChosenPerson(null);
applyQuery(event.target.value.trim());
};

return (
<>
<div className="dropdown is-active">
<div className="dropdown-trigger">
<input
type="text"
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
value={query}
onChange={handleQueryChange}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
/>
</div>

{isFocused && people.length && (
<div className="dropdown-menu" role="menu" data-cy="suggestions-list">
<div className="dropdown-content">
{people.map(person => (
<div
className="dropdown-item"
data-cy="suggestion-item"
key={person.slug}
onMouseDown={() => handlePersonSelection(person)}
>
<p
className={cn('has-text-link', {
'has-text-danger': person.sex === 'f',
})}
>
{person.name}
</p>
</div>
))}
</div>
</div>
)}
</div>

{!people.length && (
<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>
)}
</>
);
};
Loading