|
@@ -1,4 +1,5 @@
|
|
|
import React from 'react';
|
|
import React from 'react';
|
|
|
|
|
+import Highlighter from 'react-highlight-words';
|
|
|
|
|
|
|
|
import { Suggestion, SuggestionGroup } from './QueryField';
|
|
import { Suggestion, SuggestionGroup } from './QueryField';
|
|
|
|
|
|
|
@@ -16,6 +17,7 @@ interface TypeaheadItemProps {
|
|
|
isSelected: boolean;
|
|
isSelected: boolean;
|
|
|
item: Suggestion;
|
|
item: Suggestion;
|
|
|
onClickItem: (Suggestion) => void;
|
|
onClickItem: (Suggestion) => void;
|
|
|
|
|
+ prefix?: string;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class TypeaheadItem extends React.PureComponent<TypeaheadItemProps, {}> {
|
|
class TypeaheadItem extends React.PureComponent<TypeaheadItemProps, {}> {
|
|
@@ -38,11 +40,12 @@ class TypeaheadItem extends React.PureComponent<TypeaheadItemProps, {}> {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
render() {
|
|
|
- const { isSelected, item } = this.props;
|
|
|
|
|
|
|
+ const { isSelected, item, prefix } = this.props;
|
|
|
const className = isSelected ? 'typeahead-item typeahead-item__selected' : 'typeahead-item';
|
|
const className = isSelected ? 'typeahead-item typeahead-item__selected' : 'typeahead-item';
|
|
|
|
|
+ const { label } = item;
|
|
|
return (
|
|
return (
|
|
|
<li ref={this.getRef} className={className} onClick={this.onClick}>
|
|
<li ref={this.getRef} className={className} onClick={this.onClick}>
|
|
|
- {item.detail || item.label}
|
|
|
|
|
|
|
+ <Highlighter textToHighlight={label} searchWords={[prefix]} highlightClassName="typeahead-match" />
|
|
|
{item.documentation && isSelected ? <div className="typeahead-item-hint">{item.documentation}</div> : null}
|
|
{item.documentation && isSelected ? <div className="typeahead-item-hint">{item.documentation}</div> : null}
|
|
|
</li>
|
|
</li>
|
|
|
);
|
|
);
|
|
@@ -54,18 +57,25 @@ interface TypeaheadGroupProps {
|
|
|
label: string;
|
|
label: string;
|
|
|
onClickItem: (Suggestion) => void;
|
|
onClickItem: (Suggestion) => void;
|
|
|
selected: Suggestion;
|
|
selected: Suggestion;
|
|
|
|
|
+ prefix?: string;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class TypeaheadGroup extends React.PureComponent<TypeaheadGroupProps, {}> {
|
|
class TypeaheadGroup extends React.PureComponent<TypeaheadGroupProps, {}> {
|
|
|
render() {
|
|
render() {
|
|
|
- const { items, label, selected, onClickItem } = this.props;
|
|
|
|
|
|
|
+ const { items, label, selected, onClickItem, prefix } = this.props;
|
|
|
return (
|
|
return (
|
|
|
<li className="typeahead-group">
|
|
<li className="typeahead-group">
|
|
|
<div className="typeahead-group__title">{label}</div>
|
|
<div className="typeahead-group__title">{label}</div>
|
|
|
<ul className="typeahead-group__list">
|
|
<ul className="typeahead-group__list">
|
|
|
{items.map(item => {
|
|
{items.map(item => {
|
|
|
return (
|
|
return (
|
|
|
- <TypeaheadItem key={item.label} onClickItem={onClickItem} isSelected={selected === item} item={item} />
|
|
|
|
|
|
|
+ <TypeaheadItem
|
|
|
|
|
+ key={item.label}
|
|
|
|
|
+ onClickItem={onClickItem}
|
|
|
|
|
+ isSelected={selected === item}
|
|
|
|
|
+ item={item}
|
|
|
|
|
+ prefix={prefix}
|
|
|
|
|
+ />
|
|
|
);
|
|
);
|
|
|
})}
|
|
})}
|
|
|
</ul>
|
|
</ul>
|
|
@@ -79,14 +89,15 @@ interface TypeaheadProps {
|
|
|
menuRef: any;
|
|
menuRef: any;
|
|
|
selectedItem: Suggestion | null;
|
|
selectedItem: Suggestion | null;
|
|
|
onClickItem: (Suggestion) => void;
|
|
onClickItem: (Suggestion) => void;
|
|
|
|
|
+ prefix?: string;
|
|
|
}
|
|
}
|
|
|
class Typeahead extends React.PureComponent<TypeaheadProps, {}> {
|
|
class Typeahead extends React.PureComponent<TypeaheadProps, {}> {
|
|
|
render() {
|
|
render() {
|
|
|
- const { groupedItems, menuRef, selectedItem, onClickItem } = this.props;
|
|
|
|
|
|
|
+ const { groupedItems, menuRef, selectedItem, onClickItem, prefix } = this.props;
|
|
|
return (
|
|
return (
|
|
|
<ul className="typeahead" ref={menuRef}>
|
|
<ul className="typeahead" ref={menuRef}>
|
|
|
{groupedItems.map(g => (
|
|
{groupedItems.map(g => (
|
|
|
- <TypeaheadGroup key={g.label} onClickItem={onClickItem} selected={selectedItem} {...g} />
|
|
|
|
|
|
|
+ <TypeaheadGroup key={g.label} onClickItem={onClickItem} prefix={prefix} selected={selectedItem} {...g} />
|
|
|
))}
|
|
))}
|
|
|
</ul>
|
|
</ul>
|
|
|
);
|
|
);
|