-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathOptionSegment.js
43 lines (37 loc) · 964 Bytes
/
OptionSegment.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
/*
* OptionSegment
* Component for building custon list options that take advantage of filter result highlighting.
*/
import React from 'react';
import PropTypes from 'prop-types';
import Highlighter from '../Highlighter';
import css from './Selection.css';
const propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array]),
innerText: PropTypes.string,
searchTerm: PropTypes.string,
};
const OptionSegment = ({ searchTerm, innerText, children }) => {
const text = innerText || children;
let rendered;
if (!searchTerm || searchTerm === '' || typeof text !== 'string') {
rendered = text;
} else {
rendered = (
<Highlighter
searchWords={searchTerm.split(' ')}
text={text}
/>
);
}
return (
<div
data-test-selection-option-segment
className={css.optionSegment}
>
{rendered}
</div>
)
};
OptionSegment.propTypes = propTypes;
export default OptionSegment;