-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Solution #1172
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 #1172
Changes from all 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,113 @@ | ||
| import React from 'react'; | ||
| import React, { | ||
| useCallback, | ||
| useEffect, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from 'react'; | ||
| import './App.scss'; | ||
| import { peopleFromServer } from './data/people'; | ||
| import { Person } from './types/Person'; | ||
| import { Dropdown } from './components/Dropdown/Dropdown'; | ||
| import { useDebounce } from './hooks/useDebounce'; | ||
|
|
||
| function filteredPerson(people: Person[], query: string): Person[] { | ||
| const queryLowerCase = query.trim().toLowerCase(); | ||
|
|
||
| if (query !== '') { | ||
|
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. i assume the check should be in your current functionality you are trimming the |
||
| return people.filter(person => | ||
| person.name.toLowerCase().includes(queryLowerCase), | ||
| ); | ||
| } | ||
|
|
||
| return people; | ||
| } | ||
|
|
||
| export const App: React.FC = () => { | ||
| const { name, born, died } = peopleFromServer[0]; | ||
| const [search, setSearch] = useState(''); | ||
| const [selectedPerson, setSelectedPerson] = useState<Person | null>(null); | ||
| const [visibleDropdown, setVisibleDropdown] = useState(false); | ||
| const debounceValue = useDebounce(search); | ||
| const visiblePeople = useMemo(() => { | ||
| return filteredPerson(peopleFromServer, debounceValue); | ||
| }, [debounceValue]); | ||
| const refDropDown = useRef<HTMLDivElement | null>(null); | ||
|
|
||
| const handleChoosePerson = useCallback((person: Person) => { | ||
| setSelectedPerson(person); | ||
| setSearch(person.name); | ||
| setVisibleDropdown(false); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!visibleDropdown) { | ||
| return; | ||
| } | ||
|
|
||
| function handleClick(event: MouseEvent) { | ||
| if ( | ||
| !refDropDown.current || | ||
| refDropDown.current.contains(event.target as Node) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| setVisibleDropdown(false); | ||
| } | ||
|
|
||
| document.addEventListener('mousedown', handleClick); | ||
|
|
||
| return () => { | ||
| document.removeEventListener('mousedown', handleClick); | ||
| }; | ||
| }, [visibleDropdown]); | ||
| const personData = `${selectedPerson?.name} (${selectedPerson?.born} - ${selectedPerson?.died})`; | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <main className="section is-flex is-flex-direction-column"> | ||
| <h1 className="title" data-cy="title"> | ||
| {`${name} (${born} - ${died})`} | ||
| {selectedPerson ? personData : 'No selected person'} | ||
| </h1> | ||
|
|
||
| <div className="dropdown is-active"> | ||
| <div className="dropdown is-active" ref={refDropDown}> | ||
| <div className="dropdown-trigger"> | ||
| <input | ||
| value={search} | ||
| onChange={e => { | ||
| setSearch(e.target.value); | ||
| if (selectedPerson) { | ||
| setSelectedPerson(null); | ||
| } | ||
| }} | ||
|
Comment on lines
+77
to
+82
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. inline event handlers that exceeds one line should be moved into named handlers |
||
| type="text" | ||
| placeholder="Enter a part of the name" | ||
| className="input" | ||
| data-cy="search-input" | ||
| onFocus={() => setVisibleDropdown(true)} | ||
| /> | ||
| </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> | ||
| </div> | ||
| </div> | ||
| <Dropdown | ||
| visible={visibleDropdown} | ||
| people={visiblePeople} | ||
| onSelected={handleChoosePerson} | ||
| /> | ||
| </div> | ||
|
|
||
| <div | ||
| className=" | ||
| {visibleDropdown && !visiblePeople.length && ( | ||
| <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> | ||
| role="alert" | ||
| data-cy="no-suggestions-message" | ||
| > | ||
| <p className="has-text-danger">No matching suggestions</p> | ||
| </div> | ||
| )} | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { memo } from 'react'; | ||
| import { Person } from '../../types/Person'; | ||
|
|
||
| type Prop = { | ||
| visible: boolean; | ||
|
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. it's common practice to name boolean consts, props, arguments using prefixes like |
||
| people: Person[]; | ||
| onSelected: (person: Person) => void; | ||
| }; | ||
| export const Dropdown = memo(function Dropdown({ | ||
|
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. it looks like unnecessary memoization |
||
| visible, | ||
| people, | ||
| onSelected, | ||
| }: Prop) { | ||
| return ( | ||
| <> | ||
| {visible && people.length > 0 && ( | ||
| <div className="dropdown-menu" role="menu" data-cy="suggestions-list"> | ||
| <div className="dropdown-content"> | ||
| {people.map(item => ( | ||
| <button | ||
| type="button" | ||
| role="menuitem" | ||
| key={item.slug} | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| onClick={() => onSelected(item)} | ||
| > | ||
| <p | ||
| className={`${item.sex === 'f' ? 'has-text-danger' : 'has-text-link'}`} | ||
|
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. use |
||
| > | ||
| {item.name} | ||
| </p> | ||
| </button> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| )} | ||
| </> | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| export function useDebounce<T>(value: T, delay: number = 500) { | ||
| const [debounceValue, setDebounceValue] = useState(value); | ||
|
|
||
| useEffect(() => { | ||
| const timer = setTimeout(() => { | ||
| setDebounceValue(value); | ||
| }, delay); | ||
|
|
||
| return () => clearTimeout(timer); | ||
| }); | ||
|
|
||
| return debounceValue; | ||
| } |
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.
bad function naming, function names usually should indicate an action. in your case it's named like it is a filtered person and not an action