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

feat(Search): add Esc button support to clear input field #7230

Merged
merged 4 commits into from
Nov 6, 2020
Merged
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 packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4510,6 +4510,9 @@ Map {
"onChange": Object {
"type": "func",
},
"onKeyDown": Object {
"type": "func",
},
"placeHolderText": Object {
"type": "string",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,7 @@ exports[`DataTable should render 1`] = `
className="bx--search-input"
id="custom-id"
onChange={[Function]}
onKeyDown={[Function]}
placeholder="Filter table"
role="searchbox"
tabIndex="0"
Expand Down Expand Up @@ -3360,6 +3361,7 @@ exports[`DataTable sticky header should render 1`] = `
className="bx--search-input"
id="custom-id"
onChange={[Function]}
onKeyDown={[Function]}
placeholder="Filter table"
role="searchbox"
tabIndex="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ exports[`DataTable.TableToolbarSearch should render 1`] = `
className="bx--search-input"
id="custom-id"
onChange={[Function]}
onKeyDown={[Function]}
placeholder="Filter table"
role="searchbox"
tabIndex="-1"
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/Search/Search-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import React from 'react';
import { action } from '@storybook/addon-actions';

import { withKnobs, boolean, select, text } from '@storybook/addon-knobs';
import Search from '../Search';
import SearchSkeleton from '../Search/Search.Skeleton';
Expand Down Expand Up @@ -37,6 +36,7 @@ const props = () => ({
),
placeHolderText: text('Placeholder text (placeHolderText)', 'Search'),
onChange: action('onChange'),
onKeyDown: action('onKeyDown'),
});

export default {
Expand Down
18 changes: 17 additions & 1 deletion packages/react/src/components/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import React, { Component } from 'react';
import classNames from 'classnames';
import { Search16, Close16 } from '@carbon/icons-react';
import { settings } from 'carbon-components';
import { composeEventHandlers } from '../../tools/events';
import { keys, match } from '../../internal/keyboard';
import deprecate from '../../prop-types/deprecate';

const { prefix } = settings;
Expand Down Expand Up @@ -56,6 +58,11 @@ export default class Search extends Component {
*/
onChange: PropTypes.func,

/**
* Provide a handler that is invoked on the key down event for the input
*/
onKeyDown: PropTypes.func,

/**
* Provide an optional placeholder text for the Search.
* Note: if the label and placeholder differ,
Expand Down Expand Up @@ -137,6 +144,12 @@ export default class Search extends Component {
this.props.onChange(evt);
};

handleKeyDown = (evt) => {
if (match(evt, keys.Escape)) {
this.clearInput(evt);
}
};

render() {
const {
className,
Expand All @@ -151,6 +164,8 @@ export default class Search extends Component {
size = !small ? 'xl' : 'sm',
light,
disabled,
onChange,
onKeyDown,
...other
} = this.props;

Expand Down Expand Up @@ -186,7 +201,8 @@ export default class Search extends Component {
className={`${prefix}--search-input`}
id={id}
placeholder={placeHolderText}
onChange={this.handleChange}
onChange={composeEventHandlers([onChange, this.handleChange])}
onKeyDown={composeEventHandlers([onKeyDown, this.handleKeyDown])}
ref={(input) => {
this.input = input;
}}
Expand Down