-
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
add solution #760
base: master
Are you sure you want to change the base?
add solution #760
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.dropdown-item:hover { | ||
cursor: pointer; | ||
background-color: #e5e5e5; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import classNames from 'classnames'; | ||
import { Person } from '../types/Person'; | ||
import { useMemo, useState } from 'react'; | ||
import debounce from 'lodash.debounce'; | ||
import './Autocomplete.scss'; | ||
|
||
type Props = { | ||
people: Person[]; | ||
onSelected: (person: Person | null) => void; | ||
delay?: number; | ||
}; | ||
|
||
export const Autocomplete: React.FC<Props> = ({ | ||
people, | ||
onSelected = () => {}, | ||
delay = 300, | ||
}) => { | ||
const [focusOnInput, setFocusOnInput] = useState(false); | ||
const [appliedQuery, setAppliedQuery] = useState(''); | ||
const [query, setQuery] = useState(''); | ||
|
||
const applyQuery = debounce(setAppliedQuery, delay); | ||
|
||
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = event.target.value; | ||
|
||
setQuery(value); | ||
applyQuery(value); | ||
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 are not checking if the user entered spaces only. This can lead to unnecessary function calls. You should add a condition to check if the |
||
setFocusOnInput(true); | ||
onSelected(null); | ||
}; | ||
|
||
const handleSelectPerson = (person: Person) => { | ||
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. The naming convention for this method could be improved. According to the conventions, it's better to name the function |
||
onSelected(person); | ||
setFocusOnInput(false); | ||
setQuery(person.name); | ||
}; | ||
|
||
const filteredPeople = useMemo(() => { | ||
return people.filter(person => | ||
person.name | ||
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 are directly interacting with DOM here. It's recommended to use React as much as possible. You can use the |
||
.toLowerCase() | ||
.trim() | ||
.includes(appliedQuery.toLowerCase().trim()), | ||
); | ||
}, [appliedQuery, people]); | ||
|
||
return ( | ||
<div | ||
className={classNames('dropdown', { | ||
'is-active': focusOnInput, | ||
})} | ||
> | ||
<div className="dropdown-trigger"> | ||
<input | ||
type="text" | ||
placeholder="Enter a part of the name" | ||
className="input" | ||
value={query} | ||
data-cy="search-input" | ||
onFocus={() => setFocusOnInput(true)} | ||
onBlur={() => setFocusOnInput(false)} | ||
onChange={handleInputChange} | ||
/> | ||
</div> | ||
|
||
{focusOnInput && ( | ||
<div className="dropdown-menu" role="menu" data-cy="suggestions-list"> | ||
<div className="dropdown-content"> | ||
{filteredPeople.length > 0 ? ( | ||
filteredPeople.map(person => ( | ||
<div | ||
key={person.slug} | ||
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 are generating a key on render. This can lead to performance issues. It's recommended to generate keys during the creation of items. Here is a link explaining why. |
||
className="dropdown-item" | ||
data-cy="suggestion-item" | ||
onMouseDown={() => handleSelectPerson(person)} | ||
> | ||
<p | ||
className={classNames('has-text-link', { | ||
'has-text-danger': person.sex === 'f', | ||
})} | ||
> | ||
{person.name} | ||
</p> | ||
</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> | ||
)} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.dropdown-item:hover { | ||
cursor: pointer; | ||
background-color: #e5e5e5; | ||
} |
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.
The naming convention for this method could be improved. According to the conventions, it's better to name the function
onInputChange
instead ofhandleInputChange
.