Skip to content
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

A Fix for Issue #286 — Spacebar input not registered when 'No results found' #287

Merged
merged 1 commit into from
Jul 27, 2018

Conversation

AdenFraser
Copy link

@AdenFraser AdenFraser commented Jul 27, 2018

This is a proposed fix for Issue #286

The handleSpace method is used to either select a focused option when one exists, or open the full list of options when it is not open. However, there is an edge-case where the user is attempting to enter a string which includes a space, which is not in the list. The space-key entry is omitted on first try, resulting in a malformed string.

This fix simple checks whether there are available results/'No results found' message (or equivalent) is showing — and if that is the case, there is no need to re-attempt to open the menu, thus allowing the user to input their string as expected.

Fixes #286

@@ -318,7 +318,7 @@ export default class Autocomplete extends Component {

handleSpace (event) {
// if not open, open
if (this.props.showAllValues && this.state.menuOpen === false) {
if (this.props.showAllValues && this.state.menuOpen === false && this.state.options.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can use menuIsVisible instead of menuOpen here, which I think better captures the intent and avoids adding another expression?

Copy link
Author

Choose a reason for hiding this comment

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

That makes a lot more sense. Sorry am new to this package and codebase, didn't see the menuIsVisible method!

I'll get that amended and pushed.

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries – I'm still getting to grips with it myself!

Copy link
Author

Choose a reason for hiding this comment

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

Actually I'm not too sure how best to refactor this, as the menuIsVisible const is insider the render()?

Making menuIsVisible available from handleSpace will also require const showNoOptionsFound = this.props.showNoOptionsFound && inputFocused && noOptionsAvailable && queryNotEmpty && queryLongEnough from the render() too? Which then relies on the latter four also being available.

Unless I'm overlooking something — it's mighty hot in our office! 😅💦☀️

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry Aden, you're quite right (I told you I was still getting to grips with it myself!)

From talking to @joelanman who originally added this feature, the intent is that when you focus the element using the keyboard (i.e. by tabbing to it) and press space it should open the menu and display all results (mimicking behaviour of native selects).

I've just tested your fix and whilst it corrects the behaviour that you're seeing (adding a space whilst seeing no results triggers the menu), it doesn't quite work in that scenario – this.state.options.length is 0 if there is no query, so this handler doesn't kick in and rather than opening the menu it just searches for all items with a space in the name.

I think it makes sense to open the menu if it is not already open and there is not already a query – which would work in both cases I think? In which case changing the line to…

if (this.props.showAllValues && this.state.menuOpen === false && this.state.query != '') {

might make sense? I've done some initial testing and it seems to work well, but it'd be good if you could double-check that solves your problem.

Copy link
Author

@AdenFraser AdenFraser Jul 27, 2018

Choose a reason for hiding this comment

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

Hi, gave that a go but that didn't work — although I think we actually just need this.state.query === '', since we only wish for the options list to appear when handling space input initially, not with latter space input.

I've tested it with both the intended original scenario and against the issue I was facing and both now seem to be resolved.

Have rebuilt dist, rebased and squashed and all pushed up.

Aden

Copy link
Contributor

Choose a reason for hiding this comment

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

Amazing, thanks Aden! I think that's what I intended to suggest, not sure how I messed that up.

@36degrees
Copy link
Contributor

Thanks for this Aden! I've had a little play locally and your fix does solve the problem, but I've got a suggestion I think makes it slightly clearer.

Copy link
Contributor

@36degrees 36degrees 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. Needs a second approval from someone else on the team.

Copy link

@kr8n3r kr8n3r left a comment

Choose a reason for hiding this comment

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

thanks @AdenFraser for fixing it :)

@36degrees 36degrees merged commit 01c4c89 into alphagov:master Jul 27, 2018
36degrees added a commit that referenced this pull request Aug 13, 2018
36degrees added a commit that referenced this pull request Aug 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spacebar input not registered when 'No results found'
3 participants