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
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
141 changes: 96 additions & 45 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,124 @@
import React from 'react';
import React, { useEffect, 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];
type AppProps = {
debounceDelay?: number;
onSelected?: (person: Person) => void;
};

export const App: React.FC<AppProps> = ({
debounceDelay = 300,
onSelected,
}) => {
const [inputValue, setInputValue] = useState('');
const [debouncedValue, setDebouncedValue] = useState('');
const [selectedPerson, setSelectedPerson] = useState<Person | null>(null);
const [isFocused, setIsFocused] = useState(false);

useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(inputValue);
}, debounceDelay);

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

const filteredPeople = React.useMemo(() => {
const trimmedValue = debouncedValue.trim();

if (trimmedValue === '') {
return [];
}

return peopleFromServer.filter(person => {
return person.name.toLowerCase().includes(trimmedValue.toLowerCase());
});
}, [debouncedValue]);

let peopleToShow: Person[] = [];

if (isFocused) {
if (inputValue === '') {
peopleToShow = peopleFromServer;
} else if (filteredPeople.length > 0) {
peopleToShow = filteredPeople;
}
}

const showNoSuggestions =
isFocused && inputValue !== '' && filteredPeople.length === 0;

useEffect(() => {
if (selectedPerson && inputValue !== selectedPerson.name) {
setSelectedPerson(null);
}
}, [inputValue, selectedPerson]);

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 ${isFocused ? 'is-active' : ''}`}
data-cy="dropdown"
>
<div className="dropdown-trigger">
<input
type="text"
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
value={inputValue}
onChange={e => setInputValue(e.target.value)}
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>
{peopleToShow.map(person => (
<div
key={person.name}
className="dropdown-item"
data-cy="suggestion-item"
onMouseDown={() => {
setInputValue(person.name);
setSelectedPerson(person);
onSelected?.(person);
setIsFocused(false);
}}
Comment on lines +92 to +97
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When a person is selected, you also need to call the onSelected prop (which should be destructured from the component's props) with the selected person.

>
<p
className={
person.died ? 'has-text-danger' : '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>
{showNoSuggestions && (
<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