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

Functionality to clear input from sidenav search and icon gallery search #247

Merged
merged 6 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this comment lost the URL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that comes from my bad habit of clicking ctrl x 😅 I'll fix that

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its typically not a great idea to use a div button for accessibility reasons. We should instead use a button instead.

That being said, is there a reason you couldn't add the event handlers to the FeatherIcon component itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't trigger when click on the FeatherIcon, I wasn't sure why that was happening which is why I wrapped it in a div. I will change this to a button!

tabIndex={0}
onKeyDown={handleKeyDown}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intention for this to trigger when the button is focused?

onClick={handleClick}
className={styles.clearButton}
>
<FeatherIcon
name={value !== '' ? 'x' : 'search'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably be simplified to value ? 'search' : 'x' since empty strings are falsy.

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