Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

feat(Dropdown): highlight by character key in non-search #1270

Merged
merged 27 commits into from
May 14, 2019

Conversation

silviuaavram
Copy link
Collaborator

@silviuaavram silviuaavram commented Apr 25, 2019

Behaves as in aria spec

When open, sending a character key will focus on the item that begins with that key.

If there is already an option highlighted, the search will begin from the next item.

If it reaches the end of the array and does not find any match, it will check from the start (circular movement).

If there is no option starting with that character, it will not do anything.

If characters are typed in rapid succession (one in under 500ms) it will search for the option starting with those keys. After 500ms after last key type, the 'starting with' string is cleared.

Also includes adding to the state the array of already filtered files (by value and searchQuery) to be used where needed. These strings will be updated only when the items are changed, value is changed in multiple selection or searchQuery is changed in search.

@silviuaavram silviuaavram changed the title feat(Dropdown): select by character key in non-search feat(Dropdown): highlight by character key in non-search Apr 25, 2019
@codecov
Copy link

codecov bot commented Apr 25, 2019

Codecov Report

Merging #1270 into master will increase coverage by 0.15%.
The diff coverage is 98.03%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1270      +/-   ##
==========================================
+ Coverage   72.16%   72.32%   +0.15%     
==========================================
  Files         762      762              
  Lines        5734     5766      +32     
  Branches     1678     1687       +9     
==========================================
+ Hits         4138     4170      +32     
  Misses       1590     1590              
  Partials        6        6
Impacted Files Coverage Δ
packages/react/src/types.ts 50% <ø> (ø) ⬆️
...ackages/react/src/components/Dropdown/Dropdown.tsx 88.54% <98.03%> (+1.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 33fb2f0...a34a55a. Read the comment docs.

Copy link
Collaborator

@bmdalex bmdalex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, nice feature, just few comments

packages/react/src/components/Dropdown/Dropdown.tsx Outdated Show resolved Hide resolved
packages/react/src/components/Dropdown/Dropdown.tsx Outdated Show resolved Hide resolved
packages/react/src/components/Dropdown/Dropdown.tsx Outdated Show resolved Hide resolved
packages/react/src/components/Dropdown/Dropdown.tsx Outdated Show resolved Hide resolved
@silviuaavram silviuaavram added accessibility All the Accessibility tasks and bugs should be tagged with this. 🚀 ready for review labels Apr 30, 2019
@silviuaavram silviuaavram force-pushed the feat-highlight-by-letter-keys branch from 98ee868 to d1b2257 Compare May 3, 2019 07:42
itemsList.simulate('keydown', { keyCode: keyboardKey.A, key: 'A' })
expect(dropdown.state('highlightedIndex')).toBe(0)

jest.advanceTimersByTime(200)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Dropdown.charKeyPressedCleanupTime / 2

this.a11yStatusTimeout = setTimeout(() => {
this.setState({ a11ySelectionStatus: '' })
}, Dropdown.a11yStatusCleanupTime)
}

private setStartingString = (startingString: string): void => {
clearTimeout(this.charKeysPressedTimeout)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would replace clearTimeout + setTimeout with _.debounce

const { value } = state

if (!items) {
return state
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return state
return null

getDerivedStateFromProps is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or null to update nothing.

https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops

if (_.isFunction(search)) {
filteredResults = {
filteredItems: search(filteredItemsByValue, searchQuery),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we have already used early return, I suggest to use it there, too

if (_.isFunction(search)) {
  return { filteredItems: search(filteredItemsByValue, searchQuery) }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it's better to have it assigned to that variable in branch, and return at the end {...state, ...filteredResults }. Now with return we need, on each branch, to return {...state, filteredItems: whateverResultOnTheBranch}.
Do you think it's better?

@@ -1034,6 +1082,28 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo
}
}

private getHighlightedIndexOnCharKeyDown = (keyString: string): number => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am lost with semantics of this method, it's called getHighlightedIndexOnCharKeyDown(), but it contains setStartingString

Should it be setHighlightedIndexOnCharKeyDown() and contain this.setState({ highlightedIndex })?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. I will refactor it as a setter. Let's see if I also need to refactor the setStartingString one with debounce or not.

@silviuaavram silviuaavram merged commit 3e1228b into master May 14, 2019
@delete-merged-branch delete-merged-branch bot deleted the feat-highlight-by-letter-keys branch May 14, 2019 13:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accessibility All the Accessibility tasks and bugs should be tagged with this. 🚀 ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants