react-bootstrap-table-next
Version:
Next generation of react-bootstrap-table
434 lines (367 loc) • 16.3 kB
JavaScript
'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 _events = require('events');
var _events2 = _interopRequireDefault(_events);
var _utils = require('../utils');
var _utils2 = _interopRequireDefault(_utils);
var _dataContext = require('./data-context');
var _dataContext2 = _interopRequireDefault(_dataContext);
var _columnContext = require('./column-context');
var _columnContext2 = _interopRequireDefault(_columnContext);
var _sortContext = require('./sort-context');
var _sortContext2 = _interopRequireDefault(_sortContext);
var _selectionContext = require('./selection-context');
var _selectionContext2 = _interopRequireDefault(_selectionContext);
var _rowExpandContext = require('./row-expand-context');
var _rowExpandContext2 = _interopRequireDefault(_rowExpandContext);
var _remoteResolver2 = require('../props-resolver/remote-resolver');
var _remoteResolver3 = _interopRequireDefault(_remoteResolver2);
var _bootstrap = require('./bootstrap');
var _operators = require('../store/operators');
var _operators2 = _interopRequireDefault(_operators);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 no-return-assign: 0 */
/* eslint no-param-reassign: 0 */
/* eslint class-methods-use-this: 0 */
var withContext = function withContext(Base) {
return function (_remoteResolver) {
_inherits(BootstrapTableContainer, _remoteResolver);
function BootstrapTableContainer(props) {
_classCallCheck(this, BootstrapTableContainer);
var _this = _possibleConstructorReturn(this, (BootstrapTableContainer.__proto__ || Object.getPrototypeOf(BootstrapTableContainer)).call(this, props));
_this.DataContext = (0, _dataContext2.default)();
if (props.registerExposedAPI) {
var exposedAPIEmitter = new _events2.default();
exposedAPIEmitter.on('get.table.data', function (payload) {
return payload.result = _this.table.getData();
});
exposedAPIEmitter.on('get.selected.rows', function (payload) {
return payload.result = _this.selectionContext.getSelected();
});
exposedAPIEmitter.on('get.filtered.rows', function (payload) {
if (_this.searchContext) {
payload.result = _this.searchContext.getSearched();
} else if (_this.filterContext) {
payload.result = _this.filterContext.getFiltered();
} else {
payload.result = _this.table.getData();
}
});
props.registerExposedAPI(exposedAPIEmitter);
}
if (props.columns.filter(function (col) {
return col.sort;
}).length > 0) {
_this.SortContext = (0, _sortContext2.default)(_operators2.default, _this.isRemoteSort, _this.handleRemoteSortChange);
}
if (props.columnToggle || props.columns.filter(function (col) {
return col.hidden;
}).length > 0) {
_this.ColumnManagementContext = (0, _columnContext2.default)();
}
if (props.selectRow) {
_this.SelectionContext = _selectionContext2.default;
}
if (props.expandRow) {
_this.RowExpandContext = _rowExpandContext2.default;
}
if (props.cellEdit && props.cellEdit.createContext) {
_this.CellEditContext = props.cellEdit.createContext(_utils2.default, _operators2.default, _this.isRemoteCellEdit, _this.handleRemoteCellChange);
}
if (props.filter) {
_this.FilterContext = props.filter.createContext(_utils2.default, _this.isRemoteFiltering, _this.handleRemoteFilterChange);
}
if (props.pagination) {
_this.PaginationContext = props.pagination.createContext();
}
if (props.search && props.search.searchContext) {
_this.SearchContext = props.search.searchContext(_utils2.default, _this.isRemoteSearch, _this.handleRemoteSearchChange);
}
if (props.setDependencyModules) {
props.setDependencyModules(_utils2.default);
}
if (props.setPaginationRemoteEmitter) {
props.setPaginationRemoteEmitter(_this.remoteEmitter);
}
return _this;
}
_createClass(BootstrapTableContainer, [{
key: 'UNSAFE_componentWillReceiveProps',
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.columns.filter(function (col) {
return col.sort;
}).length <= 0) {
this.SortContext = null;
} else if (!this.SortContext) {
this.SortContext = (0, _sortContext2.default)(_operators2.default, this.isRemoteSort, this.handleRemoteSortChange);
}
if (!nextProps.pagination && this.props.pagination) {
this.PaginationContext = null;
}
if (nextProps.pagination && !this.props.pagination) {
this.PaginationContext = nextProps.pagination.createContext(this.isRemotePagination, this.handleRemotePageChange);
}
if (!nextProps.cellEdit && this.props.cellEdit) {
this.CellEditContext = null;
}
if (nextProps.cellEdit && !this.props.cellEdit) {
this.CellEditContext = nextProps.cellEdit.createContext(_utils2.default, _operators2.default, this.isRemoteCellEdit, this.handleRemoteCellChange);
}
}
}, {
key: 'renderBase',
value: function renderBase() {
var _this2 = this;
return function (rootProps, filterProps, searchProps, sortProps, paginationProps, columnToggleProps) {
return _react2.default.createElement(Base, _extends({
ref: function ref(n) {
return _this2.table = n;
}
}, _this2.props, sortProps, filterProps, searchProps, paginationProps, columnToggleProps, {
data: rootProps.getData(filterProps, searchProps, sortProps, paginationProps)
}));
};
}
}, {
key: 'renderWithColumnManagementCtx',
value: function renderWithColumnManagementCtx(base, baseProps) {
var _this3 = this;
return function (rootProps, filterProps, searchProps, sortProps, paginationProps) {
return _react2.default.createElement(
_this3.ColumnManagementContext.Provider,
_extends({}, baseProps, {
toggles: _this3.props.columnToggle ? _this3.props.columnToggle.toggles : null
}),
_react2.default.createElement(
_this3.ColumnManagementContext.Consumer,
null,
function (columnToggleProps) {
return base(rootProps, filterProps, searchProps, sortProps, paginationProps, columnToggleProps);
}
)
);
};
}
}, {
key: 'renderWithSelectionCtx',
value: function renderWithSelectionCtx(base, baseProps) {
var _this4 = this;
return function (rootProps, filterProps, searchProps, sortProps, paginationProps) {
return _react2.default.createElement(
_this4.SelectionContext.Provider,
_extends({}, baseProps, {
ref: function ref(n) {
return _this4.selectionContext = n;
},
selectRow: _this4.props.selectRow,
data: rootProps.getData(filterProps, searchProps, sortProps, paginationProps)
}),
base(rootProps, filterProps, searchProps, sortProps, paginationProps)
);
};
}
}, {
key: 'renderWithRowExpandCtx',
value: function renderWithRowExpandCtx(base, baseProps) {
var _this5 = this;
return function (rootProps, filterProps, searchProps, sortProps, paginationProps) {
return _react2.default.createElement(
_this5.RowExpandContext.Provider,
_extends({}, baseProps, {
ref: function ref(n) {
return _this5.rowExpandContext = n;
},
expandRow: _this5.props.expandRow,
data: rootProps.getData(filterProps, searchProps, sortProps, paginationProps)
}),
base(rootProps, filterProps, searchProps, sortProps, paginationProps)
);
};
}
}, {
key: 'renderWithPaginationCtx',
value: function renderWithPaginationCtx(base) {
var _this6 = this;
return function (rootProps, filterProps, searchProps, sortProps) {
return _react2.default.createElement(
_this6.PaginationContext.Provider,
{
ref: function ref(n) {
return _this6.paginationContext = n;
},
pagination: _this6.props.pagination,
data: rootProps.getData(filterProps, searchProps, sortProps),
bootstrap4: _this6.props.bootstrap4,
isRemotePagination: _this6.isRemotePagination,
remoteEmitter: _this6.remoteEmitter,
onDataSizeChange: _this6.props.onDataSizeChange,
tableId: _this6.props.id
},
_react2.default.createElement(
_this6.PaginationContext.Consumer,
null,
function (paginationProps) {
return base(rootProps, filterProps, searchProps, sortProps, paginationProps);
}
)
);
};
}
}, {
key: 'renderWithSortCtx',
value: function renderWithSortCtx(base, baseProps) {
var _this7 = this;
return function (rootProps, filterProps, searchProps) {
return _react2.default.createElement(
_this7.SortContext.Provider,
_extends({}, baseProps, {
ref: function ref(n) {
return _this7.sortContext = n;
},
defaultSorted: _this7.props.defaultSorted,
defaultSortDirection: _this7.props.defaultSortDirection,
sort: _this7.props.sort,
data: rootProps.getData(filterProps, searchProps)
}),
_react2.default.createElement(
_this7.SortContext.Consumer,
null,
function (sortProps) {
return base(rootProps, filterProps, searchProps, sortProps);
}
)
);
};
}
}, {
key: 'renderWithSearchCtx',
value: function renderWithSearchCtx(base, baseProps) {
var _this8 = this;
return function (rootProps, filterProps) {
return _react2.default.createElement(
_this8.SearchContext.Provider,
_extends({}, baseProps, {
ref: function ref(n) {
return _this8.searchContext = n;
},
data: rootProps.getData(filterProps),
searchText: _this8.props.search.searchText,
dataChangeListener: _this8.props.dataChangeListener
}),
_react2.default.createElement(
_this8.SearchContext.Consumer,
null,
function (searchProps) {
return base(rootProps, filterProps, searchProps);
}
)
);
};
}
}, {
key: 'renderWithFilterCtx',
value: function renderWithFilterCtx(base, baseProps) {
var _this9 = this;
return function (rootProps) {
return _react2.default.createElement(
_this9.FilterContext.Provider,
_extends({}, baseProps, {
ref: function ref(n) {
return _this9.filterContext = n;
},
data: rootProps.getData(),
filter: _this9.props.filter.options || {},
dataChangeListener: _this9.props.dataChangeListener
}),
_react2.default.createElement(
_this9.FilterContext.Consumer,
null,
function (filterProps) {
return base(rootProps, filterProps);
}
)
);
};
}
}, {
key: 'renderWithCellEditCtx',
value: function renderWithCellEditCtx(base, baseProps) {
var _this10 = this;
return function (rootProps) {
return _react2.default.createElement(
_this10.CellEditContext.Provider,
_extends({}, baseProps, {
ref: function ref(n) {
return _this10.cellEditContext = n;
},
selectRow: _this10.props.selectRow,
cellEdit: _this10.props.cellEdit,
data: rootProps.getData()
}),
base(rootProps)
);
};
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
keyField = _props.keyField,
columns = _props.columns,
bootstrap4 = _props.bootstrap4;
var baseProps = { keyField: keyField, columns: columns };
var base = this.renderBase();
if (this.ColumnManagementContext) {
base = this.renderWithColumnManagementCtx(base, baseProps);
}
if (this.SelectionContext) {
base = this.renderWithSelectionCtx(base, baseProps);
}
if (this.RowExpandContext) {
base = this.renderWithRowExpandCtx(base, baseProps);
}
if (this.PaginationContext) {
base = this.renderWithPaginationCtx(base, baseProps);
}
if (this.SortContext) {
base = this.renderWithSortCtx(base, baseProps);
}
if (this.SearchContext) {
base = this.renderWithSearchCtx(base, baseProps);
}
if (this.FilterContext) {
base = this.renderWithFilterCtx(base, baseProps);
}
if (this.CellEditContext) {
base = this.renderWithCellEditCtx(base, baseProps);
}
return _react2.default.createElement(
_bootstrap.BootstrapContext.Provider,
{ value: { bootstrap4: bootstrap4 } },
_react2.default.createElement(
this.DataContext.Provider,
_extends({}, baseProps, {
data: this.props.data
}),
_react2.default.createElement(
this.DataContext.Consumer,
null,
base
)
)
);
}
}]);
return BootstrapTableContainer;
}((0, _remoteResolver3.default)(_react.Component));
};
exports.default = withContext;