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

Cascader: Add clear button #84449

Merged
merged 3 commits into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 32 additions & 7 deletions packages/grafana-ui/src/components/Cascader/Cascader.tsx
Expand Up @@ -6,6 +6,7 @@ import React, { PureComponent } from 'react';
import { SelectableValue } from '@grafana/data';

import { Icon } from '../Icon/Icon';
import { IconButton } from '../IconButton/IconButton';
import { Input } from '../Input/Input';
import { Select } from '../Select/Select';

Expand Down Expand Up @@ -40,6 +41,8 @@ export interface CascaderProps {
disabled?: boolean;
/** ID for the underlying Select/Cascader component */
id?: string;
/** Whether you can clear the selected value or not */
isClearable?: boolean;
}

interface CascaderState {
Expand Down Expand Up @@ -217,8 +220,17 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
};

render() {
const { allowCustomValue, formatCreateLabel, placeholder, width, changeOnSelect, options, disabled, id } =
this.props;
const {
allowCustomValue,
formatCreateLabel,
placeholder,
width,
changeOnSelect,
options,
disabled,
id,
isClearable,
} = this.props;
const { focusCascade, isSearching, rcValue, activeLabel, inputValue } = this.state;

const searchableOptions = this.getSearchableOptions(options);
Expand Down Expand Up @@ -262,11 +274,24 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
onKeyDown={this.onInputKeyDown}
onChange={() => {}}
suffix={
focusCascade ? (
<Icon name="angle-up" />
) : (
<Icon name="angle-down" style={{ marginBottom: 0, marginLeft: '4px' }} />
)
<>
{isClearable && activeLabel !== '' && (
<IconButton
name="times"
aria-label="Clear selection"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
this.setState({ rcValue: [], activeLabel: '', inputValue: '' });
this.props.onSelect('');
}}
/>
)}
<Icon
name={focusCascade ? 'angle-up' : 'angle-down'}
style={{ marginBottom: 0, marginLeft: '4px' }}
Copy link
Contributor

Choose a reason for hiding this comment

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

What about wrapping the suffix into <Stack gap={0.5}> to get rid of these inline styles?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea!!

/>
</>
}
disabled={disabled}
id={id}
Expand Down
Expand Up @@ -62,6 +62,7 @@ export class UnitPicker extends PureComponent<UnitPickerProps> {
formatCreateLabel={formatCreateLabel}
options={groupOptions}
placeholder="Choose"
isClearable
onSelect={this.props.onChange}
/>
);
Expand Down