UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

162 lines (137 loc) 6.28 kB
/** @jsx createElement */ 'use strict'; 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 _rax = require('rax'); var _nukeView = require('../../View/index.js'); var _nukeView2 = _interopRequireDefault(_nukeView); var _grid = require('./grid.js'); var _grid2 = _interopRequireDefault(_grid); var _col = require('./col.js'); var _col2 = _interopRequireDefault(_col); 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 MultiRow = function (_Component) { _inherits(MultiRow, _Component); function MultiRow() { _classCallCheck(this, MultiRow); return _possibleConstructorReturn(this, (MultiRow.__proto__ || Object.getPrototypeOf(MultiRow)).apply(this, arguments)); } _createClass(MultiRow, [{ key: 'render', value: function render() { var _props = this.props, _props$style = _props.style, style = _props$style === undefined ? {} : _props$style, _props$renderCell = _props.renderCell, renderCell = _props$renderCell === undefined ? {} : _props$renderCell, _props$renderRow = _props.renderRow, renderRow = _props$renderRow === undefined ? {} : _props$renderRow, _props$dataSource = _props.dataSource, dataSource = _props$dataSource === undefined ? [] : _props$dataSource, others = _objectWithoutProperties(_props, ['style', 'renderCell', 'renderRow', 'dataSource']); return (0, _rax.createElement)( _nukeView2.default, _extends({ style: style }, others), this.getContent() ); } }, { key: 'renderEmpty', value: function renderEmpty() { return (0, _rax.createElement)(_nukeView2.default, null); } }, { key: 'getContent', value: function getContent() { var _props2 = this.props, dataSource = _props2.dataSource, colStyle = _props2.colStyle, gridStyle = _props2.gridStyle, rows = _props2.rows, cells = _props2.cells, renderCell = _props2.renderCell, renderRow = _props2.renderRow; var count = cells || rows; var renderFunc = renderCell || renderRow; var grids = []; var gridDataArr = []; if (count > 1) { for (var i = 0; i < dataSource.length; i++) { var index = Math.floor(i / count); if (i % count == 0) { gridDataArr = []; } gridDataArr.push((0, _rax.createElement)( _col2.default, { style: colStyle }, renderFunc(dataSource[i], i) )); if (i > 0) { if (i < dataSource.length - 1 && (i + 1) % count === 0) { grids.push((0, _rax.createElement)( _grid2.default, { style: gridStyle }, gridDataArr )); } if (i == dataSource.length - 1) { var extraItemForMultiRow = count - dataSource.length % count; if (extraItemForMultiRow > 0 && extraItemForMultiRow < count) { for (var _index = 0; _index < extraItemForMultiRow; _index++) { gridDataArr.push((0, _rax.createElement)( _col2.default, { style: colStyle }, this.renderEmpty() )); } } grids.push((0, _rax.createElement)( _grid2.default, { style: gridStyle }, gridDataArr )); } } } } else { dataSource.map(function (item, index) { grids.push(renderFunc(item, index)); }); } return grids; } }]); return MultiRow; }(_rax.Component); MultiRow.propTypes = { /** * 每行列数 rows of each line */ rows: _rax.PropTypes.number, /** * 渲染每个单元格的方法 render function */ renderRow: _rax.PropTypes.func, /** * 数据源 dataSource */ dataSource: _rax.PropTypes.array, colStyle: _rax.PropTypes.any, gridStyle: _rax.PropTypes.any }; MultiRow.defaultProps = { colStyle: {}, gridStyle: {}, dataSource: [], renderRow: function renderRow() {}, rows: 1 }; exports.default = MultiRow; module.exports = exports['default'];