-
Notifications
You must be signed in to change notification settings - Fork 1.3k
add solution #1171
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 #1171
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,93 @@ | ||
| import React from 'react'; | ||
| import React, { useMemo, useState } from 'react'; | ||
| import './App.scss'; | ||
| import { peopleFromServer } from './data/people'; | ||
| import { Person } from './types/Person'; | ||
|
|
||
| export const App: React.FC = () => { | ||
| const { name, born, died } = peopleFromServer[0]; | ||
| interface AppProps { | ||
| onSelected?: (person: Person) => void; | ||
|
Comment on lines
+6
to
+7
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. Missing |
||
| } | ||
|
|
||
| export const App: React.FC<AppProps> = ({ onSelected }) => { | ||
| const [query, setQuery] = useState(''); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [selectedPerson, setSelectedPerson] = useState<Person | null>(null); | ||
|
|
||
| const normalizedQuery = query.trim().toLowerCase(); | ||
|
|
||
| const filteredPeople = useMemo(() => { | ||
| if (!normalizedQuery) { | ||
| return peopleFromServer; | ||
| } | ||
|
|
||
| return peopleFromServer.filter(person => | ||
| person.name.toLowerCase().includes(normalizedQuery), | ||
| ); | ||
|
Comment on lines
+20
to
+27
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. Missing debounce implementation - The task explicitly requires: 'suggestions should appear after some |
||
| }, [normalizedQuery]); | ||
|
|
||
| const showNoResults = normalizedQuery !== '' && filteredPeople.length === 0; | ||
|
|
||
| const handleSelect = (person: Person) => { | ||
| setSelectedPerson(person); | ||
| setQuery(person.name); | ||
| setIsOpen(false); | ||
| onSelected?.(person); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <main className="section is-flex is-flex-direction-column"> | ||
| <h1 className="title" data-cy="title"> | ||
| {`${name} (${born} - ${died})`} | ||
| {selectedPerson | ||
| ? `${selectedPerson.name} (${selectedPerson.born} - ${selectedPerson.died})` | ||
| : 'No selected person'} | ||
| </h1> | ||
|
|
||
| <div className="dropdown is-active"> | ||
| <div | ||
| className={`dropdown ${isOpen ? 'is-active' : ''}`} | ||
| data-cy="autocomplete-dropdown" | ||
| > | ||
| <div className="dropdown-trigger"> | ||
| <input | ||
| type="text" | ||
| placeholder="Enter a part of the name" | ||
| className="input" | ||
| data-cy="search-input" | ||
| value={query} | ||
| onFocus={() => setIsOpen(true)} | ||
| onChange={event => { | ||
| setQuery(event.target.value); | ||
| setSelectedPerson(null); | ||
| }} | ||
| onBlur={() => setIsOpen(false)} | ||
| /> | ||
| </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> | ||
| {filteredPeople.map(person => ( | ||
| <div | ||
| key={person.slug} | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| onMouseDown={() => handleSelect(person)} | ||
| > | ||
| <p className="has-text-link">{person.name}</p> | ||
| </div> | ||
| ))} | ||
| </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> | ||
| {showNoResults && ( | ||
| <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> | ||
| )} | ||
| </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
delayprop is referenced but not implemented. Add a customizabledelayprop with a default value of 300ms for the debounce functionality as required.