-
Notifications
You must be signed in to change notification settings - Fork 858
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
base: master
Are you sure you want to change the base?
Develop #756
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} |
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(''); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename |
||
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> | ||
); | ||
|
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), []); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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> | ||||||
)} | ||||||
</> | ||||||
); | ||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export const App: FC = () => {