Skip to content

Commit

Permalink
feat: clear input from inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cayla Hamann committed Jun 25, 2020
1 parent f9e7fea commit de40df6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/components/FeatherIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const FeatherIcon = ({ className, name, size = '1em' }) => {
) : null;
};

// Icons from https://feathericons.com/
// Icons from
const ICONS = {
'book-open': (
<>
Expand Down Expand Up @@ -97,6 +97,12 @@ const ICONS = {
<polyline points="16 16 12 12 8 16" />
</>
),
x: (
<>
<line x1="18" y1="6" x2="6" y2="18" />
<line x1="6" y1="6" x2="18" y2="18" />
</>
),
};

FeatherIcon.propTypes = {
Expand Down
8 changes: 4 additions & 4 deletions src/components/IconGallery.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import styles from './IconGallery.module.scss';
import IconReference from './IconReference';
import SearchInput from './SearchInput';

const IconGallery = () => {
if (typeof window === 'undefined') global.window = {};
Expand All @@ -22,13 +23,12 @@ const IconGallery = () => {
<h2>Icon Gallery</h2>

<form className={styles.iconFilter}>
<input
<SearchInput
className={styles.search}
type="text"
name="filter"
placeholder="Filter icons by name"
value={search}
onClear={() => setSearch('')}
onChange={(e) => setSearch(e.target.value)}
value={search}
/>
</form>

Expand Down
2 changes: 1 addition & 1 deletion src/components/IconGallery.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

.search {
width: 60%;
padding: 0.75rem 1rem;
margin: 1rem 0;
height: auto;
}

Expand Down
34 changes: 28 additions & 6 deletions src/components/SearchInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,37 @@ import cx from 'classnames';
import FeatherIcon from './FeatherIcon';
import styles from './SearchInput.module.scss';

const SearchInput = ({ className, ...props }) => (
<div className={cx(styles.container, className)}>
<input {...props} className={styles.input} type="text" />
<FeatherIcon name="search" className={styles.icon} />
</div>
);
const SearchInput = ({ className, onClear, value, ...props }) => {
const handleClick = (e) => {
e.preventDefault();
onClear();
};
const handleKeyDown = (e) => {
if (e.key === 'Escape') onClear();
};
return (
<div className={cx(styles.container, className)}>
<input value={value} {...props} className={styles.input} type="text" />
<span
role="button"
tabIndex={0}
onKeyDown={handleKeyDown}
onClick={handleClick}
className={styles.clearButton}
>
<FeatherIcon
name={value !== '' ? 'x' : 'search'}
className={styles.icon}
/>
</span>
</div>
);
};

SearchInput.propTypes = {
className: PropTypes.string,
onClear: PropTypes.func,
value: PropTypes.string,
};

export default SearchInput;
1 change: 1 addition & 0 deletions src/components/SearchInput.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.icon {
position: absolute;
right: 0.5rem;
bottom: 0.5rem;
stroke: var(--color-neutrals-700);
}

Expand Down
1 change: 1 addition & 0 deletions src/components/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Sidebar = ({ className }) => {
<SearchInput
className={styles.searchInput}
placeholder="Search developer docs"
onClear={() => setSearchTerm('')}
onChange={(e) => setSearchTerm(e.target.value)}
value={searchTerm}
/>
Expand Down

0 comments on commit de40df6

Please sign in to comment.