-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Added solution #1137
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
base: master
Are you sure you want to change the base?
Added solution #1137
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,100 @@ | ||
| import { Person } from '../types/Person'; | ||
| import React, { useCallback, useMemo } from 'react'; | ||
| import debounce from 'lodash.debounce'; | ||
| import classNames from 'classnames'; | ||
|
|
||
| type Props = { | ||
| people: Person[]; | ||
| delay?: number; | ||
| onSelected?: (person: Person | null) => void; | ||
| }; | ||
|
|
||
| export const UserSelect = ({ | ||
| people, | ||
| delay = 300, | ||
| onSelected = () => {}, | ||
| }: Props) => { | ||
| const [query, setQuery] = React.useState(''); | ||
| const [appliedQuery, setAppliedQuery] = React.useState(''); | ||
| const [isDropdownOpen, setIsDropdownOpen] = React.useState(false); | ||
|
|
||
| const applyQuery = useCallback(debounce(setAppliedQuery, delay), [delay]); | ||
|
|
||
| const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| setQuery(event.target.value); | ||
| applyQuery(event.target.value); | ||
| onSelected(null); | ||
| }; | ||
|
|
||
| const filterPeople = useMemo(() => { | ||
| if (!appliedQuery.trim()) { | ||
| return people; | ||
| } | ||
|
|
||
| return people.filter(person => person.name.includes(appliedQuery)); | ||
| }, [people, appliedQuery]); | ||
|
|
||
| return ( | ||
| <> | ||
| <div className={classNames('dropdown', { 'is-active': isDropdownOpen })}> | ||
| <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={() => setIsDropdownOpen(true)} | ||
| onBlur={() => setIsDropdownOpen(false)} | ||
| /> | ||
|
Comment on lines
+41
to
+50
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. For a more intuitive user experience, consider adding an |
||
| </div> | ||
|
|
||
| {isDropdownOpen && ( | ||
| <div className="dropdown-menu" role="menu" data-cy="suggestions-list"> | ||
| <div className="dropdown-content"> | ||
| {filterPeople.map(person => ( | ||
| <div | ||
| className="dropdown-item" | ||
| key={person.slug} | ||
| data-cy="suggestion-item" | ||
| onClick={() => { | ||
| onSelected(person); | ||
| setQuery(person.name); | ||
| setAppliedQuery(person.name); | ||
| setIsDropdownOpen(false); | ||
| }} | ||
|
Comment on lines
+61
to
+66
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 requirement to 'close the list' after selecting an item is not met. You should update the state to close the dropdown here. Additionally,
Comment on lines
+61
to
+66
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 dropdown list should close after a user selects an item, as required by the task. You can achieve this by updating the state that controls the dropdown's visibility inside this |
||
| > | ||
| <p | ||
| className={classNames({ | ||
| 'has-text-link': person.sex === 'm', | ||
| 'has-text-danger': person.sex === 'f', | ||
| })} | ||
| > | ||
| {person.name} | ||
| </p> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| {filterPeople.length === 0 && appliedQuery.trim().length > 0 && ( | ||
| <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.
The
onBlurhandler here causes a problem: when you click on a suggestion, thisonBlurevent fires before the suggestion'sonClickevent on line 61. This closes the dropdown, preventing theonClickfrom ever firing, which means suggestions can't be selected.As mentioned in the previous review, replacing
onClickwithonMouseDownon the suggestion items will fix this becausemousedownfires beforeblur.