UNPKG

1.24 kBJavaScriptView Raw
1import invariant from 'invariant';
2import getStringLabelKey from './getStringLabelKey';
3import hasOwnProperty from './hasOwnProperty';
4import { isFunction, isString } from './nodash';
5
6/**
7 * Retrieves the display string from an option. Options can be the string
8 * themselves, or an object with a defined display string. Anything else throws
9 * an error.
10 */
11function getOptionLabel(option, labelKey) {
12 // Handle internally created options first.
13 if (!isString(option) && (hasOwnProperty(option, 'paginationOption') || hasOwnProperty(option, 'customOption'))) {
14 return option[getStringLabelKey(labelKey)];
15 }
16
17 var optionLabel;
18
19 if (isFunction(labelKey)) {
20 optionLabel = labelKey(option);
21 } else if (isString(option)) {
22 optionLabel = option;
23 } else {
24 // `option` is an object and `labelKey` is a string.
25 optionLabel = option[labelKey];
26 }
27
28 !isString(optionLabel) ? process.env.NODE_ENV !== "production" ? invariant(false, 'One or more options does not have a valid label string. Check the ' + '`labelKey` prop to ensure that it matches the correct option key and ' + 'provides a string for filtering and display.') : invariant(false) : void 0;
29 return optionLabel;
30}
31
32export default getOptionLabel;
\No newline at end of file