-
Notifications
You must be signed in to change notification settings - Fork 14
/
ResultsItem.tsx
66 lines (57 loc) · 1.64 KB
/
ResultsItem.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as React from 'react';
import { AnchorItem } from './modifiers/anchor';
import AnchorRenderer from './modifiers/anchor/AnchorRenderer';
interface Props<T> {
// results renderer function
children: Omnibar.ResultRenderer<T>;
// the item
item: T;
// onMouseEnter item callback
onMouseEnter?: (e: any /* Event */) => void;
// onMouseLeave item callback
onMouseLeave?: (e: any /* Event */) => void;
// onClick callback
onClickItem?: (e: any /* Event */) => void;
// set to true if the item is currently selected
isSelected?: boolean;
// optional style override
style?: React.CSSProperties;
}
interface State {
// set to true to highlight
isHighlighted: boolean;
}
export default class ResultsItem<T> extends React.PureComponent<
Props<T>,
State
> {
static defaultProps = {
isSelected: false,
};
state: State = {
isHighlighted: false,
};
handleMouseEnter = (evt: any /* Event */) => {
this.setState({ isHighlighted: true });
this.props.onMouseEnter && this.props.onMouseEnter(evt);
};
handleMouseLeave = (evt: any /* Event */) => {
this.setState({ isHighlighted: false });
this.props.onMouseLeave && this.props.onMouseLeave(evt);
};
render() {
const item = this.props.item;
const renderer = this.props.children
? this.props.children
: (AnchorRenderer as Omnibar.ResultRenderer<T>);
return renderer({
style: this.props.style,
item,
isSelected: this.props.isSelected,
isHighlighted: this.state.isHighlighted,
onMouseEnter: this.handleMouseEnter,
onMouseLeave: this.handleMouseLeave,
onClick: this.props.onClickItem,
});
}
}