Skip to content

Commit

Permalink
Cascader: Don't lose input value when typing (#84274)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoSilvaGrafana committed Mar 12, 2024
1 parent d63bc6d commit 6080a9c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/grafana-ui/src/components/Cascader/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface CascaderState {
//Array for cascade navigation
rcValue: SelectableValue<string[]>;
activeLabel: string;
inputValue: string;
}

export interface CascaderOption {
Expand Down Expand Up @@ -84,6 +85,7 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
focusCascade: false,
rcValue,
activeLabel,
inputValue: '',
};
}

Expand Down Expand Up @@ -141,6 +143,7 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
focusCascade: true,
activeLabel,
isSearching: false,
inputValue: activeLabel,
};
this.setState(state);
this.props.onSelect(selectedOptions[selectedOptions.length - 1].value);
Expand All @@ -152,6 +155,7 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
const activeLabel = this.props.displayAllSelectedLevels ? obj.label : obj.singleLabel || '';
const state: CascaderState = {
activeLabel: activeLabel,
inputValue: activeLabel,
rcValue: { value: valueArray, label: activeLabel },
isSearching: false,
focusCascade: false,
Expand All @@ -163,6 +167,7 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
onCreateOption = (value: string) => {
this.setState({
activeLabel: value,
inputValue: value,
rcValue: [],
isSearching: false,
});
Expand Down Expand Up @@ -192,26 +197,26 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
};

onInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (['ArrowDown', 'ArrowUp', 'Enter', 'ArrowLeft', 'ArrowRight', 'Backspace'].includes(e.key)) {
if (['ArrowDown', 'ArrowUp', 'Enter', 'ArrowLeft', 'ArrowRight'].includes(e.key)) {
return;
}
const { activeLabel } = this.state;
this.setState({
focusCascade: false,
isSearching: true,
inputValue: activeLabel,
});
};

onSelectInputChange = (value: string) => {
if (value === '') {
this.setState({
isSearching: false,
});
}
this.setState({
inputValue: value,
});
};

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

const searchableOptions = this.getSearchableOptions(options);

Expand All @@ -230,6 +235,7 @@ export class Cascader extends PureComponent<CascaderProps, CascaderState> {
width={width}
onInputChange={this.onSelectInputChange}
disabled={disabled}
inputValue={inputValue}
/>
) : (
<RCCascader
Expand Down

0 comments on commit 6080a9c

Please sign in to comment.