Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

feat: add openOnFocus and remove minLength #31

Merged
merged 9 commits into from
Feb 25, 2020
Merged

Conversation

eunjae-lee
Copy link
Collaborator

@eunjae-lee eunjae-lee commented Feb 24, 2020

Summary

This PR adds openOnFocus and removes minLength.

Preview ➡️

@@ -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
Copy link
Collaborator Author

@eunjae-lee eunjae-lee Feb 24, 2020

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.

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).

Copy link
Owner

@francoischalifour francoischalifour left a 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({

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) {

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 ||

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) {

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

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,

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

README.md Outdated Show resolved Hide resolved
@eunjae-lee
Copy link
Collaborator Author

openOnEmptyFocus.. ?

@@ -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)

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

82a8b5a

like this, right?

Choose a reason for hiding this comment

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

Yep

packages/autocomplete-core/src/types/api.ts Outdated Show resolved Hide resolved
eunjae-lee and others added 3 commits February 25, 2020 15:27
Co-Authored-By: François Chalifour <francoischalifour@users.noreply.github.com>
@eunjae-lee eunjae-lee merged commit 553ea68 into next Feb 25, 2020
@francoischalifour francoischalifour deleted the feat/open-on-focus branch February 25, 2020 15:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants