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

Added isClearable prop to EuiFieldSearch #2723

Merged
merged 7 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Converted `EuiSuggest` to Typescript ([#2692](https://github.com/elastic/eui/pull/2692))
- Converted `EuiErrorBoundary` to Typescript ([#2690](https://github.com/elastic/eui/pull/2690))
- Updated `EuiNavDrawer` to accept React fragments ([#2710](https://github.com/elastic/eui/pull/2710))
- Added `isClearable` prop to `EuiFieldSearch` ([#2723](https://github.com/elastic/eui/pull/2723))

**Bug fixes**

Expand Down
15 changes: 15 additions & 0 deletions src-docs/src/views/form_controls/display_toggles.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class DisplayToggles extends Component {
append: false,
isPopoverOpen: false,
invalid: false,
isClearable: false,
};
}

Expand All @@ -48,6 +49,7 @@ export class DisplayToggles extends Component {
canPrepend,
canAppend,
canInvalid,
canClear,
children,
extras,
} = this.props;
Expand All @@ -59,6 +61,7 @@ export class DisplayToggles extends Component {
if (canLoading) canProps.isLoading = this.state.loading;
if (canFullWidth) canProps.fullWidth = this.state.fullWidth;
if (canCompressed) canProps.compressed = this.state.compressed;
if (canClear) canProps.isClearable = this.state.isClearable;
if (canPrepend && this.state.prepend) canProps.prepend = 'Prepend';
if (canAppend && this.state.append) canProps.append = 'Append';
if (canInvalid) canProps.isInvalid = this.state.invalid;
Expand Down Expand Up @@ -145,6 +148,18 @@ export class DisplayToggles extends Component {
/>
</EuiFlexItem>
)}
{canClear && (
<EuiFlexItem grow={false}>
<EuiSwitch
compressed
label={'clearable'}
checked={this.state.isClearable}
onChange={e =>
this.updateProperty(e.target.checked, 'isClearable')
}
/>
</EuiFlexItem>
)}
{canCompressed && (
<EuiFlexItem grow={false}>
<EuiSwitch
Expand Down
4 changes: 2 additions & 2 deletions src-docs/src/views/form_controls/field_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default class extends Component {

onChange = e => {
this.setState({
value: e.target.value,
value: e === '' ? '' : e.target.value,
});
};

render() {
return (
/* DisplayToggles wrapper for Docs only */
<DisplayToggles>
<DisplayToggles canClear>
cchaos marked this conversation as resolved.
Show resolved Hide resolved
<EuiFieldSearch
placeholder="Search this"
value={this.state.value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`EuiFieldSearch is rendered 1`] = `
<eui-form-control-layout
clear="[object Object]"
cchaos marked this conversation as resolved.
Show resolved Hide resolved
compressed="false"
fullwidth="false"
icon="search"
Expand Down
14 changes: 10 additions & 4 deletions src/components/form/field_search/_field_search.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* 1. Fix for Safari to ensure that it renders like a normal text input
* and doesn't add extra spacing around text
*/
* 2. Remove the X clear button from input type search in Chrome and IE
*/

.euiFieldSearch {
@include euiFormControlStyle;
Expand All @@ -11,11 +12,16 @@
// sass-lint:disable-block no-vendor-prefixes
-webkit-appearance: textfield; /* 1 */

&::-webkit-search-decoration {
-webkit-appearance: none; /* 1 */
&::-webkit-search-decoration,
&::-webkit-search-cancel-button {
-webkit-appearance: none; /* 1, 2 */
}

&::-ms-clear {
display: none; /* 2 */
}

&.euiFieldSearch--compressed {
@include euiFormControlWithIcon($isIconOptional: false, $side: 'left', $compressed: true);
}
}
}
16 changes: 16 additions & 0 deletions src/components/form/field_search/field_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ const propTypes = {
* when `true` creates a shorter height input
*/
compressed: PropTypes.bool,
/**
* Shows a clear button when set to true
cchaos marked this conversation as resolved.
Show resolved Hide resolved
*/
isClearable: PropTypes.bool,
};

const defaultProps = {
fullWidth: false,
isLoading: false,
incremental: false,
compressed: false,
isClearable: true,
};

export class EuiFieldSearch extends Component {
Expand All @@ -63,6 +68,11 @@ export class EuiFieldSearch extends Component {
}
}

onClear = () => {
this.props.onChange('');
Copy link
Contributor

Choose a reason for hiding this comment

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

This change does not respect the contract of the onChange handler that expects a React.ChangeEvent

Screen Shot 2020-01-15 at 12 50 05

It caused a regression bug on our side (elastic/kibana#54810), we can quickly fix it but it might have caused a regression on other consumers so it would be better to change it here. @andreadelrio

Copy link
Contributor

Choose a reason for hiding this comment

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

Pulled this out into a stand-alone issue #2763

this.inputElement.focus();
};

componentWillUnmount() {
this.cleanups.forEach(cleanup => cleanup());
}
Expand Down Expand Up @@ -100,6 +110,7 @@ export class EuiFieldSearch extends Component {
incremental,
compressed,
onSearch,
isClearable,
...rest
} = this.props;

Expand All @@ -118,6 +129,11 @@ export class EuiFieldSearch extends Component {
icon="search"
fullWidth={fullWidth}
isLoading={isLoading}
clear={
isClearable && value && !rest.readOnly && !rest.disabled
? { onClick: this.onClear }
: null
}
compressed={compressed}>
<EuiValidatableControl isInvalid={isInvalid}>
<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`EuiSearchBox render - custom placeholder and incremental 1`] = `
fullWidth={true}
incremental={true}
inputRef={[Function]}
isClearable={true}
isLoading={false}
onSearch={[Function]}
placeholder="..."
Expand All @@ -26,6 +27,7 @@ exports[`EuiSearchBox render - no config 1`] = `
fullWidth={true}
incremental={false}
inputRef={[Function]}
isClearable={true}
isLoading={false}
onSearch={[Function]}
placeholder="Search..."
Expand Down