react chained select code example

Example 1: react select with custom option

import React from "react";
import ReactDOM from "react-dom";
import Select from "react-select";

const options = [
  { value: "Abe", label: "Abe", customAbbreviation: "A" },
  { value: "John", label: "John", customAbbreviation: "J" },
  { value: "Dustin", label: "Dustin", customAbbreviation: "D" }
];

const formatOptionLabel = ({ value, label, customAbbreviation }) => (
  <div style={{ display: "flex" }}>
    <div>{label}</div>
    <div style={{ marginLeft: "10px", color: "#ccc" }}>
      {customAbbreviation}
    </div>
  </div>
);

const CustomControl = () => (
  <Select
    defaultValue={options[0]}
    formatOptionLabel={formatOptionLabel}
    options={options}
  />
);

ReactDOM.render(<CustomControl />, document.getElementById("root"));

Example 2: react-select example

import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import AsyncSelect from 'react-select/lib/Async'
import axios from 'axios'
import './styles.css'

const NEW_API_KEY = '?api_key=cfe422613b250f702980a3bbf9e90716'
const SEARCH_URL = 'https://api.themoviedb.org/3/search/movie'
const LANGUAGE = '&language=en-US&'
const END_OPTIONS = '&page=1&include_adult=false'
const QUERY = `query=`

export default class App extends Component {
  
  state = {
    selectedTitle: ''
  }

  searchTitles = async (movieTitle) => {
    const urlRequest = 
      `${SEARCH_URL}${NEW_API_KEY}${LANGUAGE}${QUERY}
	  ${!movieTitle && 'a'}${END_OPTIONS}`
    
    const { data } = await axios.get(urlRequest)
    
    const compare = data.results
      .filter((i) =>
        i.overview.toLowerCase().includes(movieTitle.toLowerCase())
      )
      .map((film) => ({
        label: film.title,
        value: film.id
      }))

    return compare
  }
  render() {
    return (
      <div className="App">
        <AsyncSelect
          cacheOptions
          defaultOptions
          value={this.state.selectedTitle}
          loadOptions={this.searchTitles}
          onChange={(property, value) => {
            this.setState({ selectedTitle: property })
          }}/>
      </div>
    )
  }
}

const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)