-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add openOnFocus and remove minLength #31
Conversation
@@ -228,7 +226,7 @@ export function getPropGetters<TItem>({ | |||
providedProps.inputElement === | |||
props.environment.document.activeElement && | |||
!store.getState().isOpen && | |||
store.getState().query.length >= props.minLength | |||
props.openOnFocus |
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.
This kind of sounds funny to me.
- When it's not open
- and it's meant to be open on focus
- then let's give it a focus
- (so that it will be open)
I don't have any other idea. So we can leave it as is.
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.
I think this needs to be: (props.openOnFocus || store.getState().query.length > 0)
.
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.
As explained in DM, the option name that we originally though about (openOnFocus
) is a bit misleading because it means "open on focus when there's no query". Either way, we always want to open the dropdown on focus.
Therefore I reviewed this PR with the intended behavior. We can rename the option later if we find a better name (openOnEmptyQuery
?).
setContext, | ||
}); | ||
} | ||
onInput({ |
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.
We need to wrap this with if (props.openOnFocus)
otherwise, the dropdown opens when you click "reset" without the option openOnFocus
.
@@ -55,21 +55,6 @@ export function onInput<TItem>({ | |||
setHighlightedIndex(props.defaultHighlightedIndex); | |||
setQuery(query); | |||
|
|||
if (query.length < props.minLength) { |
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.
We want to keep this branch, but the condition should be query.length === 0 && props.openOnFocus === false
. This makes sure to "reset" the state.
The reasoning behind this is that as opposed to InstantSearch, when there's no query, we do not want to consider any results, except when openOnFocus
is true
. This behavior would be easier to validate once we write integration tests.
@@ -117,7 +102,7 @@ export function onInput<TItem>({ | |||
setSuggestions(suggestions as any); | |||
setIsOpen( | |||
nextState.isOpen ?? | |||
(query.length >= props.minLength && | |||
(props.openOnFocus || |
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.
openOnFocus
should always be paired with a query length check: (query.length === 0 && props.openOnFocus) || /* ... */
.
if (store.getState().query.length >= props.minLength) { | ||
// We want to trigger a query when `openOnFocus` is true | ||
// because the dropdown should open with the current query. | ||
if (props.openOnFocus) { |
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.
I think we want: props.openOnFocus || store.getState().query.length > 0
.
@@ -228,7 +226,7 @@ export function getPropGetters<TItem>({ | |||
providedProps.inputElement === | |||
props.environment.document.activeElement && | |||
!store.getState().isOpen && | |||
store.getState().query.length >= props.minLength | |||
props.openOnFocus |
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.
I think this needs to be: (props.openOnFocus || store.getState().query.length > 0)
.
@@ -115,7 +115,7 @@ export const stateReducer: Reducer = (action, state, props) => { | |||
return { | |||
...state, | |||
highlightedIndex: props.defaultHighlightedIndex, | |||
isOpen: state.query.length >= props.minLength, | |||
isOpen: props.openOnFocus, |
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.
props.openOnFocus || state.query.length > 0
|
Co-Authored-By: François Chalifour <francoischalifour@users.noreply.github.com>
…ocomplete.js into feat/open-on-focus
@@ -228,7 +227,7 @@ export function getPropGetters<TItem>({ | |||
providedProps.inputElement === | |||
props.environment.document.activeElement && | |||
!store.getState().isOpen && | |||
store.getState().query.length >= props.minLength | |||
(props.openOnFocus || store.getState().query.length > 0) |
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.
Actually this condition is already checked in the onFocus
function. We can remove it.
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.
like this, right?
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.
Yep
Co-Authored-By: François Chalifour <francoischalifour@users.noreply.github.com>
…ocomplete.js into feat/open-on-focus
Summary
This PR adds
openOnFocus
and removesminLength
.Preview ➡️