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

[EuiComboBox] Fix keyboard selection when sortMatchesBy=startsWith #3823

Merged
merged 4 commits into from
Jul 30, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fixed bug in `EuiPagination` showing wrong page count when `compressed` prop is true. ([#3827](https://github.com/elastic/eui/pull/3827))
- Fixed bug in EUI's input field components where their `inputRef` couldn't be a `RefObject` ([#3822](https://github.com/elastic/eui/pull/3822))
- Moved `react-view` and `html-format` to be `devDependencies` ([#3828](https://github.com/elastic/eui/pull/3828))
- Fix `EuiComboBox` keyboard selection when `sortMatchesBy=startsWith` ([#3823](https://github.com/elastic/eui/pull/3823))

## [`27.3.0`](https://github.com/elastic/eui/tree/v27.3.0)

Expand Down
35 changes: 31 additions & 4 deletions src/components/combo_box/combo_box.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,45 @@ describe('behavior', () => {
});

describe('sortMatchesBy', () => {
test('options startsWith', () => {
const sortMatchesByOptions = [
{
label: 'Something is Disabled',
},
...options,
];
test('options "none"', () => {
const component = mount<
EuiComboBox<TitanOption>,
EuiComboBoxProps<TitanOption>,
{ matchingOptions: TitanOption[] }
>(<EuiComboBox options={options} sortMatchesBy="startsWith" />);
>(<EuiComboBox options={sortMatchesByOptions} sortMatchesBy="none" />);

findTestSubject(component, 'comboBoxSearchInput').simulate('change', {
target: { value: 'e' },
target: { value: 'di' },
});

expect(component.state('matchingOptions')[0].label).toBe('Enceladus');
expect(component.state('matchingOptions')[0].label).toBe(
'Something is Disabled'
);
});

test('options "startsWith"', () => {
const component = mount<
EuiComboBox<TitanOption>,
EuiComboBoxProps<TitanOption>,
{ matchingOptions: TitanOption[] }
>(
<EuiComboBox
options={sortMatchesByOptions}
sortMatchesBy="startsWith"
/>
);

findTestSubject(component, 'comboBoxSearchInput').simulate('change', {
target: { value: 'di' },
});

expect(component.state('matchingOptions')[0].label).toBe('Dione');
});
});

Expand Down
46 changes: 19 additions & 27 deletions src/components/combo_box/combo_box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export class EuiComboBox<T> extends Component<
this.props.selectedOptions,
initialSearchValue,
this.props.async,
Boolean(this.props.singleSelection)
Boolean(this.props.singleSelection),
this.props.sortMatchesBy
),
searchValue: initialSearchValue,
width: 0,
Expand Down Expand Up @@ -789,7 +790,12 @@ export class EuiComboBox<T> extends Component<
nextProps: _EuiComboBoxProps<T>,
prevState: EuiComboBoxState<T>
) {
const { options, selectedOptions, singleSelection } = nextProps;
const {
options,
selectedOptions,
singleSelection,
sortMatchesBy,
} = nextProps;
const { activeOptionIndex, searchValue } = prevState;

// Calculate and cache the options which match the searchValue, because we use this information
Expand All @@ -799,7 +805,8 @@ export class EuiComboBox<T> extends Component<
selectedOptions,
searchValue,
nextProps.async,
Boolean(singleSelection)
Boolean(singleSelection),
sortMatchesBy
);

const stateUpdate: Partial<EuiComboBoxState<T>> = { matchingOptions };
Expand Down Expand Up @@ -857,7 +864,12 @@ export class EuiComboBox<T> extends Component<
};

componentDidUpdate() {
const { options, selectedOptions, singleSelection } = this.props;
const {
options,
selectedOptions,
singleSelection,
sortMatchesBy,
} = this.props;
const { searchValue } = this.state;

// React 16.3 has a bug (fixed in 16.4) where getDerivedStateFromProps
Expand All @@ -869,7 +881,8 @@ export class EuiComboBox<T> extends Component<
selectedOptions,
searchValue,
this.props.async,
Boolean(singleSelection)
Boolean(singleSelection),
sortMatchesBy
)
);
}
Expand Down Expand Up @@ -918,27 +931,6 @@ export class EuiComboBox<T> extends Component<
matchingOptions,
} = this.state;

let newMatchingOptions = matchingOptions;

if (sortMatchesBy === 'startsWith') {
const refObj: {
startWith: Array<EuiComboBoxOptionOption<T>>;
others: Array<EuiComboBoxOptionOption<T>>;
} = { startWith: [], others: [] };

newMatchingOptions.forEach(object => {
if (
object.label
.toLowerCase()
.startsWith(searchValue.trim().toLowerCase())
) {
refObj.startWith.push(object);
} else {
refObj.others.push(object);
}
});
newMatchingOptions = [...refObj.startWith, ...refObj.others];
}
// Visually indicate the combobox is in an invalid state if it has lost focus but there is text entered in the input.
// When custom options are disabled and the user leaves the combo box after entering text that does not match any
// options, this tells the user that they've entered invalid input.
Expand Down Expand Up @@ -975,7 +967,7 @@ export class EuiComboBox<T> extends Component<
fullWidth={fullWidth}
isLoading={isLoading}
listRef={this.listRefCallback}
matchingOptions={newMatchingOptions}
matchingOptions={matchingOptions}
onCloseList={this.closeList}
onCreateOption={onCreateOption}
onOptionClick={this.onOptionClick}
Expand Down
8 changes: 7 additions & 1 deletion src/components/combo_box/matching_options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ interface GetMatchingOptionsTestCase {
searchValue: string;
selectedOptions: EuiComboBoxOptionOption[];
showPrevSelected: boolean;
sortMatchesBy: string;
}

const testCases: GetMatchingOptionsTestCase[] = [
Expand All @@ -123,6 +124,7 @@ const testCases: GetMatchingOptionsTestCase[] = [
isPreFiltered: false,
showPrevSelected: false,
expected: [],
sortMatchesBy: 'none',
},
{
options,
Expand All @@ -139,6 +141,7 @@ const testCases: GetMatchingOptionsTestCase[] = [
{ 'data-test-subj': 'titanOption', label: 'Titan' },
{ label: 'Mimas' },
],
sortMatchesBy: 'none',
},
{
options,
Expand All @@ -152,6 +155,7 @@ const testCases: GetMatchingOptionsTestCase[] = [
isPreFiltered: false,
showPrevSelected: true,
expected: [{ 'data-test-subj': 'saturnOption', label: 'Saturn' }],
sortMatchesBy: 'none',
},
{
options,
Expand All @@ -169,6 +173,7 @@ const testCases: GetMatchingOptionsTestCase[] = [
{ 'data-test-subj': 'saturnOption', label: 'Saturn' },
{ label: 'Mimas' },
],
sortMatchesBy: 'none',
},
];

Expand All @@ -182,7 +187,8 @@ describe('getMatchingOptions', () => {
testCase.selectedOptions,
testCase.searchValue,
testCase.isPreFiltered,
testCase.showPrevSelected
testCase.showPrevSelected,
testCase.sortMatchesBy
)
).toMatchObject(testCase.expected);
}
Expand Down
20 changes: 19 additions & 1 deletion src/components/combo_box/matching_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export const getMatchingOptions = <T>(
selectedOptions: Array<EuiComboBoxOptionOption<T>>,
searchValue: string,
isPreFiltered: boolean,
showPrevSelected: boolean
showPrevSelected: boolean,
sortMatchesBy: string
) => {
const normalizedSearchValue = searchValue.trim().toLowerCase();
const matchingOptions: Array<EuiComboBoxOptionOption<T>> = [];
Expand Down Expand Up @@ -122,5 +123,22 @@ export const getMatchingOptions = <T>(
);
}
});

if (sortMatchesBy === 'startsWith') {
const refObj: {
startWith: Array<EuiComboBoxOptionOption<T>>;
others: Array<EuiComboBoxOptionOption<T>>;
} = { startWith: [], others: [] };

matchingOptions.forEach(object => {
if (object.label.toLowerCase().startsWith(normalizedSearchValue)) {
refObj.startWith.push(object);
} else {
refObj.others.push(object);
}
});
return [...refObj.startWith, ...refObj.others];
}

return matchingOptions;
};