-
Notifications
You must be signed in to change notification settings - Fork 1.3k
develop #1175
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?
develop #1175
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import React, { useEffect, useMemo, useState } from 'react'; | ||
| import { Person } from '../types/Person'; | ||
|
|
||
| type Props = { | ||
| people: Person[]; | ||
| delay?: number; | ||
| onSelected: (person: Person | null) => void; | ||
| }; | ||
|
|
||
| export const Autocomplete: React.FC<Props> = ({ | ||
| people, | ||
| delay = 300, | ||
| onSelected, | ||
| }) => { | ||
| const [query, setQuery] = useState(''); | ||
| const [appliedQuery, setAppliedQuery] = useState(''); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| const timerId = window.setTimeout(() => { | ||
| setAppliedQuery(query); | ||
| }, delay); | ||
|
|
||
| return () => { | ||
| window.clearTimeout(timerId); | ||
| }; | ||
| }, [query, delay]); | ||
|
|
||
| const visiblePeople = useMemo(() => { | ||
| return people.filter(person => | ||
| person.name.toLowerCase().includes(appliedQuery.toLowerCase()), | ||
| ); | ||
| }, [people, appliedQuery]); | ||
|
|
||
| const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| setQuery(event.target.value); | ||
| setIsOpen(true); | ||
| onSelected(null); | ||
|
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 selection clearing is too aggressive. Currently |
||
| }; | ||
|
|
||
| const handleSelect = (person: Person) => { | ||
| setQuery(person.name); | ||
| setAppliedQuery(person.name); | ||
| setIsOpen(false); | ||
| onSelected(person); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={`dropdown ${isOpen ? '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={handleChange} | ||
| onFocus={() => setIsOpen(true)} | ||
| onBlur={() => setTimeout(() => setIsOpen(false), 200)} | ||
| /> | ||
| </div> | ||
|
|
||
| {isOpen && ( | ||
| <div className="dropdown-menu" role="menu" data-cy="suggestions-list"> | ||
| <div className="dropdown-content"> | ||
| {visiblePeople.length > 0 ? ( | ||
| visiblePeople.map(person => ( | ||
| <div | ||
| key={person.slug} | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| onClick={() => handleSelect(person)} | ||
| > | ||
| <p className="has-text-link">{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> | ||
| ); | ||
| }; | ||
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.
This violates checklist item #1: filter is called when user enters spaces only. The filter runs with whitespace (
appliedQuery.toLowerCase().includes(' ')) instead of skipping the filter when input is only whitespace. Consider checking ifappliedQuery.trim()is empty and returning all people in that case.