UNPKG

1.67 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = void 0;
9
10var _invariant = _interopRequireDefault(require("invariant"));
11
12var _getStringLabelKey = _interopRequireDefault(require("./getStringLabelKey"));
13
14var _hasOwnProperty = _interopRequireDefault(require("./hasOwnProperty"));
15
16var _nodash = require("./nodash");
17
18/**
19 * Retrieves the display string from an option. Options can be the string
20 * themselves, or an object with a defined display string. Anything else throws
21 * an error.
22 */
23function getOptionLabel(option, labelKey) {
24 // Handle internally created options first.
25 if (!(0, _nodash.isString)(option) && ((0, _hasOwnProperty["default"])(option, 'paginationOption') || (0, _hasOwnProperty["default"])(option, 'customOption'))) {
26 return option[(0, _getStringLabelKey["default"])(labelKey)];
27 }
28
29 var optionLabel;
30
31 if ((0, _nodash.isFunction)(labelKey)) {
32 optionLabel = labelKey(option);
33 } else if ((0, _nodash.isString)(option)) {
34 optionLabel = option;
35 } else {
36 // `option` is an object and `labelKey` is a string.
37 optionLabel = option[labelKey];
38 }
39
40 !(0, _nodash.isString)(optionLabel) ? process.env.NODE_ENV !== "production" ? (0, _invariant["default"])(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.') : (0, _invariant["default"])(false) : void 0;
41 return optionLabel;
42}
43
44var _default = getOptionLabel;
45exports["default"] = _default;
\No newline at end of file