office-ui-fabric-react
Version: 
Reusable React components for building experiences for Office 365.
186 lines • 10.1 kB
JavaScript
define(["require", "exports", "tslib", "react", "../../Utilities", "./GroupedListSection", "../../List", "../../utilities/selection/index", "./GroupedList.scss"], function (require, exports, tslib_1, React, Utilities_1, GroupedListSection_1, List_1, index_1, stylesImport) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var styles = stylesImport;
    var GroupedList = /** @class */ (function (_super) {
        tslib_1.__extends(GroupedList, _super);
        function GroupedList(props) {
            var _this = _super.call(this, props) || this;
            _this._isSomeGroupExpanded = _this._computeIsSomeGroupExpanded(props.groups);
            _this.state = {
                lastWidth: 0,
                groups: props.groups
            };
            return _this;
        }
        GroupedList.prototype.scrollToIndex = function (index, measureItem) {
            this._list && this._list.scrollToIndex(index, measureItem);
        };
        GroupedList.prototype.componentWillReceiveProps = function (newProps) {
            var _a = this.props, groups = _a.groups, selectionMode = _a.selectionMode;
            var shouldForceUpdates = false;
            if (newProps.groups !== groups) {
                this.setState({ groups: newProps.groups });
                shouldForceUpdates = true;
            }
            if (newProps.selectionMode !== selectionMode) {
                shouldForceUpdates = true;
            }
            if (shouldForceUpdates) {
                this._forceListUpdates();
            }
        };
        GroupedList.prototype.render = function () {
            var _a = this.props, className = _a.className, usePageCache = _a.usePageCache, onShouldVirtualize = _a.onShouldVirtualize;
            var groups = this.state.groups;
            return (React.createElement("div", { className: Utilities_1.css('ms-GroupedList', styles.root, className), "data-automationid": 'GroupedList', "data-is-scrollable": 'false', role: 'presentation' }, !groups ?
                this._renderGroup(null, 0) : (React.createElement(List_1.List, { ref: this._resolveRef('_list'), items: groups, onRenderCell: this._renderGroup, getItemCountForPage: this._returnOne, getPageSpecification: this._getPageSpecification, usePageCache: usePageCache, onShouldVirtualize: onShouldVirtualize }))));
        };
        GroupedList.prototype.forceUpdate = function () {
            _super.prototype.forceUpdate.call(this);
            this._forceListUpdates();
        };
        GroupedList.prototype.toggleCollapseAll = function (allCollapsed) {
            var groups = this.state.groups;
            var groupProps = this.props.groupProps;
            var onToggleCollapseAll = groupProps && groupProps.onToggleCollapseAll;
            if (groups) {
                if (onToggleCollapseAll) {
                    onToggleCollapseAll(allCollapsed);
                }
                for (var groupIndex = 0; groupIndex < groups.length; groupIndex++) {
                    groups[groupIndex].isCollapsed = allCollapsed;
                }
                this._updateIsSomeGroupExpanded();
                this.forceUpdate();
            }
        };
        GroupedList.prototype._renderGroup = function (group, groupIndex) {
            var _a = this.props, dragDropEvents = _a.dragDropEvents, dragDropHelper = _a.dragDropHelper, eventsToRegister = _a.eventsToRegister, groupProps = _a.groupProps, items = _a.items, listProps = _a.listProps, onRenderCell = _a.onRenderCell, selectionMode = _a.selectionMode, selection = _a.selection, viewport = _a.viewport, onShouldVirtualize = _a.onShouldVirtualize;
            // override group header/footer props as needed
            var dividerProps = {
                onToggleSelectGroup: this._onToggleSelectGroup,
                onToggleCollapse: this._onToggleCollapse,
                onToggleSummarize: this._onToggleSummarize
            };
            var headerProps = Utilities_1.assign({}, groupProps.headerProps, dividerProps);
            var showAllProps = Utilities_1.assign({}, groupProps.showAllProps, dividerProps);
            var footerProps = Utilities_1.assign({}, groupProps.footerProps, dividerProps);
            var groupNestingDepth = this._getGroupNestingDepth();
            if (!groupProps.showEmptyGroups && group && group.count === 0) {
                return null;
            }
            return (React.createElement(GroupedListSection_1.GroupedListSection, { ref: 'group_' + groupIndex, key: this._getGroupKey(group, groupIndex), dragDropEvents: dragDropEvents, dragDropHelper: dragDropHelper, eventsToRegister: eventsToRegister, footerProps: footerProps, getGroupItemLimit: groupProps && groupProps.getGroupItemLimit, group: group, groupIndex: groupIndex, groupNestingDepth: groupNestingDepth, headerProps: headerProps, listProps: listProps, items: items, onRenderCell: onRenderCell, onRenderGroupHeader: groupProps.onRenderHeader, onRenderGroupShowAll: groupProps.onRenderShowAll, onRenderGroupFooter: groupProps.onRenderFooter, selectionMode: selectionMode, selection: selection, showAllProps: showAllProps, viewport: viewport, onShouldVirtualize: onShouldVirtualize }));
        };
        GroupedList.prototype._returnOne = function () {
            return 1;
        };
        GroupedList.prototype._getGroupKey = function (group, index) {
            return 'group-' + ((group && group.key) ? group.key : String(index));
        };
        GroupedList.prototype._getGroupNestingDepth = function () {
            var groups = this.state.groups;
            var level = 0;
            var groupsInLevel = groups;
            while (groupsInLevel && groupsInLevel.length > 0) {
                level++;
                groupsInLevel = groupsInLevel[0].children;
            }
            return level;
        };
        GroupedList.prototype._onToggleCollapse = function (group) {
            var groupProps = this.props.groupProps;
            var onToggleCollapse = groupProps && groupProps.headerProps && groupProps.headerProps.onToggleCollapse;
            if (group) {
                if (onToggleCollapse) {
                    onToggleCollapse(group);
                }
                group.isCollapsed = !group.isCollapsed;
                this._updateIsSomeGroupExpanded();
                this.forceUpdate();
            }
        };
        GroupedList.prototype._onToggleSelectGroup = function (group) {
            if (group) {
                this.props.selection.toggleRangeSelected(group.startIndex, group.count);
            }
        };
        GroupedList.prototype._forceListUpdates = function (groups) {
            groups = groups || this.state.groups;
            var groupCount = groups ? groups.length : 1;
            if (this._list) {
                this._list.forceUpdate();
                for (var i = 0; i < groupCount; i++) {
                    var group = this._list.refs['group_' + String(i)];
                    if (group) {
                        group.forceListUpdate();
                    }
                }
            }
            else {
                var group = this.refs['group_' + String(0)];
                if (group) {
                    group.forceListUpdate();
                }
            }
        };
        GroupedList.prototype._onToggleSummarize = function (group) {
            var groupProps = this.props.groupProps;
            var onToggleSummarize = groupProps && groupProps.showAllProps && groupProps.showAllProps.onToggleSummarize;
            if (onToggleSummarize) {
                onToggleSummarize(group);
            }
            else {
                if (group) {
                    group.isShowingAll = !group.isShowingAll;
                }
                this.forceUpdate();
            }
        };
        GroupedList.prototype._getPageSpecification = function (itemIndex, visibleRect) {
            var groups = this.state.groups;
            var pageGroup = groups && groups[itemIndex];
            return {
                key: pageGroup && pageGroup.name
            };
        };
        GroupedList.prototype._computeIsSomeGroupExpanded = function (groups) {
            var _this = this;
            return !!(groups && groups.some(function (group) { return group.children ? _this._computeIsSomeGroupExpanded(group.children) : !group.isCollapsed; }));
        };
        GroupedList.prototype._updateIsSomeGroupExpanded = function () {
            var groups = this.state.groups;
            var onGroupExpandStateChanged = this.props.onGroupExpandStateChanged;
            var newIsSomeGroupExpanded = this._computeIsSomeGroupExpanded(groups);
            if (this._isSomeGroupExpanded !== newIsSomeGroupExpanded) {
                if (onGroupExpandStateChanged) {
                    onGroupExpandStateChanged(newIsSomeGroupExpanded);
                }
                this._isSomeGroupExpanded = newIsSomeGroupExpanded;
            }
        };
        GroupedList.defaultProps = {
            selectionMode: index_1.SelectionMode.multiple,
            isHeaderVisible: true,
            groupProps: {}
        };
        tslib_1.__decorate([
            Utilities_1.autobind
        ], GroupedList.prototype, "_renderGroup", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], GroupedList.prototype, "_onToggleCollapse", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], GroupedList.prototype, "_onToggleSelectGroup", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], GroupedList.prototype, "_onToggleSummarize", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], GroupedList.prototype, "_getPageSpecification", null);
        return GroupedList;
    }(Utilities_1.BaseComponent));
    exports.GroupedList = GroupedList;
});
//# sourceMappingURL=GroupedList.js.map