Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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://ZhylaRoman.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
2 changes: 1 addition & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { defineConfig } = require('cypress');

module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:3000',
baseUrl: 'http://localhost:3005',
specPattern: 'cypress/integration/**/*.spec.{js,ts,jsx,tsx}',
},
Comment on lines 6 to 7
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The delay prop is referenced but not implemented. Add a customizable delay prop with a default value of 300ms for the debounce functionality as required.

video: true,
Expand Down
110 changes: 65 additions & 45 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,93 @@
import React from 'react';
import React, { useMemo, useState } from 'react';
import './App.scss';
import { peopleFromServer } from './data/people';
import { Person } from './types/Person';

export const App: React.FC = () => {
const { name, born, died } = peopleFromServer[0];
interface AppProps {
onSelected?: (person: Person) => void;
Comment on lines +6 to +7
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing delay prop - The task requires 'the delay prop should be customizable via props (default value is 300ms)'. There's no delay prop defined in the AppProps interface.

}

export const App: React.FC<AppProps> = ({ onSelected }) => {
const [query, setQuery] = useState('');
const [isOpen, setIsOpen] = useState(false);
const [selectedPerson, setSelectedPerson] = useState<Person | null>(null);

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

const filteredPeople = useMemo(() => {
if (!normalizedQuery) {
return peopleFromServer;
}

return peopleFromServer.filter(person =>
person.name.toLowerCase().includes(normalizedQuery),
);
Comment on lines +20 to +27
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing debounce implementation - The task explicitly requires: 'suggestions should appear after some delay in typing (debounce)' and 'the delay should be customizable via props (default value is 300ms)'. Currently, filtering happens immediately on each keystroke with no delay.

}, [normalizedQuery]);

const showNoResults = normalizedQuery !== '' && filteredPeople.length === 0;

const handleSelect = (person: Person) => {
setSelectedPerson(person);
setQuery(person.name);
setIsOpen(false);
onSelected?.(person);
};

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

<div className="dropdown is-active">
<div
className={`dropdown ${isOpen ? 'is-active' : ''}`}
data-cy="autocomplete-dropdown"
>
<div className="dropdown-trigger">
<input
type="text"
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
value={query}
onFocus={() => setIsOpen(true)}
onChange={event => {
setQuery(event.target.value);
setSelectedPerson(null);
}}
onBlur={() => setIsOpen(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>
{filteredPeople.map(person => (
<div
key={person.slug}
className="dropdown-item"
data-cy="suggestion-item"
onMouseDown={() => handleSelect(person)}
>
<p className="has-text-link">{person.name}</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>
{showNoResults && (
<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>
)}
</main>
</div>
);
Expand Down
Loading