UNPKG

@fluentui/react

Version:

Reusable React components for building web experiences.

91 lines 4 kB
define(["require", "exports", "tslib", "../Dropdown.types"], function (require, exports, tslib_1, Dropdown_types_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DropdownSizePosCache = void 0; /** * A utility class to cache size and position in cache. * * Dropdown options has non-selectable display types. It is therefore not cheap to determine * the total number of actual selectable options as well as the position an option is in the * list of options - O(n) cost for each lookup. * * Given that we potentially have to make this determination on every single render pass, this * cache should provide a little bit of relief. */ var DropdownSizePosCache = /** @class */ (function () { function DropdownSizePosCache() { this._size = 0; } /** * Invalidates the cache and recalculate the size of selectable options. */ DropdownSizePosCache.prototype.updateOptions = function (options) { var displayOnlyOptionsCache = []; var notSelectableOptionsCache = []; var size = 0; for (var i = 0; i < options.length; i++) { var _a = options[i], itemType = _a.itemType, hidden = _a.hidden; if (itemType === Dropdown_types_1.DropdownMenuItemType.Divider || itemType === Dropdown_types_1.DropdownMenuItemType.Header) { displayOnlyOptionsCache.push(i); notSelectableOptionsCache.push(i); } else if (hidden) { notSelectableOptionsCache.push(i); } else { size++; } } this._size = size; this._displayOnlyOptionsCache = displayOnlyOptionsCache; this._notSelectableOptionsCache = notSelectableOptionsCache; this._cachedOptions = tslib_1.__spreadArray([], options, true); }; Object.defineProperty(DropdownSizePosCache.prototype, "optionSetSize", { /** * The size of all the selectable options. */ get: function () { return this._size; }, enumerable: false, configurable: true }); Object.defineProperty(DropdownSizePosCache.prototype, "cachedOptions", { /** * The chached options array. */ get: function () { return this._cachedOptions; }, enumerable: false, configurable: true }); /** * Returns the position of this option element relative to the full set of selectable option elements. * Note: the first selectable element is position 1 in the set. * @param index The raw index of the option element. */ DropdownSizePosCache.prototype.positionInSet = function (index) { if (index === undefined) { return undefined; } // we could possibly memoize this too but this should be good enough, most of the time (the expectation is that // when you have a lot of options, the selectable options will heavily dominate over the non-selectable options. var offset = 0; while (index > this._notSelectableOptionsCache[offset]) { offset++; } if (this._displayOnlyOptionsCache[offset] === index) { throw new Error("Unexpected: Option at index ".concat(index, " is not a selectable element.")); } if (this._notSelectableOptionsCache[offset] === index) { return undefined; } return index - offset + 1; }; return DropdownSizePosCache; }()); exports.DropdownSizePosCache = DropdownSizePosCache; }); //# sourceMappingURL=DropdownSizePosCache.js.map