React Search Filter

In this example below you will see how to do a React Search Filter with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by -J0hn-, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright -J0hn- ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>React Search Filter</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="app"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/-J0hn-/react-search-filter-GOBdQe */
img {
  width: 14em;
  height: 21em;
  display: block;
}

#movies {
  width: 720px;
  margin: 1.3em auto 0;
  display: flex;
  flex-wrap: wrap;
}
#movies figure {
  margin: .50em;
  box-shadow: 0 0 15px gray;
}

figcaption {
  padding: 1em .40em 0;
  line-height: 1;
}
figcaption > * {
  margin: 0;
}

.genre {
  font-size: .80em;
  color: #cccc00;
  font-weight: bold;
}

.rating {
  display: flex;
  align-items: center;
}
.rating .fa-heart {
  margin-right: .30em;
  color: #e60000;
}
.rating p {
  font-weight: bold;
}

#search-input {
  display: inline-block;
  margin: 1.5em 0 0 2em;
  position: relative;
  background: red;
}
#search-input .fa-search {
  position: absolute;
  right: 5px;
  bottom: 8px;
  color: #e7e7e7;
}

::-webkit-input-placeholder {
  color: lightgray;
  letter-spacing: 1px;
}

:-ms-input-placeholder {
  color: lightgray;
  letter-spacing: 1px;
}

::-ms-input-placeholder {
  color: lightgray;
  letter-spacing: 1px;
}

::placeholder {
  color: lightgray;
  letter-spacing: 1px;
}

input[type="search"] {
  letter-spacing: 1px;
  border: none;
  border-bottom: solid 2px lightgray;
  outline: none;
  color: gray;
  padding: .20em .50em .20em 0;
}
input[type="search"]:focus {
  border-bottom-color: #cccc00;
}


/*Downloaded from https://www.codeseek.co/-J0hn-/react-search-filter-GOBdQe */
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var movies = [{
  title: 'Mad Max: Fury Road',
  genre: ['Action', 'Adventure', 'Sci-Fi'],
  rating: 8.3,
  poster: 'https://s-media-cache-ak0.pinimg.com/originals/ce/0c/93/ce0c93d50ae68922d1f4f6667c95e1a8.jpg'
}, {
  title: 'The Hunger Games: Mockingjay Part 1',
  genre: ['Adventure', 'Sci-Fi', 'Thriller'],
  rating: 6.8,
  poster: 'https://1.bp.blogspot.com/-Ds0sudZmSq4/Vgxrr75E77I/AAAAAAAAREo/sZkZW5YMDTw/s1600/Mockingjay%2BPart%2B1.jpg'
}, {
  title: 'Jurassic World',
  genre: ['Action', 'Adventure', 'Sci-Fi'],
  rating: 7.2,
  poster: 'https://s-media-cache-ak0.pinimg.com/736x/0b/ab/9a/0bab9a9c671dbb7aa8626eec44a0195f.jpg'
}, {
  title: 'Everest',
  genre: ['Adventure', 'Drame', 'Thriller'],
  rating: 7.4,
  poster: 'http://www.impawards.com/2015/posters/everest_ver4.jpg'
}, {
  title: 'Insurgent',
  genre: ['Adventure', 'Sci-Fi', 'Thriller'],
  rating: 6.4,
  poster: 'http://cdn2-www.comingsoon.net/assets/uploads/2015/01/FIN16_Insurgent_Guns_1Sht_Trim-1422379653-mtv-14224534611.jpg'
}, {
  title: 'Sicario',
  genre: ['Action', 'Crime', 'Drama'],
  rating: 8,
  poster: 'https://s-media-cache-ak0.pinimg.com/564x/7f/a1/5c/7fa15c26aa2cb48562ea37bbc177be74.jpg'
}];

var Movie = function Movie(prop) {
  return React.createElement(
    'figure',
    null,
    React.createElement('img', { src: prop.movie.poster }),
    React.createElement(
      'figcaption',
      null,
      React.createElement(
        'p',
        { className: 'genre' },
        prop.movie.genre.join(', ')
      ),
      React.createElement(
        'div',
        { className: 'rating' },
        React.createElement('i', { className: 'fa fa-heart' }),
        React.createElement(
          'p',
          null,
          prop.movie.rating
        )
      )
    )
  );
};

var Form = function (_React$Component) {
  _inherits(Form, _React$Component);

  function Form(props) {
    _classCallCheck(this, Form);

    var _this = _possibleConstructorReturn(this, (Form.__proto__ || Object.getPrototypeOf(Form)).call(this, props));

    _this.updateSearch = _this.updateSearch.bind(_this);
    return _this;
  }

  _createClass(Form, [{
    key: 'updateSearch',
    value: function updateSearch(newSearch) {
      this.props.updateSearch(newSearch);
    }
  }, {
    key: 'render',
    value: function render() {
      return React.createElement(
        'div',
        null,
        React.createElement(
          'div',
          { id: 'search-input' },
          React.createElement('input', { type: 'search',
            placeholder: 'Find Movies...',
            value: this.props.search,
            onChange: this.updateSearch
          }),
          React.createElement('i', { className: 'fa fa-search' })
        )
      );
    }
  }]);

  return Form;
}(React.Component);

var Main = function (_React$Component2) {
  _inherits(Main, _React$Component2);

  function Main(props) {
    _classCallCheck(this, Main);

    var _this2 = _possibleConstructorReturn(this, (Main.__proto__ || Object.getPrototypeOf(Main)).call(this, props));

    _this2.updateSearch = _this2.updateSearch.bind(_this2);
    _this2.state = {
      search: ''
    };
    return _this2;
  }

  _createClass(Main, [{
    key: 'updateSearch',
    value: function updateSearch(e) {
      e.preventDefault();
      this.setState({
        search: e.target.value
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var search = this.state.search;

      return React.createElement(
        'main',
        null,
        React.createElement(Form, { search: search, updateSearch: this.updateSearch }),
        React.createElement(
          'section',
          { id: 'movies' },
          this.props.movies.filter(function (movie) {
            return movie.title.toLowerCase().includes(search.toLowerCase());
          }).map(function (movie) {
            return React.createElement(Movie, { movie: movie });
          })
        )
      );
    }
  }]);

  return Main;
}(React.Component);

var App = function (_React$Component3) {
  _inherits(App, _React$Component3);

  function App(props) {
    _classCallCheck(this, App);

    var _this3 = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));

    _this3.state = {
      movies: movies
    };
    return _this3;
  }

  _createClass(App, [{
    key: 'render',
    value: function render() {
      var movies = this.state.movies;

      return React.createElement(Main, { movies: movies });
    }
  }]);

  return App;
}(React.Component);

var app = document.getElementById('app');
ReactDOM.render(React.createElement(App, null), app);

Comments