UNPKG

954 BJavaScriptView Raw
1import invariant from 'invariant';
2import getStringLabelKey from './getStringLabelKey';
3import hasOwnProperty from './hasOwnProperty';
4import { isFunction, isString } from './nodash';
5function 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}
26export default getOptionLabel;