forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaces_menu.js
127 lines (109 loc) · 3.02 KB
/
spaces_menu.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
EuiContextMenuPanel,
EuiContextMenuItem,
EuiFieldSearch,
EuiButton,
} from '@elastic/eui';
import { SpaceAvatar } from '../../components/space_avatar';
import { SPACE_SEARCH_COUNT_THRESHOLD } from '../../../../common/constants';
import './spaces_menu.less';
export class SpacesMenu extends Component {
state = {
searchTerm: ''
}
render() {
const {
searchTerm
} = this.state;
let items = this.getVisibleSpaces(searchTerm).map(this.renderSpaceMenuItem);
if (this.props.spaces.length >= SPACE_SEARCH_COUNT_THRESHOLD) {
items = [this.renderSearchField(), this.renderSpacesListPanel(items, searchTerm)];
}
items.push(this.renderManageButton());
return (
<EuiContextMenuPanel
className="spacesMenu"
title={'Change current space'}
watchedItemProps={['data-search-term']}
items={items}
/>
);
}
getVisibleSpaces = (searchTerm) => {
const {
spaces,
} = this.props;
let filteredSpaces = spaces;
if (searchTerm) {
filteredSpaces = spaces
.filter(space => {
const {
name,
description = ''
} = space;
return name.toLowerCase().indexOf(searchTerm) >= 0 || (description.toLowerCase().indexOf(searchTerm) >= 0);
});
}
return filteredSpaces;
}
renderSpacesListPanel = (items, searchTerm) => {
return (
<EuiContextMenuPanel
key={`spacesMenuList`}
data-search-term={searchTerm}
className="spacesMenu__spacesList"
items={items}
/>
);
}
renderSearchField = () => {
return (
<EuiContextMenuItem key="spacesMenuSearchField">
<EuiFieldSearch
className="spacesMenu__searchField"
placeholder="Find a space"
incremental={true}
onSearch={this.onSearch}
compressed
/>
</EuiContextMenuItem>
);
}
renderManageButton = () => {
return (
<EuiContextMenuItem>
<EuiButton size="s" style={{ width: `100%` }}>Manage spaces</EuiButton>
</EuiContextMenuItem>
);
}
onSearch = (searchTerm) => {
this.setState({
searchTerm: searchTerm.trim().toLowerCase()
});
}
renderSpaceMenuItem = (space) => {
const icon = <SpaceAvatar space={space} size={'s'} />;
return (
<EuiContextMenuItem
key={space.id}
icon={icon}
onClick={this.props.onSelectSpace.bind(this, space)}
toolTipTitle={space.description && space.name}
toolTipContent={space.description}
>
{space.name}
</EuiContextMenuItem>
);
}
}
SpacesMenu.propTypes = {
spaces: PropTypes.array.isRequired,
onSelectSpace: PropTypes.func.isRequired,
};