UNPKG

@enum-plus/plugin-antd

Version:

A plugin for enum-plus that provides functionality to bind enums to Ant Design

39 lines 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const enum_plus_1 = require("enum-plus"); const toSelectPlugin = (options = {}, Enum) => { const { labelField: globalLabelField, valueField: globalValueField } = options; Enum.extends({ toSelect(config) { var _a, _b; const { firstOption, labelField = globalLabelField || 'label', valueField = globalValueField || 'value', } = config !== null && config !== void 0 ? config : {}; const selectItems = this.items.map((item) => { const valueFieldName = typeof valueField === 'function' ? valueField(item) : valueField; const labelFieldName = typeof labelField === 'function' ? labelField(item) : labelField; return { [valueFieldName]: item.value, [labelFieldName]: item.label, }; }); if (firstOption) { const label = firstOption[labelField]; let newLabel = label; if (label) { const localize = (_b = (_a = this[enum_plus_1.ENUM_OPTIONS]) === null || _a === void 0 ? void 0 : _a.localize) !== null && _b !== void 0 ? _b : Enum.localize; if (localize) { newLabel = localize(label); } } return [ Object.assign(Object.assign({}, firstOption), { [labelField]: newLabel }), ...selectItems, ]; } else { return Array.from(selectItems); } }, }); }; exports.default = toSelectPlugin; //# sourceMappingURL=../../src/toSelect.js.map