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

Added a search bar to find snippets #506

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions models/groupBySearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Snippet } from './Snippet';

export const groupBySearch = (snippets: Snippet[], query: string) => snippets.reduce((acc, item) => ((acc[item.category] = [...(acc[item.category] || []) , item ].filter(el => el.title.toLowerCase().includes(query.toLowerCase())) ), acc), {});
17 changes: 14 additions & 3 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ import * as React from 'react';
import { SnippetList } from '../components/SnippetList';
import { loadSnippets } from '../models/loadSnippets';
import { groupByCategory } from '../models/groupByCategory';
import { groupBySearch } from '../models/groupBySearch';
import { Layout } from '../layouts/Layout';
import { uid } from '../utils/uid';

import type { Snippet } from '../models/Snippet';

const HomePage: React.FC<{
snippets: Snippet[];
}> = ({ snippets }) => {
const categories = groupByCategory(snippets);
const id = uid();
}> = ({ snippets }) => {
const [categories, setCategories] = React.useState(groupByCategory(snippets));
const id = uid();
const filterCategories = (e) => {
if (e.target.value) {
setCategories(groupBySearch(snippets, e.target.value))
} else {
setCategories(groupByCategory(snippets))
}
}


return (
<Layout>
Expand All @@ -23,6 +32,8 @@ const HomePage: React.FC<{
<h1 className="page-home__heading">Favorite JavaScript Utilities</h1>
<Heading level={4}>in single line of code! No more!</Heading>
<Spacer size="large" />
<input type="input" className="page-home__search" onKeyUp={filterCategories} name="search" id="search" placeholder='Search...' />
<Spacer size="large" />
</div>
{Object.keys(categories).map((category) => (
<div key={category}>
Expand Down
7 changes: 7 additions & 0 deletions styles/pages/_home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@
border-top: 1px solid #e6e6e6;
content: '';
}

.page-home__search{
padding: 6px;
font-size: 150%;
}