Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>Tabbing Bug</h2>
<ol>
  <li>Focus on 'Name' input</li>
  <li>Type "Test"</li>
  <li>Tab to 'Language' input</li>
  <li>Click 'Close' button</li>
  <li>Tab to 'Age' input</li>
  <li>Tab back to 'Language' input (shift + tab)</li>
  <li>Tab to 'Age' input again</li>
</ol>
EXPECT: Age input to be in focus
  <br />
  ACTUAL: 'Language' input remains in focus; cannot tab out of 'Language' input without first blurring with mouse
<br />
<br />
<div id="app"></div>

              
            
!

CSS

              
                body {
  font-family: Helvetica, sans-serif;
}

.react-autosuggest__container {
  position: relative;
}

.react-autosuggest__input, input {
  width: 240px;
  height: 30px;
  padding: 10px 20px;
  font-family: Helvetica, sans-serif;
  font-weight: 300;
  font-size: 16px;
  border: 1px solid #aaa;
  border-radius: 4px;
}

.react-autosuggest__input:focus, input:focus {
  outline: none;
}

.react-autosuggest__container--open .react-autosuggest__input {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.react-autosuggest__suggestions-container {
  display: none;
}

.react-autosuggest__container--open .react-autosuggest__suggestions-container {
  display: block;
  position: absolute;
  top: 51px;
  width: 280px;
  border: 1px solid #aaa;
  background-color: #fff;
  font-family: Helvetica, sans-serif;
  font-weight: 300;
  font-size: 16px;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  z-index: 2;
}

.react-autosuggest__suggestions-list {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

.react-autosuggest__suggestion {
  cursor: pointer;
  padding: 10px 20px;
}

.react-autosuggest__suggestion--focused {
  background-color: #ddd;
}

              
            
!

JS

              
                const languages = [
  {
    name: "C",
    year: 1972
  },
  {
    name: "C#",
    year: 2000
  },
  {
    name: "C++",
    year: 1983
  },
  {
    name: "Clojure",
    year: 2007
  },
  {
    name: "Elm",
    year: 2012
  },
  {
    name: "Go",
    year: 2009
  },
  {
    name: "Haskell",
    year: 1990
  },
  {
    name: "Java",
    year: 1995
  },
  {
    name: "Javascript",
    year: 1995
  },
  {
    name: "Perl",
    year: 1987
  },
  {
    name: "PHP",
    year: 1995
  },
  {
    name: "Python",
    year: 1991
  },
  {
    name: "Ruby",
    year: 1995
  },
  {
    name: "Scala",
    year: 2003
  }
];

// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
function escapeRegexCharacters(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

function getSuggestions(value) {
  const escapedValue = escapeRegexCharacters(value.trim());

  if (escapedValue === "") {
    return languages;
  }

  const regex = new RegExp("^" + escapedValue, "i");

  return languages.filter((language) => regex.test(language.name));
}

const alwaysTrue = () => true;

function getSuggestionValue(suggestion) {
  return suggestion.name;
}

function renderSuggestion(suggestion) {
  return <span>{suggestion.name}</span>;
}

class App extends React.Component {
  constructor() {
    super();

    this.state = {
      value: "",
      suggestions: languages
    };

    setInterval(() => {
      if (!this.autosuggest) {
        return;
      }
      // console.log('El count', document.querySelector('#react-autowhatever-1').childElementCount);
      // console.log("autosuggest", this.autosuggest);
      // console.log(
      //   "justClickedOnSuggestionsContainer",
      //   this.autosuggest.justClickedOnSuggestionsContainer
      // );
      // console.log("justPressedUpDown", this.autosuggest.justPressedUpDown);
    }, 1000);
  }

  onChange = (event, { newValue, method }) => {
    this.setState({
      value: newValue
    });
  };

  onSuggestionsFetchRequested = ({ value }) => {
    this.setState({
      suggestions: getSuggestions(value)
    });
  };

  onSuggestionsClearRequested = () => {
    // alert('clear requested');
    this.setState({
      suggestions: []
    });
  };

  storeInputReference = (autosuggest) => {
    if (autosuggest !== null) {
      this.input = autosuggest.input;
      this.autosuggest = autosuggest;
      window.AS = this.autosuggest;
    }
  };

  onButtonMouseDown = (event) => {
    // Do this if you want to close and lose focus
    const closeAndBlur = () => this.autosuggest.input.blur();
    // Or do this if you want to close and keep focus
    const close = () => this.autosuggest.closeSuggestions();
    close();
    // FIX: Uncomment this setTimeout to fix the issue.
    // setTimeout(() => {
    //   // Make sure `justClickedOnSuggestionsContainer` is set back to false
    //   // otherwise you can't tab out of the input.
    //   this.autosuggest.justClickedOnSuggestionsContainer = false;
    // }, 0);
  };

  render() {
    const { value, suggestions } = this.state;
    const inputProps = {
      placeholder: "Type 'c'",
      value,
      onChange: this.onChange
    };

    return (
      <div>
        <label>Name</label>
        <br />
        <input name="name" className="input" />
        <br />
        <br />
        <label>Language</label>
        <Autosuggest
          suggestions={suggestions}
          onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
          onSuggestionsClearRequested={this.onSuggestionsClearRequested}
          getSuggestionValue={getSuggestionValue}
          renderSuggestion={renderSuggestion}
          shouldRenderSuggestions={alwaysTrue}
          inputProps={inputProps}
          ref={this.storeInputReference}
          renderSuggestionsContainer={({ containerProps, children }) => {
            return (
              <div {...containerProps}>
                <button
                  // Disallow focus as it'll be hidden once
                  // the input loses focus.
                  tabIndex="-1"
                  type="button"
                  onMouseDown={this.onButtonMouseDown}
                >
                  Close
                </button>
                {children}
              </div>
            );
          }}
        />
        <br />
        <label>Age</label>
        <br />
        <input name="age" className="input" />
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

              
            
!
999px

Console