-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Solution #1162
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 #1162
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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { useEffect, useMemo, useState } from 'react'; | ||
| import { Person } from './types/Person'; | ||
| import debounce from 'lodash.debounce'; | ||
|
|
||
| type Props = { | ||
| people: Person[]; | ||
| onSelected: (person: Person | null) => void; | ||
| delay?: number; | ||
| }; | ||
|
|
||
| export const Autocomplete = ({ people, onSelected, delay = 300 }: Props) => { | ||
| const [query, setQuery] = useState(''); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [appliedQuery, setAppliedQuery] = useState(''); | ||
|
|
||
| const normalizedQuery = appliedQuery.trim().toLowerCase(); | ||
|
|
||
| let visiblePeople: Person[]; | ||
|
|
||
| if (normalizedQuery === '') { | ||
| visiblePeople = people; | ||
| } else { | ||
| visiblePeople = people.filter(person => | ||
| person.name.toLowerCase().includes(normalizedQuery), | ||
| ); | ||
| } | ||
|
|
||
| const applyQuery = useMemo(() => { | ||
| return debounce((value: string) => { | ||
| setAppliedQuery(value); | ||
| }, delay); | ||
| }, [delay]); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| applyQuery.cancel(); | ||
| }; | ||
| }, [applyQuery]); | ||
|
|
||
| return ( | ||
| <> | ||
| <div className={isOpen ? 'dropdown is-active' : 'dropdown'}> | ||
| <div className="dropdown-trigger"> | ||
| <input | ||
| type="text" | ||
| placeholder="Enter a part of the name" | ||
| className="input" | ||
| data-cy="search-input" | ||
| value={query} | ||
| onChange={event => { | ||
| const value = event.target.value; | ||
|
|
||
| if (value !== query) { | ||
| onSelected(null); | ||
| } | ||
|
|
||
| setQuery(value); | ||
| onSelected(null); | ||
|
|
||
| if (value.trim() === '') { | ||
| setAppliedQuery(''); | ||
| applyQuery.cancel(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| applyQuery(value); | ||
| }} | ||
| onFocus={() => setIsOpen(true)} | ||
| /> | ||
| </div> | ||
|
|
||
| <div className="dropdown-menu" role="menu" data-cy="suggestions-list"> | ||
| <div className="dropdown-content"> | ||
| {visiblePeople.map(person => ( | ||
| <div | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| key={person.slug} | ||
| onClick={() => { | ||
| setQuery(person.name); | ||
| setAppliedQuery(person.name); | ||
| setIsOpen(false); | ||
| onSelected(person); | ||
| }} | ||
| > | ||
| <p | ||
| className={ | ||
| person.sex === 'm' ? 'has-text-link' : 'has-text-danger' | ||
| } | ||
| > | ||
| {person.name} | ||
| </p> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| {isOpen && visiblePeople.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> | ||
| )} | ||
| </> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| { | ||
| "extends": "@mate-academy/students-ts-config", | ||
| "include": [ | ||
| "src" | ||
| ], | ||
| "compilerOptions": { | ||
| "sourceMap": false, | ||
| "types": ["node", "cypress"] | ||
| } | ||
| "target": "ES2020", | ||
| "useDefineForClassFields": true, | ||
| "lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
| "module": "ESNext", | ||
| "skipLibCheck": true, | ||
| "moduleResolution": "Bundler", | ||
| "allowImportingTsExtensions": false, | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react-jsx", | ||
| "strict": true | ||
| }, | ||
| "include": ["src"] | ||
| } | ||
|
Comment on lines
+15
to
17
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. Checklist item #1 violation: The filter runs for spaces-only input. Add a check in the |
||
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.
Checklist item #1 violation: The filter will run even if user enters spaces only. Add a check like
value.trim() !== ''before callingapplyQuery(value)to prevent filtering on whitespace-only input.