-
Notifications
You must be signed in to change notification settings - Fork 1.3k
add solution #1156
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?
add solution #1156
Changes from 1 commit
10f05cb
0d89c20
49a15c6
4aa5fb1
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,106 @@ | ||
| import React, { useEffect, useRef, useState } from 'react'; | ||
| import { Person } from '../types/Person'; | ||
| import classNames from 'classnames'; | ||
|
|
||
| type Props = { | ||
| people: Person[]; | ||
| delay?: number; | ||
| onSelected: (person: Person | null) => void; | ||
| }; | ||
|
|
||
| export const Autocomplete: React.FC<Props> = ({ | ||
| people, | ||
| delay = 300, | ||
| onSelected, | ||
|
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 project instructions say: "Don't remove the title currently uses
|
||
| }) => { | ||
| const [query, setQuery] = useState(''); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [suggestions, setSuggestions] = useState<Person[]>(people); | ||
| const prevQuery = useRef(''); | ||
|
|
||
| useEffect(() => { | ||
| const timeout = setTimeout(() => { | ||
| if (prevQuery.current === query) { | ||
| return; | ||
| } | ||
|
|
||
| prevQuery.current = query; | ||
|
|
||
| if (!query.trim()) { | ||
| setSuggestions(people); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const filtered = people.filter(person => | ||
| person.name.toLowerCase().includes(query.toLowerCase()), | ||
| ); | ||
|
|
||
| setSuggestions(filtered); | ||
| }, delay); | ||
|
|
||
| return () => clearTimeout(timeout); | ||
| }, [query, delay, people]); | ||
|
|
||
| const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| const value = event.target.value; | ||
|
|
||
| setQuery(value); | ||
| onSelected(null); | ||
| }; | ||
|
|
||
| const onFocus = () => { | ||
| setIsOpen(true); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={classNames('dropdown', { 'is-active': isOpen })}> | ||
| <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={onFocus} | ||
|
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 internal event handler is named |
||
| /> | ||
| </div> | ||
|
|
||
| <div className="dropdown-menu" role="menu" data-cy="suggestions-list"> | ||
| <div className="dropdown-content"> | ||
| {suggestions.map((human: Person) => ( | ||
| <div | ||
| key={human.name + human.born} | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| onMouseDown={() => { | ||
| setQuery(human.name); | ||
| setIsOpen(false); | ||
| onSelected(human); | ||
| }} | ||
| > | ||
| <p className="has-text-link">{human.name}</p> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
|
|
||
| {query.trim() && suggestions.length === 0 && ( | ||
| <div | ||
| className=" | ||
| notification | ||
| is-danger | ||
| is-light | ||
| mt-3 | ||
| is-align-self-flex-start | ||
| " | ||
| role="alert" | ||
| data-cy="no-suggestions-message" | ||
|
Comment on lines
+63
to
+99
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. This file uses |
||
| > | ||
| <p className="has-text-danger">No matching suggestions</p> | ||
| </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.
The app's title uses
data-cy="title". Tests and the task description requiredata-qaattributes. Please replace the attribute withdata-qa="title". (Requirement: "Don't remove thedata-qaattributes. It is required for tests.")