1 | import invariant from 'invariant';
|
2 | import getStringLabelKey from './getStringLabelKey';
|
3 | import hasOwnProperty from './hasOwnProperty';
|
4 | import { isFunction, isString } from './nodash';
|
5 | function getOptionLabel(option, labelKey) {
|
6 | if (!isString(option) &&
|
7 | (hasOwnProperty(option, 'paginationOption') ||
|
8 | hasOwnProperty(option, 'customOption'))) {
|
9 | return option[getStringLabelKey(labelKey)];
|
10 | }
|
11 | let optionLabel;
|
12 | if (isFunction(labelKey)) {
|
13 | optionLabel = labelKey(option);
|
14 | }
|
15 | else if (isString(option)) {
|
16 | optionLabel = option;
|
17 | }
|
18 | else {
|
19 | optionLabel = option[labelKey];
|
20 | }
|
21 | invariant(isString(optionLabel), 'One or more options does not have a valid label string. Check the ' +
|
22 | '`labelKey` prop to ensure that it matches the correct option key and ' +
|
23 | 'provides a string for filtering and display.');
|
24 | return optionLabel;
|
25 | }
|
26 | export default getOptionLabel;
|