-
Notifications
You must be signed in to change notification settings - Fork 1.3k
solution #1157
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?
solution #1157
Changes from 1 commit
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,73 +1,116 @@ | ||
| import React from 'react'; | ||
| import React, { useEffect, useRef, useState } from 'react'; | ||
| import './App.scss'; | ||
| import { peopleFromServer } from './data/people'; | ||
|
|
||
| export const App: React.FC = () => { | ||
| const { name, born, died } = peopleFromServer[0]; | ||
| type Person = { | ||
| name: string; | ||
| born: number; | ||
| died: number; | ||
| }; | ||
|
|
||
| type Props = { | ||
| debounceDelay?: number; | ||
| onSelected?: (person: Person) => void; | ||
| }; | ||
|
|
||
| export const App: React.FC<Props> = ({ debounceDelay = 300, onSelected }) => { | ||
| const [query, setQuery] = useState(''); | ||
| const [suggestions, setSuggestions] = useState<Person[]>(peopleFromServer); | ||
| const [selectedPerson, setSelectedPerson] = useState<Person | null>(null); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const isSelectingRef = useRef(false); | ||
|
|
||
| const filteredPeople = (value: string) => { | ||
| const q = value.trim().toLowerCase(); | ||
|
|
||
| if (!q) { | ||
| return peopleFromServer; | ||
| } | ||
|
|
||
| return peopleFromServer.filter(person => | ||
| person.name.toLowerCase().includes(q), | ||
| ); | ||
| }; | ||
|
|
||
| const showTitle = ({ name, born, died }: Person) => | ||
| `${name} (${born} - ${died})`; | ||
|
|
||
| useEffect(() => { | ||
| if (isSelectingRef.current) { | ||
| return; | ||
| } | ||
|
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 effect returns early when |
||
|
|
||
| const timer = setTimeout(() => { | ||
| setSuggestions(filteredPeople(query)); | ||
| }, debounceDelay); | ||
|
|
||
| return () => clearTimeout(timer); | ||
| }, [query, debounceDelay]); | ||
|
Comment on lines
+26
to
+44
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 effect unconditionally calls
Comment on lines
+26
to
+44
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 effect schedules filtering on every |
||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <main className="section is-flex is-flex-direction-column"> | ||
| <h1 className="title" data-cy="title"> | ||
| {`${name} (${born} - ${died})`} | ||
| {selectedPerson ? showTitle(selectedPerson) : 'No selected person'} | ||
| </h1> | ||
|
|
||
| <div className="dropdown is-active"> | ||
| <div className={`dropdown ${isOpen ? 'is-active' : ''}`}> | ||
| <div className="dropdown-trigger"> | ||
| <input | ||
| type="text" | ||
| placeholder="Enter a part of the name" | ||
| className="input" | ||
| placeholder="Enter a part of the name" | ||
| data-cy="search-input" | ||
| value={query} | ||
| onFocus={() => { | ||
| setIsOpen(true); | ||
|
|
||
| if (!query.trim()) { | ||
| setSuggestions(peopleFromServer); | ||
| } | ||
| }} | ||
| onChange={e => { | ||
| setQuery(e.target.value); | ||
| setSelectedPerson(null); | ||
| }} | ||
| /> | ||
| </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> | ||
| {suggestions.map(person => ( | ||
| <a | ||
| key={person.name} | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| onMouseDown={() => { | ||
| setSelectedPerson(person); | ||
| setQuery(person.name); | ||
| setIsOpen(false); | ||
| onSelected?.(person); | ||
| }} | ||
|
Comment on lines
+84
to
+90
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. In the selection handler you should set |
||
| > | ||
| {person.name} | ||
| </a> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div | ||
| className=" | ||
| {suggestions.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> | ||
| is-align-self-flex-start" | ||
| role="alert" | ||
| data-cy="no-suggestions-message" | ||
|
Comment on lines
50
to
+110
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 replaced the required
Comment on lines
50
to
+110
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. Replace these |
||
| > | ||
| <p className="has-text-danger">No matching suggestions</p> | ||
| </div> | ||
| )} | ||
| </main> | ||
| </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.
isSelectingRefis declared but never updated. This makes the ref dead code and the intent (preventing filtering during selection) is not implemented — either set it when selecting or remove/replace this logic.