polaris icon indicating copy to clipboard operation
polaris copied to clipboard

[AutoComplete] Enter key not confirming selection

Open maxariss opened this issue 8 years ago • 10 comments

Issue summary

When tabbing through an <Autocomplete />'s option list, hitting the enter key doesn't confirm a selection and update the field.

Expected behavior

Hitting enter on your keyboard should confirm your selection and update the field.

Actual behavior

Hitting enter just closes the <Popover />

I’ve narrowed it down to this.state.popoverActive being false when it should be true on line:

https://github.com/Shopify/polaris-react/blob/master/src/components/Autocomplete/components/ComboBox/ComboBox.tsx#L335

Steps to reproduce the problem

  • Checkout https://github.com/Shopify/web/pull/8161 in Shopify/web
  • Open a smart collection
  • Change a condition to product type
  • Begin tabbing through types, and press enter on your keyboard

Specifications

  • Are you using the React components?: ✅
  • Polaris version number: 3.0.0-beta.14
  • Browser: Chrome
  • Device: MBP
  • Operating System: Not Mojave

maxariss avatar Nov 07 '18 14:11 maxariss

👋 Thanks for opening your first issue. A contributor should give feedback soon. If you haven’t already, please check out the contributing guidelines. You can also join #polaris on the Shopify Partners Slack.

ghost avatar Nov 07 '18 14:11 ghost

Thanks @maxariss I'll have a look.

solonaarmstrong-zz avatar Nov 07 '18 16:11 solonaarmstrong-zz

Hi @solonaarmstrong ! Thanks for taking a look :) Were you able to make heads or tales of where the race condition is coming from ?

maxariss avatar Nov 12 '18 18:11 maxariss

I'm just looking at it right now, actually. I had another PR open that affected arrow keys and I wanted to make sure that wasn't the problem. I'm trying to reproduce it.

solonaarmstrong-zz avatar Nov 12 '18 18:11 solonaarmstrong-zz

I was able to see the bug, but I can't reproduce it in the playground, using nearly identical code.

autocomplete-enter2

/* eslint-disable */

import * as React from 'react';
import {Page, AppProvider, Button} from '@shopify/polaris';
import Autocomplete from '../src/components/Autocomplete';
import _ from 'lodash';

interface Props {}
interface State {}

export default class DemoTagsAutocomplete extends React.Component<
  Props,
  State
> {
  // Quick Config
  ALLOW_MULTIPLE = false;
  RESULT_PAGE_LENGTH = 10;

  options = [
    {value: 'byward_market', label: 'Byward Market'},
    {value: 'cheese_pizza', label: 'Cheese Pizza'},
    {value: 'macaroni_pizza', label: 'Macaroni Pizza'},
    {value: 'pepperoni_pizza', label: 'Pepperoni Pizza'},
    {value: 'centretown', label: 'Centretown'},
    {value: 'hintonburg', label: 'Hintonburg'},
  ];

  state = {
    selected: [],
    inputText: '',
    options: this.options.slice(0, this.RESULT_PAGE_LENGTH),
    filteredOptions: this.options.slice(
      this.RESULT_PAGE_LENGTH,
      this.options.length - 1,
    ),
    loading: false,
  };

  updateText = (newValue: string) => {
    this.setState({inputText: newValue});
    this.filterAndUpdateOptions(newValue);
  };

  filterAndUpdateOptions = (inputString: string) => {
    if (inputString === '') {
      this.setState({
        options: this.options,
      });
      return;
    }

    const filterRegex = new RegExp(inputString, 'i');
    const resultOptions = this.options.filter((option) =>
      option.label.match(filterRegex),
    );
    this.setState({
      options: resultOptions,
    });
  };

  updateSelection = (updatedSelection: string[]) => {
    const selectedText = updatedSelection.map((selectedItem) => {
      const matchedOption = this.options.filter((option) => {
        return option.value.match(selectedItem);
      });
      return matchedOption[0] && matchedOption[0].label;
    });
    this.setState({selected: selectedText, inputText: selectedText});
  };

  render() {
    return (
      <AppProvider>
        <Page title="Demo Tags Autocomplete">
          <Autocomplete
            options={this.state.options}
            selected={this.state.selected}
            onSelect={this.updateSelection}
            textField={
              <Autocomplete.TextField
                onChange={this.updateText}
                label=""
                labelHidden
                value={this.state.inputText}
                connectedRight={
                  <Button
                    icon="arrowUpDown"
                    // onClick={this.handleShowAllOptions}
                  />
                }
              />
            }
          />
        </Page>
      </AppProvider>
    );
  }


/* eslint-enable */

solonaarmstrong-zz avatar Nov 13 '18 13:11 solonaarmstrong-zz

@solonaarmstrong Interesting ! Can you identify any issue with the code used in web ? Or do you suspect there's a common discrepancy allowing the popover to work in the playground, but preventing it from working in web as expected ?

maxariss avatar Nov 14 '18 19:11 maxariss

@AndrewMusgrave is having a look. I'm caught up in a few other things.

solonaarmstrong-zz avatar Nov 14 '18 21:11 solonaarmstrong-zz

@maxariss as Solona mentioned Dan asked me to look into this tonight. I'm not quite sure this is an AutoComplete issue, it's not that the auto complete isn't getting updated. When you press enter and TextField is your focus, your form handler handleSave function is being called

AndrewMusgrave avatar Nov 14 '18 22:11 AndrewMusgrave

As a consumer would you expect the form not to submit when you press enter with focus on the text field, while the combobox is open?

AndrewMusgrave avatar Nov 15 '18 16:11 AndrewMusgrave

@AndrewMusgrave is correct ! the binding for the form was overriding the binding in the component.

maxariss avatar Nov 15 '18 20:11 maxariss