Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

bootstrap dropdown with state

import React from 'react';
import './style.css';
import ReactDOM from 'react-dom';
import $ from 'jquery';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isDropdownOpen: false,
      inputValue: '',
      options: ['action', 'newAction', 'oldAction']
    };
  }

  componentDidUpdate(prevProps, prevState) {
    if (
      $('.dropdown-menu')
        .attr('class')
        .indexOf('show') >= 0
    ) {
      $('#dropdownMenuButton').trigger('click.bs.dropdown');
      $('#dropdownMenuButton').trigger('click.bs.dropdown');
    } else {
      $('#dropdownMenuButton').trigger('click.bs.dropdown');
    }

    $('#search').focus();
  }

  handleChange = evt => {
    const value = evt.target.value;
    this.setState({ inputValue: value });
  };
  render() {
    return (
      <div>
        <h1>Hello StackBlitz!</h1>
        <h1>Hello StackBlitz!</h1>
        <h1>Hello StackBlitz!</h1>
        <h1>Hello StackBlitz!</h1>
        <h1>Hello StackBlitz!</h1>
        <h1>Hello StackBlitz!</h1>
        <div className="dropdown" ref={node => (this.DDRef = node)}>
          <button
            className="btn btn-secondary dropdown-toggle"
            type="button"
            id="dropdownMenuButton"
            data-toggle="dropdown"
            aria-haspopup="true"
            aria-expanded="false"
            onClick={() =>
              this.setState({ isDropdownOpen: !this.state.isDropdownOpen })
            }
          >
            {this.state.isDropdownOpen ? (
              <div style={{ display: 'flex' }}>
                <input
                  id="search"
                  type="text"
                  defaultValue={''}
                  value={this.state.inputValue}
                  placeholder={'Search'}
                  ref={node => (this.inputRef = node)}
                  onChange={this.handleChange}
                />
                <span
                  className="cancel-icon"
                  ref={node => (this.cancelRef = node)}
                >
                  x
                </span>
              </div>
            ) : (
              <div>Dropdown button</div>
            )}
          </button>
          <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
            {this.state.options
              .filter(el => el.startsWith(this.state.inputValue))
              .map(option => {
                return (
                  <a className="dropdown-item" href="#">
                    {option}
                  </a>
                );
              })}
          </div>
        </div>
      </div>
    );
  }
}

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs sqlite3 db. insert 
Javascript :: javascript const memory 
Javascript :: inherit mdn 
Javascript :: simple form in react native with code 
Javascript :: Node-Red Custom UI 
Javascript :: imleç 
Javascript :: add link in react table to specific column 
Javascript :: js destructuring arguments 
Javascript :: npm install error `not foundram Files/nodejs/npm: 3: /mnt/c/Program Files/nodejs/npm:` 
Javascript :: class function constructor 
Javascript :: react-native-modal big border 
Javascript :: How to Use the Return Keyword in a Function 
Javascript :: Function Returning Object Saved To A Variable 
Javascript :: hacker news api react 
Javascript :: Backbone Model Vs Backbone Collection 
Javascript :: js change img ssrc 
Javascript :: get user id from username discord 
Javascript :: Backbone Sync Example 
Javascript :: ticket 
Javascript :: inject html string to div javascript 
Javascript :: vertical lineal star pattern javascript 
Javascript :: javascript online string concatenation 
Javascript :: math min js 
Javascript :: jsx children 
Javascript :: react password check wordpress api 
Javascript :: convert string to moment date 
Javascript :: get window object in nextjs 
Javascript :: translate javascript to english 
Javascript :: random color by EventListener click 
Javascript :: alert title change 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =