-
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 2 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 |
|---|---|---|
| @@ -1,73 +1,114 @@ | ||
| import React from 'react'; | ||
| import React, { useEffect, 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 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})`; | ||
|
|
||
| // ✅ debounce + filtering (SINGLE SOURCE OF TRUTH) | ||
| useEffect(() => { | ||
| 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 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> | ||
| {suggestions.map(person => ( | ||
| <a | ||
| key={person.name} | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| onMouseDown={() => { | ||
| setSelectedPerson(person); | ||
| setQuery(person.name); | ||
| setIsOpen(false); | ||
|
|
||
| <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> | ||
| 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=" | ||
| 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> | ||
| {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
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.
The effect unconditionally calls
setSuggestions(filteredPeople(query))wheneverquerychanges. This can re-run filtering even when the trimmed query hasn't actually changed (for example when only trailing spaces are typed or when the visible text is identical), which violates the requirement: "Do not run the filtering logic again if the text value has not changed since the last filtering (e.g., when there is just a pause in typing with identical text)." Consider storing the last trimmed query in a ref and skipping the filtering when the new trimmed query equals the last one, or set/reset a selection guard ref when selecting a suggestion so the effect exits early.