react-virtualized-sectionlist
Version:
React SectionList component based in react-virtualized List
182 lines (152 loc) • 6.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactVirtualized = require('react-virtualized');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var SectionList = function (_PureComponent) {
_inherits(SectionList, _PureComponent);
function SectionList(props, context) {
_classCallCheck(this, SectionList);
var _this = _possibleConstructorReturn(this, (SectionList.__proto__ || Object.getPrototypeOf(SectionList)).call(this, props, context));
_this._captureRef = function (ref) {
_this._listRef = ref;
};
_this.getListRef = _this.getListRef.bind(_this);
_this._rowHeight = _this._rowHeight.bind(_this);
_this._renderItem = _this._renderItem.bind(_this);
_this.state = _this._computeState(props);
return _this;
}
_createClass(SectionList, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
this.setState(this._computeState(nextProps));
}
}, {
key: '_computeState',
value: function _computeState(props) {
var itemCount = props.sections.reduce(function (v, section) {
return v + section.data.length + 1; // Add one for the section header.
}, 0);
var sectionHeaderRenderer = props.sectionHeaderRenderer,
sectionHeaderHeight = props.sectionHeaderHeight,
rowRenderer = props.rowRenderer,
rowHeight = props.rowHeight,
childProps = _objectWithoutProperties(props, ['sectionHeaderRenderer', 'sectionHeaderHeight', 'rowRenderer', 'rowHeight']);
return {
childProps: _extends({}, childProps, {
rowCount: itemCount,
rowHeight: this._rowHeight,
rowRenderer: this._renderItem
})
};
}
}, {
key: '_rowHeight',
value: function _rowHeight(_ref) {
var index = _ref.index;
var info = subExtractor(this.props.sections, index);
if (!info) {
return 0;
}
if (!!info.header) {
if (!!this.props.sectionHeaderRenderer) {
if (!!this.props.sectionHeaderHeight) {
return typeof this.props.sectionHeaderHeight === "function" ? this.props.sectionHeaderHeight({ index: index }) : +this.props.sectionHeaderHeight;
}
} else {
return 0;
}
}
return typeof this.props.rowHeight === "function" ? this.props.rowHeight({ index: index }) : +this.props.rowHeight;
}
}, {
key: '_renderItem',
value: function _renderItem(_ref2) {
var key = _ref2.key,
index = _ref2.index,
isScrolling = _ref2.isScrolling,
isVisible = _ref2.isVisible,
style = _ref2.style,
parent = _ref2.parent;
var info = subExtractor(this.props.sections, index);
if (!!info) {
if (!!info.header) {
if (!!this.props.sectionHeaderRenderer) {
return this.props.sectionHeaderRenderer({
key: key,
title: info.section.title || null,
sectionIndex: info.sectionIndex,
isScrolling: isScrolling,
isVisible: isVisible,
parent: parent,
style: style
});
}
} else {
return this.props.rowRenderer({
key: key,
item: info.section.data[info.index],
sectionIndex: info.sectionIndex,
rowIndex: info.index,
isScrolling: isScrolling,
isVisible: isVisible,
parent: parent,
style: style
});
}
}
return null;
}
}, {
key: 'getListRef',
value: function getListRef() {
return this._listRef;
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(_reactVirtualized.List, _extends({}, this.state.childProps, { ref: this._captureRef }));
}
}]);
return SectionList;
}(_react.PureComponent);
SectionList.defaultProps = _extends({}, _reactVirtualized.List.defaultProps, {
sections: []
});
exports.default = SectionList;
function subExtractor() {
var sections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var index = arguments[1];
var itemIndex = index;
for (var ii = 0; ii < sections.length; ii++) {
var section = sections[ii];
itemIndex -= 1; // The section adds an item for the header
if (itemIndex >= section.data.length) {
itemIndex -= section.data.length;
} else if (itemIndex === -1) {
return {
section: section,
sectionIndex: ii,
index: null,
header: true
};
} else {
return {
section: section,
sectionIndex: ii,
index: itemIndex,
header: false
};
}
}
};