-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Solution #1152
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
Open
Stan0men
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
Stan0men:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #1152
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| @import 'bulma/css/bulma.min.css'; | ||
|
|
||
| iframe { | ||
| display: none; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { Person } from '../types/Person'; | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| interface Props { | ||
| people: Person[]; | ||
| delay?: number; | ||
| onSelected: (person: Person) => void; | ||
| onInputChange?: () => void; | ||
| } | ||
|
|
||
| export default function Input({ | ||
| people, | ||
| delay = 300, | ||
| onSelected, | ||
| onInputChange, | ||
| }: Props) { | ||
| const [query, setQuery] = useState(''); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [suggestions, setSuggestions] = useState<Person[]>([]); | ||
| const lastTrimmedRef = useRef(''); | ||
|
|
||
| useEffect(() => { | ||
| const trimmed = query.trim().toLowerCase(); | ||
|
|
||
| if (lastTrimmedRef.current === trimmed) { | ||
| return; | ||
| } | ||
|
|
||
| lastTrimmedRef.current = trimmed; | ||
|
|
||
| const timeout = window.setTimeout(() => { | ||
| const nextSuggestions = | ||
| trimmed === '' | ||
| ? people | ||
| : people.filter(pers => pers.name.toLowerCase().includes(trimmed)); | ||
|
|
||
| setSuggestions(nextSuggestions); | ||
| }, delay); | ||
|
|
||
| return () => { | ||
| clearTimeout(timeout); | ||
| }; | ||
| }, [query, delay, people]); | ||
|
|
||
| const handleSelect = (selectedPerson: Person) => { | ||
| setQuery(selectedPerson.name); | ||
| setIsOpen(false); | ||
| onSelected(selectedPerson); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={isOpen ? 'dropdown is-active' : 'dropdown'}> | ||
| <div className="dropdown-trigger"> | ||
| <input | ||
| className="input is-medium" | ||
| type="text" | ||
| placeholder="Enter a part of the name" | ||
| value={query} | ||
| onChange={e => { | ||
| setQuery(e.target.value); | ||
| setIsOpen(true); | ||
| onInputChange?.(); | ||
| }} | ||
| onFocus={() => { | ||
| setIsOpen(true); | ||
| if (query.trim() === '') { | ||
| setSuggestions(people); | ||
| } | ||
| }} | ||
| data-cy="search-input" | ||
| data-qa="search-input" | ||
| /> | ||
| </div> | ||
| <div className="dropdown-menu" id="dropdown-menu" role="menu"> | ||
| <div | ||
| className="dropdown-content" | ||
| data-cy="suggestions-list" | ||
| data-qa="suggestions-list" | ||
| > | ||
| {query.trim() !== '' && suggestions.length === 0 ? ( | ||
| <div | ||
| className="dropdown-item" | ||
| data-cy="no-suggestions-message" | ||
| data-qa="no-suggestions-message" | ||
| > | ||
| No matching suggestions | ||
| </div> | ||
| ) : ( | ||
| suggestions.map(pers => ( | ||
| <button | ||
| key={pers.slug} | ||
| type="button" | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
| data-qa="suggestion-item" | ||
| onMouseDown={e => { | ||
| e.preventDefault(); | ||
| handleSelect(pers); | ||
| }} | ||
| > | ||
| {pers.name} | ||
| </button> | ||
| )) | ||
| )} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You compute a trimmed/lowercased version of the query here:
const queryLower = query.trim().toLowerCase();— but you still rely on the rawqueryin the effect dependencies. As a result the effect will re-run whenquerychanges by whitespace only (e.g."John"->"John ") even though the trimmed text didn't change, causing redundant filtering. The task requires: "Don't run filtering again if the text has not changed (a pause in typing happened when the text was the same as before)." Consider derivingqueryLoweroutside and using it in the dependency array or storing the last trimmed value in a ref and skipping filtering when it hasn't changed.