Added SearchBar component and queryParsed

This commit is contained in:
unknown
2021-10-04 13:44:12 +02:00
parent a6a014d47f
commit 966db73238
4 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import { useRef, useEffect, KeyboardEvent } from 'react';
import { searchParser } from '../utils';
export const SearchBar = (): JSX.Element => {
const inputRef = useRef<HTMLInputElement>(document.createElement('input'));
useEffect(() => {
inputRef.current.focus();
}, [inputRef]);
const inputHandler = (e: KeyboardEvent<HTMLInputElement>) => {
const rawQuery = searchParser(inputRef.current.value);
};
return (
<div className='mb-3'>
<input
type='text'
className='form-control'
placeholder='card lang:typescript tags:ui,react'
ref={inputRef}
onKeyUp={e => inputHandler(e)}
/>
<div className='form-text text-gray'>
Submit search query by pressing `Enter`. Read more about available
filters{' '}
<a
href='https://github.com/pawelmalak/snippet-box/wiki/Search-filters'
target='_blank'
rel='noreferrer'
className='text-success text-decoration-none'
>
here
</a>
</div>
</div>
);
};

View File

@@ -2,6 +2,7 @@ import { useEffect, useContext, Fragment } from 'react';
import { SnippetsContext } from '../store'; import { SnippetsContext } from '../store';
import { Layout, PageHeader, EmptyState } from '../components/UI'; import { Layout, PageHeader, EmptyState } from '../components/UI';
import { SnippetGrid } from '../components/Snippets/SnippetGrid'; import { SnippetGrid } from '../components/Snippets/SnippetGrid';
import { SearchBar } from '../components/SearchBar';
export const Home = (): JSX.Element => { export const Home = (): JSX.Element => {
const { snippets, getSnippets } = useContext(SnippetsContext); const { snippets, getSnippets } = useContext(SnippetsContext);
@@ -16,6 +17,12 @@ export const Home = (): JSX.Element => {
<EmptyState /> <EmptyState />
) : ( ) : (
<Fragment> <Fragment>
<PageHeader title='Search' />
<SearchBar />
{/* <div className='col-12 mb-4'>
<SnippetGrid snippets={snippets.filter(s => s.isPinned)} />
</div> */}
{snippets.some(s => s.isPinned) && ( {snippets.some(s => s.isPinned) && (
<Fragment> <Fragment>
<PageHeader title='Pinned snippets' /> <PageHeader title='Pinned snippets' />

View File

@@ -1,3 +1,4 @@
export * from './dateParser'; export * from './dateParser';
export * from './badgeColor'; export * from './badgeColor';
export * from './findLanguage'; export * from './findLanguage';
export * from './searchParser';

View File

@@ -0,0 +1,25 @@
export const searchParser = (rawQuery: string): void => {
// const rawQuery = 'my search tags:ui,react lang:typescript';
// Extract filters from query
const tags = extractFilters(rawQuery, 'tags');
const languages = extractFilters(rawQuery, 'lang');
const query = rawQuery.replaceAll(/(tags|lang):[a-zA-Z]+(,[a-zA-Z]+)*/g, '');
console.log(tags);
console.log(languages);
console.log(query);
};
const extractFilters = (query: string, filter: string): string[] => {
let filters: string[] = [];
const regex = new RegExp(filter + ':[a-zA-Z]+(,[a-zA-Z]+)*');
const matcher = query.match(regex);
if (matcher) {
filters = matcher[0].split(':')[1].split(',');
}
return filters;
};