-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwithNormalizedOptions.jsx
68 lines (58 loc) · 1.86 KB
/
withNormalizedOptions.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { normalizeOptions } from '../helpers/optionHelpers';
/**
* Normalizes options before passing them on to the base component.
* @param {string|function} BaseComponent - The component to enhance.
* @returns {function} The enhanced component.
*/
export default function withNormalizedOptions(BaseComponent) {
const baseComponentName = BaseComponent.displayName
|| BaseComponent.name
|| 'Component';
const propTypes = {
// TODO: Stop using propTypes in isInput, and in render method of cspace-ui Field component.
// Until then, propTypes need to be hoisted from the base component.
// eslint-disable-next-line react/forbid-foreign-prop-types
...BaseComponent.propTypes,
blankable: PropTypes.bool,
prefilter: PropTypes.func,
options: PropTypes.arrayOf(PropTypes.shape({
value: PropTypes.string,
})),
sortComparator: PropTypes.func,
};
const defaultProps = {
blankable: true,
options: undefined,
prefilter: undefined,
sortComparator: undefined,
};
function WithNormalizedOptions(props) {
const {
blankable,
options,
prefilter,
sortComparator,
...remainingProps
} = props;
let normalizedOptions = normalizeOptions(options, blankable);
if (prefilter) {
normalizedOptions = normalizedOptions.filter(prefilter);
}
if (sortComparator) {
normalizedOptions.sort(sortComparator);
}
return (
<BaseComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...remainingProps}
options={normalizedOptions}
/>
);
}
WithNormalizedOptions.propTypes = propTypes;
WithNormalizedOptions.defaultProps = defaultProps;
WithNormalizedOptions.displayName = `withNormalizedOptions(${baseComponentName})`;
return WithNormalizedOptions;
}