-
Notifications
You must be signed in to change notification settings - Fork 295
/
ItemsDropdown.tsx
77 lines (73 loc) · 2.16 KB
/
ItemsDropdown.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
67
68
69
70
71
72
73
74
75
76
77
// @flow
import React, { Component } from 'react';
import classnames from 'classnames';
import { filter, escapeRegExp } from 'lodash';
import { intlShape } from 'react-intl';
import { Select } from 'react-polymorph/lib/components/Select';
import { SelectSkin } from 'react-polymorph/lib/skins/simple/SelectSkin';
import ItemDropdownOption from './ItemDropdownOption';
import type { ItemDropdown } from './ItemDropdownOption';
import styles from './ItemsDropdown.scss';
import globalMessages from '../../../i18n/global-messages';
/**
*
* This component extends the React Polymorph's Select component
* Any prop from it can be used
* Reference:
* https://github.com/input-output-hk/react-polymorph/blob/develop/source/components/Select.js
*
* For a simple usage, it only requires an `option` array, with an extra `detail` item:
*
* [
* {
* label: 'Top content',
* detail: 'Bottom content',
* value: '...'
* }
* ]
*
*/
export type ItemDropdownProps = {
options: Array<ItemDropdown>,
className?: string,
};
export const onSearchItemsDropdown = (
searchValue: string,
options: Array<any>
) => {
return filter(options, (option) => {
const { label, detail, value } = option;
const regex = new RegExp(escapeRegExp(searchValue), 'i');
return regex.test(label) || regex.test(detail) || regex.test(value);
});
};
export default class ItemsDropdown extends Component<ItemDropdownProps> {
static contextTypes = {
intl: intlShape.isRequired,
};
static defaultProps = {
optionRenderer: (optionProps: ItemDropdown) => (
<ItemDropdownOption {...optionProps} />
),
selectionRenderer: (optionProps: ItemDropdown) => (
<ItemDropdownOption selected {...optionProps} />
),
onSearch: onSearchItemsDropdown,
skin: SelectSkin,
};
render() {
const { intl } = this.context;
const { className } = this.props;
const componentStyles = classnames([styles.component, className]);
return (
<Select
{...this.props}
className={componentStyles}
optionHeight={50}
noResultsMessage={intl.formatMessage(
globalMessages.searchNoResultsMessage
)}
/>
);
}
}