UNPKG

react-bootstrap-table-next

Version:
197 lines (157 loc) 8.04 kB
'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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _operators = require('../store/operators'); var _operators2 = _interopRequireDefault(_operators); var _utils = require('../utils'); var _utils2 = _interopRequireDefault(_utils); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } 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; } /* eslint camelcase: 0 */ /* eslint react/prop-types: 0 */ var RowExpandContext = _react2.default.createContext(); var RowExpandProvider = function (_React$Component) { _inherits(RowExpandProvider, _React$Component); function RowExpandProvider() { var _ref; var _temp, _this, _ret; _classCallCheck(this, RowExpandProvider); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = RowExpandProvider.__proto__ || Object.getPrototypeOf(RowExpandProvider)).call.apply(_ref, [this].concat(args))), _this), _this.state = { expanded: _this.props.expandRow.expanded || [], isClosing: _this.props.expandRow.isClosing || [] }, _this.onClosed = function (closedRow) { _this.setState({ isClosing: _this.state.isClosing.filter(function (value) { return value !== closedRow; }) }); }, _this.handleRowExpand = function (rowKey, expanded, rowIndex, e) { var _this$props = _this.props, data = _this$props.data, keyField = _this$props.keyField, _this$props$expandRow = _this$props.expandRow, onExpand = _this$props$expandRow.onExpand, onlyOneExpanding = _this$props$expandRow.onlyOneExpanding, nonExpandable = _this$props$expandRow.nonExpandable; if (nonExpandable && _utils2.default.contains(nonExpandable, rowKey)) { return; } var currExpanded = [].concat(_toConsumableArray(_this.state.expanded)); var isClosing = [].concat(_toConsumableArray(_this.state.isClosing)); if (expanded) { if (onlyOneExpanding) { isClosing = isClosing.concat(currExpanded); currExpanded = [rowKey]; } else currExpanded.push(rowKey); } else { isClosing.push(rowKey); currExpanded = currExpanded.filter(function (value) { return value !== rowKey; }); } if (onExpand) { var row = _operators2.default.getRowByRowId(data, keyField, rowKey); onExpand(row, expanded, rowIndex, e); } _this.setState(function () { return { expanded: currExpanded, isClosing: isClosing }; }); }, _this.handleAllRowExpand = function (e, expandAll) { var _this$props2 = _this.props, data = _this$props2.data, keyField = _this$props2.keyField, _this$props2$expandRo = _this$props2.expandRow, onExpandAll = _this$props2$expandRo.onExpandAll, nonExpandable = _this$props2$expandRo.nonExpandable; var expanded = _this.state.expanded; var currExpanded = void 0; if (expandAll) { currExpanded = expanded.concat(_operators2.default.expandableKeys(data, keyField, nonExpandable)); } else { currExpanded = expanded.filter(function (s) { return typeof data.find(function (d) { return _utils2.default.get(d, keyField) === s; }) === 'undefined'; }); } if (onExpandAll) { onExpandAll(expandAll, _operators2.default.getExpandedRows(data, keyField, currExpanded), e); } _this.setState(function () { return { expanded: currExpanded }; }); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(RowExpandProvider, [{ key: 'UNSAFE_componentWillReceiveProps', value: function UNSAFE_componentWillReceiveProps(nextProps) { var _this2 = this; if (nextProps.expandRow) { var nextExpanded = [].concat(_toConsumableArray(nextProps.expandRow.expanded || this.state.expanded)); var _nextProps$expandRow$ = nextProps.expandRow.nonExpandable, nonExpandable = _nextProps$expandRow$ === undefined ? [] : _nextProps$expandRow$; nextExpanded = nextExpanded.filter(function (rowId) { return !_utils2.default.contains(nonExpandable, rowId); }); var isClosing = this.state.expanded.reduce(function (acc, cur) { if (!_utils2.default.contains(nextExpanded, cur)) { acc.push(cur); } return acc; }, []); this.setState(function () { return { expanded: nextExpanded, isClosing: isClosing }; }); } else { this.setState(function () { return { expanded: _this2.state.expanded }; }); } } }, { key: 'render', value: function render() { var _props = this.props, data = _props.data, keyField = _props.keyField; return _react2.default.createElement( RowExpandContext.Provider, { value: _extends({}, this.props.expandRow, { nonExpandable: this.props.expandRow.nonExpandable, expanded: this.state.expanded, isClosing: this.state.isClosing, onClosed: this.onClosed, isAnyExpands: _operators2.default.isAnyExpands(data, keyField, this.state.expanded), onRowExpand: this.handleRowExpand, onAllRowExpand: this.handleAllRowExpand }) }, this.props.children ); } }]); return RowExpandProvider; }(_react2.default.Component); RowExpandProvider.propTypes = { children: _propTypes2.default.node.isRequired, data: _propTypes2.default.array.isRequired, keyField: _propTypes2.default.string.isRequired }; exports.default = { Provider: RowExpandProvider, Consumer: RowExpandContext.Consumer };