Skip to content
Snippets Groups Projects
Unverified Commit 131ff8f7 authored by Jack Reed's avatar Jack Reed Committed by GitHub
Browse files

Merge pull request #3355 from dbmdz/contentsearch-fixes

Fixes for Content Search panel
parents 8218a6b0 292c346c
No related merge requests found
...@@ -2,6 +2,7 @@ import React, { Component } from 'react'; ...@@ -2,6 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import deburr from 'lodash/deburr'; import deburr from 'lodash/deburr';
import debounce from 'lodash/debounce'; import debounce from 'lodash/debounce';
import isObject from 'lodash/isObject';
import Autocomplete from '@material-ui/lab/Autocomplete'; import Autocomplete from '@material-ui/lab/Autocomplete';
import CircularProgress from '@material-ui/core/CircularProgress'; import CircularProgress from '@material-ui/core/CircularProgress';
import TextField from '@material-ui/core/TextField'; import TextField from '@material-ui/core/TextField';
...@@ -9,6 +10,10 @@ import SearchIcon from '@material-ui/icons/SearchSharp'; ...@@ -9,6 +10,10 @@ import SearchIcon from '@material-ui/icons/SearchSharp';
import MiradorMenuButton from '../containers/MiradorMenuButton'; import MiradorMenuButton from '../containers/MiradorMenuButton';
import SearchPanelNavigation from '../containers/SearchPanelNavigation'; 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 { export class SearchPanelControls extends Component {
/** */ /** */
...@@ -25,8 +30,7 @@ export class SearchPanelControls extends Component { ...@@ -25,8 +30,7 @@ export class SearchPanelControls extends Component {
} }
/** /**
* Set the component's local search state * Update the query in the component state if the query has changed in the redux store
* to blank when the query has been cleared
*/ */
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
const { query } = this.props; const { query } = this.props;
...@@ -41,6 +45,11 @@ export class SearchPanelControls extends Component { ...@@ -41,6 +45,11 @@ export class SearchPanelControls extends Component {
/** */ /** */
handleChange(event, value, reason) { handleChange(event, value, reason) {
// For some reason the value gets reset to an empty value from the
// useAutocomplete hook sometimes, we just ignore these cases
if (reason === 'reset' && !value) {
return;
}
this.setState({ this.setState({
search: value, search: value,
suggestions: [], suggestions: [],
...@@ -93,8 +102,8 @@ export class SearchPanelControls extends Component { ...@@ -93,8 +102,8 @@ export class SearchPanelControls extends Component {
/** */ /** */
selectItem(_event, selectedItem, _reason) { selectItem(_event, selectedItem, _reason) {
if (selectedItem && selectedItem.match) { if (selectedItem && getMatch(selectedItem)) {
this.setState({ search: selectedItem.match }, this.submitSearch); this.setState({ search: getMatch(selectedItem) }, this.submitSearch);
} }
} }
...@@ -113,14 +122,15 @@ export class SearchPanelControls extends Component { ...@@ -113,14 +122,15 @@ export class SearchPanelControls extends Component {
id={id} id={id}
inputValue={search} inputValue={search}
options={suggestions} options={suggestions}
getOptionLabel={option => option.match} getOptionLabel={getMatch}
getOptionSelected={(option, value) => ( getOptionSelected={(option, value) => (
deburr(option.match.trim()).toLowerCase() deburr(getMatch(option).trim()).toLowerCase()
=== deburr(value.match.trim()).toLowerCase() === deburr(getMatch(value).trim()).toLowerCase()
)} )}
noOptionsText="" noOptionsText=""
onChange={this.selectItem} onChange={this.selectItem}
onInputChange={this.handleChange} onInputChange={this.handleChange}
freeSolo
renderInput={params => ( renderInput={params => (
<TextField <TextField
{...params} {...params}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment