-
Notifications
You must be signed in to change notification settings - Fork 5
/
TermPickerInput.jsx
98 lines (82 loc) · 2.52 KB
/
TermPickerInput.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
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { getDisplayName } from 'cspace-refname';
import SubstringFilteringDropdownMenuInput from './SubstringFilteringDropdownMenuInput';
import PrefixFilteringDropdownMenuInput from './PrefixFilteringDropdownMenuInput';
import withNormalizedOptions from '../enhancers/withNormalizedOptions';
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
...SubstringFilteringDropdownMenuInput.propTypes,
// eslint-disable-next-line react/forbid-foreign-prop-types
...PrefixFilteringDropdownMenuInput.propTypes,
filter: PropTypes.string,
terms: PropTypes.arrayOf(PropTypes.shape({
refName: PropTypes.string,
displayName: PropTypes.string,
})),
onMount: PropTypes.func,
};
const defaultProps = {
filter: 'substring', // or 'prefix'
terms: undefined,
onMount: undefined,
};
const BaseSubstringFilteringDropdownMenuInput = withNormalizedOptions(
SubstringFilteringDropdownMenuInput,
);
const BasePrefixFilteringDropdownMenuInput = withNormalizedOptions(
PrefixFilteringDropdownMenuInput,
);
export default class TermPickerInput extends Component {
componentDidMount() {
const {
onMount,
} = this.props;
if (onMount) {
onMount();
}
}
render() {
const {
filter,
onMount,
terms,
...remainingProps
} = this.props;
const {
value,
} = remainingProps;
let options;
if (terms) {
options = terms.map((term) => {
const option = {
value: term.refName,
label: term.displayName,
};
if (term.termStatus === 'inactive') {
option.disabled = true;
}
return option;
});
} else {
options = [];
}
const selectedOption = options.find((option) => option.value === value);
const valueLabel = selectedOption ? selectedOption.label : getDisplayName(value);
const BaseDropdownMenuInput = (filter === 'prefix')
? BasePrefixFilteringDropdownMenuInput
: BaseSubstringFilteringDropdownMenuInput;
return (
<BaseDropdownMenuInput
options={options}
valueLabel={valueLabel}
// eslint-disable-next-line react/jsx-props-no-spreading
{...remainingProps}
/>
);
}
}
TermPickerInput.propTypes = propTypes;
TermPickerInput.defaultProps = defaultProps;