Skip to content
Snippets Groups Projects
Commit 292c346c authored by Johannes Baiter's avatar Johannes Baiter
Browse files

Fix crashes in content search autocomplete code

parent 89cc8854
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import deburr from 'lodash/deburr';
import debounce from 'lodash/debounce';
import isObject from 'lodash/isObject';
import Autocomplete from '@material-ui/lab/Autocomplete';
import CircularProgress from '@material-ui/core/CircularProgress';
import TextField from '@material-ui/core/TextField';
......@@ -9,6 +10,10 @@ import SearchIcon from '@material-ui/icons/SearchSharp';
import MiradorMenuButton from '../containers/MiradorMenuButton';
import SearchPanelNavigation from '../containers/SearchPanelNavigation';
/** Sometimes an autocomplete match can be a simple string, other times an object
with a `match` property, this function abstracts that away */
const getMatch = (option) => (isObject(option) ? option.match : option);
/** */
export class SearchPanelControls extends Component {
/** */
......@@ -25,8 +30,7 @@ export class SearchPanelControls extends Component {
}
/**
* Set the component's local search state
* to blank when the query has been cleared
* Update the query in the component state if the query has changed in the redux store
*/
componentDidUpdate(prevProps) {
const { query } = this.props;
......@@ -98,8 +102,8 @@ export class SearchPanelControls extends Component {
/** */
selectItem(_event, selectedItem, _reason) {
if (selectedItem && selectedItem.match) {
this.setState({ search: selectedItem.match }, this.submitSearch);
if (selectedItem && getMatch(selectedItem)) {
this.setState({ search: getMatch(selectedItem) }, this.submitSearch);
}
}
......@@ -118,15 +122,15 @@ export class SearchPanelControls extends Component {
id={id}
inputValue={search}
options={suggestions}
getOptionLabel={option => option.match}
getOptionLabel={getMatch}
getOptionSelected={(option, value) => (
deburr(option.match.trim()).toLowerCase()
=== deburr(value.match.trim()).toLowerCase()
deburr(getMatch(option).trim()).toLowerCase()
=== deburr(getMatch(value).trim()).toLowerCase()
)}
noOptionsText=""
onChange={this.selectItem}
onInputChange={this.handleChange}
freeSolo={true}
freeSolo
renderInput={params => (
<TextField
{...params}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment