From bd7d68bfcccaeb08d8ddc5619c99ce7c0643cb91 Mon Sep 17 00:00:00 2001 From: Carlos Cuesta Date: Mon, 30 Nov 2020 22:19:20 +0100 Subject: [PATCH] :sparkles: Add gitmoji search (#635) --- .../__snapshots__/pages.spec.js.snap | 18 +++++++++- src/components/GitmojiList/Gitmoji/index.js | 5 +-- src/components/GitmojiList/Toolbar/index.js | 24 +++++++++++++ .../GitmojiList/Toolbar/styles.module.css | 15 ++++++++ .../__snapshots__/gitmojiList.spec.js.snap | 18 +++++++++- .../GitmojiList/__tests__/gitmojiList.spec.js | 15 ++++++++ src/components/GitmojiList/index.js | 36 +++++++++++++------ 7 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 src/components/GitmojiList/Toolbar/index.js create mode 100644 src/components/GitmojiList/Toolbar/styles.module.css diff --git a/src/__tests__/__snapshots__/pages.spec.js.snap b/src/__tests__/__snapshots__/pages.spec.js.snap index f9dc0b0..74b52f5 100644 --- a/src/__tests__/__snapshots__/pages.spec.js.snap +++ b/src/__tests__/__snapshots__/pages.spec.js.snap @@ -457,12 +457,28 @@ exports[`Pages Index should render the page 1`] = ` />
+
+
+ +
+
diff --git a/src/components/GitmojiList/Gitmoji/index.js b/src/components/GitmojiList/Gitmoji/index.js index 539d6f1..dfe9c35 100644 --- a/src/components/GitmojiList/Gitmoji/index.js +++ b/src/components/GitmojiList/Gitmoji/index.js @@ -1,4 +1,5 @@ -import React from 'react' +// @flow +import React, { type Element } from 'react' import styles from './styles.module.css' @@ -9,7 +10,7 @@ type Props = { name: string, } -const Gitmoji = (props: Props) => ( +const Gitmoji = (props: Props): Element<'article'> => (
diff --git a/src/components/GitmojiList/Toolbar/index.js b/src/components/GitmojiList/Toolbar/index.js new file mode 100644 index 0000000..bb3c638 --- /dev/null +++ b/src/components/GitmojiList/Toolbar/index.js @@ -0,0 +1,24 @@ +// @flow +import React, { type Element } from 'react' + +import styles from './styles.module.css' + +type Props = { + searchInput: ?string, + setSearchInput: Function, +} + +const Toolbar = (props: Props): Element<'div'> => ( +
+ props.setSearchInput(event.target.value)} + placeholder="Search a Gitmoji by :code:" + type="text" + value={props.searchInput} + /> +
+) + +export default Toolbar diff --git a/src/components/GitmojiList/Toolbar/styles.module.css b/src/components/GitmojiList/Toolbar/styles.module.css new file mode 100644 index 0000000..9cc43d4 --- /dev/null +++ b/src/components/GitmojiList/Toolbar/styles.module.css @@ -0,0 +1,15 @@ +.searchInput { + border-radius: 4px; + border: 0; + box-shadow: 0 2px 4px 0 var(--cardShadow); + flex: 1; + font-size: 1rem; + margin-bottom: 0.5rem; + margin-top: 1.5rem; + outline: none; + padding: 1rem; +} + +.container { + display: flex; +} diff --git a/src/components/GitmojiList/__tests__/__snapshots__/gitmojiList.spec.js.snap b/src/components/GitmojiList/__tests__/__snapshots__/gitmojiList.spec.js.snap index 5cd096e..3d61f32 100644 --- a/src/components/GitmojiList/__tests__/__snapshots__/gitmojiList.spec.js.snap +++ b/src/components/GitmojiList/__tests__/__snapshots__/gitmojiList.spec.js.snap @@ -2,12 +2,28 @@ exports[`GitmojiList should render the component 1`] = `
+
+
+ +
+
diff --git a/src/components/GitmojiList/__tests__/gitmojiList.spec.js b/src/components/GitmojiList/__tests__/gitmojiList.spec.js index 2a4c5fc..ad937e2 100644 --- a/src/components/GitmojiList/__tests__/gitmojiList.spec.js +++ b/src/components/GitmojiList/__tests__/gitmojiList.spec.js @@ -8,4 +8,19 @@ describe('GitmojiList', () => { const wrapper = renderer.create() expect(wrapper).toMatchSnapshot() }) + + describe('when user search the bug gitmoji', () => { + it('should filter the GitmojiList and find the bug gitmoji', () => { + const wrapper = renderer.create() + const instance = wrapper.root + + renderer.act(() => { + instance + .findByType('input') + .props.onChange({ target: { value: 'Bug' } }) + }) + + expect(instance.findAllByType('article').length).toEqual(1) + }) + }) }) diff --git a/src/components/GitmojiList/index.js b/src/components/GitmojiList/index.js index dab5e1f..6d21aee 100644 --- a/src/components/GitmojiList/index.js +++ b/src/components/GitmojiList/index.js @@ -3,6 +3,7 @@ import React, { type Element } from 'react' import Clipboard from 'clipboard' import Gitmoji from './Gitmoji' +import Toolbar from './Toolbar' import emojiColorsMap from './emojiColorsMap' type Props = { @@ -15,6 +16,13 @@ type Props = { } const GitmojiList = (props: Props): Element<'div'> => { + const [searchInput, setSearchInput] = React.useState(null) + const gitmojis = searchInput + ? props.gitmojis.filter((gitmoji) => + gitmoji.code.includes(searchInput.toLowerCase()) + ) + : props.gitmojis + React.useEffect(() => { const clipboard = new Clipboard( '.gitmoji-clipboard-emoji, .gitmoji-clipboard-code' @@ -40,7 +48,7 @@ const GitmojiList = (props: Props): Element<'div'> => { }, []) return ( -
+
- {props.gitmojis.map((gitmoji, index) => ( - - ))} +
+ +
+ + {gitmojis.length === 0 ? ( +

No gitmojis found for search: {searchInput}

+ ) : ( + gitmojis.map((gitmoji, index) => ( + + )) + )}
) }