Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created search input component. #26

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
24 changes: 24 additions & 0 deletions src/components/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import '../styles/components/search.css'
omarefg marked this conversation as resolved.
Show resolved Hide resolved

const Search = ({ posts, filterHandler }) => {

const onChangeHandler = event => {
const value = event.target.value.toUpperCase()
const filtered = posts.filter(({ node }) => {
const title = node.frontmatter.title || node.fields.slug
return title.toUpperCase().includes(value)
})
filterHandler(filtered)
}

return (
<input
className="search"
placeholder="Encuentra un artículo..."
onChange={onChangeHandler}
/>
);
}

omarefg marked this conversation as resolved.
Show resolved Hide resolved
export default Search;
10 changes: 8 additions & 2 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import React from "react"
import React, { useState } from "react"
omarefg marked this conversation as resolved.
Show resolved Hide resolved
import { Link, graphql } from "gatsby"

import Layout from "../components/layout"
import Search from "../components/search"
import SEO from "../components/seo"
import { rhythm } from "../utils/typography"

const BlogIndex = ({ data, location }) => {
const siteTitle = data.site.siteMetadata.title
const posts = data.allMarkdownRemark.edges
const [filteredPosts, setFilteredPosts] = useState(posts)

return (
<Layout location={location} title={siteTitle}>
<SEO title="All posts" />
{posts.map(({ node }) => {
<Search
posts={posts}
filterHandler={setFilteredPosts}
/>
{filteredPosts.map(({ node }) => {
const title = node.frontmatter.title || node.fields.slug
return (
<article key={node.fields.slug}>
Expand Down
14 changes: 14 additions & 0 deletions src/styles/components/search.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.search {
border: none;
border-radius: 5px;
outline: none;
width: 100%;
background-color: #eaeaea;
font-weight: 500;
padding: 8px 16px;
margin-top: 2em;
}

.search, .search::placeholder {
color: #444444;
}