@pinuts/bsr-uikit-relaunch
Version:
BSR UI-KIT Relaunch
41 lines (40 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _reactI18next = require("react-i18next");
var _i18next = _interopRequireDefault(require("i18next"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
*
* @param {Array} [options] - The options for the field. Can be an array of options or a string representing a key in the optionLists.
* @param {string} [emptyOptionLabel] - The label for the empty option.
* @param {Array|string} [skipOptions] - An array of option values to be skipped.
* @return {({label: *, value: *}|(*&{label: string})|{label}|*|{label, value: *}|{label: *, value: *}|undefined)[]}
*/
const useGetOptionList = function () {
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
let emptyOptionLabel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
let skipOptions = arguments.length > 2 ? arguments[2] : undefined;
const [t] = (0, _reactI18next.useTranslation)();
skipOptions = [].concat(skipOptions || []);
return (0, _react.useMemo)(() => options.map(opt => {
if (typeof opt === 'string') {
return {
value: opt,
label: opt
};
}
let label = typeof opt.label === 'object' ? opt.label[_i18next.default.language] : opt.label || opt.value;
if (opt.value === '' && !label) {
label = emptyOptionLabel || t('bsrItems.pleaseChoose');
}
return {
...opt,
label: label
};
}).filter(i => typeof i === 'object').filter(opt => !skipOptions.includes(opt.value)), [options, emptyOptionLabel, skipOptions, t]);
};
var _default = exports.default = useGetOptionList;