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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ that will suggest people matching an entered text.
- when the selected person is displayed in the title, but the value in the input changes, the selected person should be cleared and `No selected person` should be shown.

## Instructions

- 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://OlehYuriev.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
128 changes: 84 additions & 44 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,113 @@
import React from 'react';
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import './App.scss';
import { peopleFromServer } from './data/people';
import { Person } from './types/Person';
import { Dropdown } from './components/Dropdown/Dropdown';
import { useDebounce } from './hooks/useDebounce';

function filteredPerson(people: Person[], query: string): Person[] {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bad function naming, function names usually should indicate an action. in your case it's named like it is a filtered person and not an action

const queryLowerCase = query.trim().toLowerCase();

if (query !== '') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

i assume the check should be queryLowerCase !== '' instead.

in your current functionality you are trimming the queryLowerCase but checking query which means that if query has empty spaces like ' ' it would be valid for if condition

return people.filter(person =>
person.name.toLowerCase().includes(queryLowerCase),
);
}

return people;
}

export const App: React.FC = () => {
const { name, born, died } = peopleFromServer[0];
const [search, setSearch] = useState('');
const [selectedPerson, setSelectedPerson] = useState<Person | null>(null);
const [visibleDropdown, setVisibleDropdown] = useState(false);
const debounceValue = useDebounce(search);
const visiblePeople = useMemo(() => {
return filteredPerson(peopleFromServer, debounceValue);
}, [debounceValue]);
const refDropDown = useRef<HTMLDivElement | null>(null);

const handleChoosePerson = useCallback((person: Person) => {
setSelectedPerson(person);
setSearch(person.name);
setVisibleDropdown(false);
}, []);

useEffect(() => {
if (!visibleDropdown) {
return;
}

function handleClick(event: MouseEvent) {
if (
!refDropDown.current ||
refDropDown.current.contains(event.target as Node)
) {
return;
}

setVisibleDropdown(false);
}

document.addEventListener('mousedown', handleClick);

return () => {
document.removeEventListener('mousedown', handleClick);
};
}, [visibleDropdown]);
const personData = `${selectedPerson?.name} (${selectedPerson?.born} - ${selectedPerson?.died})`;

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

<div className="dropdown is-active">
<div className="dropdown is-active" ref={refDropDown}>
<div className="dropdown-trigger">
<input
value={search}
onChange={e => {
setSearch(e.target.value);
if (selectedPerson) {
setSelectedPerson(null);
}
}}
Comment on lines +77 to +82
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

inline event handlers that exceeds one line should be moved into named handlers

type="text"
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
onFocus={() => setVisibleDropdown(true)}
/>
</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>
<Dropdown
visible={visibleDropdown}
people={visiblePeople}
onSelected={handleChoosePerson}
/>
</div>

<div
className="
{visibleDropdown && !visiblePeople.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>
role="alert"
data-cy="no-suggestions-message"
>
<p className="has-text-danger">No matching suggestions</p>
</div>
)}
</main>
</div>
);
Expand Down
40 changes: 40 additions & 0 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { memo } from 'react';
import { Person } from '../../types/Person';

type Prop = {
visible: boolean;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

it's common practice to name boolean consts, props, arguments using prefixes like has, is, are. e.g. hasAccess, areEquals etc

people: Person[];
onSelected: (person: Person) => void;
};
export const Dropdown = memo(function Dropdown({
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

it looks like unnecessary memoization

visible,
people,
onSelected,
}: Prop) {
return (
<>
{visible && people.length > 0 && (
<div className="dropdown-menu" role="menu" data-cy="suggestions-list">
<div className="dropdown-content">
{people.map(item => (
<button
type="button"
role="menuitem"
key={item.slug}
className="dropdown-item"
data-cy="suggestion-item"
onClick={() => onSelected(item)}
>
<p
className={`${item.sex === 'f' ? 'has-text-danger' : 'has-text-link'}`}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

use classnames for conditional classes

>
{item.name}
</p>
</button>
))}
</div>
</div>
)}
</>
);
});
15 changes: 15 additions & 0 deletions src/hooks/useDebounce.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useState } from 'react';

export function useDebounce<T>(value: T, delay: number = 500) {
const [debounceValue, setDebounceValue] = useState(value);

useEffect(() => {
const timer = setTimeout(() => {
setDebounceValue(value);
}, delay);

return () => clearTimeout(timer);
});

return debounceValue;
}
Loading