@lessondesk/react-table-context
Advanced tools
Comparing version 1.2.4 to 2.0.0
@@ -1,9 +0,12 @@ | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var React = require('react'); | ||
var React__default = _interopDefault(React); | ||
var hash = _interopDefault(require('hash-sum')); | ||
var hash = require('hash-sum'); | ||
var fastEquals = require('fast-equals'); | ||
var debounce = _interopDefault(require('lodash.debounce')); | ||
var debounce = require('lodash.debounce'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var React__default = /*#__PURE__*/_interopDefaultLegacy(React); | ||
var hash__default = /*#__PURE__*/_interopDefaultLegacy(hash); | ||
var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce); | ||
function _extends() { | ||
@@ -30,5 +33,15 @@ _extends = Object.assign || function (target) { | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
_setPrototypeOf(subClass, superClass); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function _catch(body, recover) { | ||
@@ -49,2 +62,15 @@ try { | ||
var DEFAULT_SEARCH_WAIT = 500; | ||
var compareRows = function compareRows(array1, array2, rowFormatter) { | ||
if (array1.length !== array2.length) return false; | ||
return array1.every(function (row) { | ||
var _rowFormatter = rowFormatter(row), | ||
key = _rowFormatter.key; | ||
return array2.some(function (row2) { | ||
return key === rowFormatter(row2).key; | ||
}); | ||
}); | ||
}; | ||
function initTableContext(requestData) { | ||
@@ -67,16 +93,2 @@ if (requestData === void 0) { | ||
_this.checkIfSame = function (selected, prevSelected) { | ||
return selected.every(function (_ref) { | ||
var id = _ref.id; | ||
return prevSelected.some(function (s) { | ||
return s.id === id; | ||
}); | ||
}) && prevSelected.every(function (_ref2) { | ||
var id = _ref2.id; | ||
return selected.some(function (s) { | ||
return s.id === id; | ||
}); | ||
}); | ||
}; | ||
_this.handleUpdate = function (newValues) { | ||
@@ -115,3 +127,3 @@ var _this$state$newValues = _extends({}, _this.state, newValues), | ||
_this.getData = debounce(function (values, key, now) { | ||
_this.getData = debounce__default['default'](function (values, key, now) { | ||
try { | ||
@@ -216,3 +228,2 @@ return Promise.resolve(_catch(function () { | ||
_this.setSelected = function (selected) { | ||
// NOTE: We do not want to refresh table if an item becomes selected. | ||
_this.setState({ | ||
@@ -223,2 +234,8 @@ selected: selected | ||
_this.setExpanded = function (expanded) { | ||
_this.setState({ | ||
expanded: expanded | ||
}); | ||
}; | ||
_this.toggleSelectAll = function (type) { | ||
@@ -228,4 +245,4 @@ var _this$state2 = _this.state, | ||
currentSelection = _this$state2.selected; | ||
var mustRemoveAll = data.every(function (_ref3) { | ||
var id = _ref3.id; | ||
var mustRemoveAll = data.every(function (_ref) { | ||
var id = _ref.id; | ||
return currentSelection.some(function (s) { | ||
@@ -235,4 +252,4 @@ return s.id === id; | ||
}); | ||
var checkedRows = mustRemoveAll ? currentSelection.filter(function (_ref4) { | ||
var id = _ref4.id; | ||
var checkedRows = mustRemoveAll ? currentSelection.filter(function (_ref2) { | ||
var id = _ref2.id; | ||
return !data.some(function (d) { | ||
@@ -242,4 +259,4 @@ return d.id === id; | ||
}) : [].concat(currentSelection, data); | ||
var filteredRows = checkedRows.filter(function (_ref5, index) { | ||
var id = _ref5.id; | ||
var filteredRows = checkedRows.filter(function (_ref3, index) { | ||
var id = _ref3.id; | ||
return checkedRows.findIndex(function (r) { | ||
@@ -306,5 +323,6 @@ return r.id === id; | ||
selected: [], | ||
expanded: [], | ||
search: '' | ||
}; | ||
_this.key = hash(Date.now()); | ||
_this.key = hash__default['default'](Date.now()); | ||
_this.cache = new Map(); | ||
@@ -320,18 +338,23 @@ return _this; | ||
filters = _this$props.filters, | ||
selected = _this$props.selected; | ||
selected = _this$props.selected, | ||
expanded = _this$props.expanded; | ||
this.handleUpdate({ | ||
pageSize: pageSize, | ||
filters: filters, | ||
selected: selected | ||
selected: selected, | ||
expanded: expanded | ||
}); | ||
}; | ||
_proto.componentDidUpdate = function componentDidUpdate(prevProps) { | ||
_proto.componentDidUpdate = function componentDidUpdate(previous) { | ||
var _this$props2 = this.props, | ||
selected = _this$props2.selected, | ||
expanded = _this$props2.expanded, | ||
filters = _this$props2.filters, | ||
autoApplyFilters = _this$props2.autoApplyFilters; | ||
var isSame = this.checkIfSame(selected, prevProps.selected); | ||
autoApplyFilters = _this$props2.autoApplyFilters, | ||
rowFormatter = _this$props2.rowFormatter; | ||
var isSame = compareRows(selected, previous == null ? void 0 : previous.selected, rowFormatter) && compareRows(expanded, previous == null ? void 0 : previous.expanded, rowFormatter); | ||
if (!isSame) this.setState({ | ||
selected: selected | ||
selected: selected, | ||
expanded: expanded | ||
}); | ||
@@ -350,2 +373,3 @@ var shouldApplyFilters = autoApplyFilters && !fastEquals.deepEqual(filters, prevProps.filters); | ||
setSelected: this.setSelected, | ||
setExpanded: this.setExpanded, | ||
toggleSelectAll: this.toggleSelectAll, | ||
@@ -358,3 +382,3 @@ setFilters: this.setFilters, | ||
return /*#__PURE__*/React__default.createElement(TableContext.Provider, { | ||
return /*#__PURE__*/React__default['default'].createElement(TableContext.Provider, { | ||
value: value | ||
@@ -372,4 +396,5 @@ }, this.props.children); | ||
selected: [], | ||
expanded: [], | ||
getCacheKey: function getCacheKey(state) { | ||
return hash(state); | ||
return hash__default['default'](state); | ||
} | ||
@@ -381,1 +406,2 @@ }; | ||
module.exports = initTableContext; | ||
//# sourceMappingURL=react-table-context.js.map |
{ | ||
"name": "@lessondesk/react-table-context", | ||
"description": "Flexible Table Provider & Consumer", | ||
"version": "1.2.4", | ||
"version": "2.0.0", | ||
"main": "dist/react-table-context.js", | ||
@@ -31,3 +31,3 @@ "source": "src/index.js", | ||
"babel-eslint": "^10.0.3", | ||
"microbundle": "^0.12.2", | ||
"microbundle": "^0.13.0", | ||
"standard": "^14.3.1" | ||
@@ -34,0 +34,0 @@ }, |
@@ -8,2 +8,10 @@ import React, { createContext, Component } from 'react' | ||
const compareRows = (array1, array2, rowFormatter) => { | ||
if (array1.length !== array2.length) return false | ||
return array1.every(row => { | ||
const { key } = rowFormatter(row) | ||
return array2.some(row2 => key === rowFormatter(row2).key) | ||
}) | ||
} | ||
export default function initTableContext(requestData = () => Promise.resolve([])) { | ||
@@ -29,2 +37,3 @@ const TableContext = createContext() | ||
selected: [], | ||
expanded: [], | ||
search: '' | ||
@@ -42,2 +51,3 @@ } | ||
selected: [], | ||
expanded: [], | ||
getCacheKey: state => hash(state), | ||
@@ -47,11 +57,13 @@ }; | ||
componentDidMount() { | ||
const { pageSize, filters, selected } = this.props | ||
this.handleUpdate({ pageSize, filters, selected }) | ||
const { pageSize, filters, selected, expanded } = this.props | ||
this.handleUpdate({ pageSize, filters, selected, expanded }) | ||
} | ||
componentDidUpdate(prevProps) { | ||
const { selected, filters, autoApplyFilters } = this.props | ||
const isSame = this.checkIfSame(selected, prevProps.selected) | ||
if (!isSame) this.setState({ selected }) | ||
componentDidUpdate(previous) { | ||
const { selected, expanded, filters, autoApplyFilters, rowFormatter } = this.props | ||
const isSame = compareRows(selected, previous?.selected, rowFormatter) && | ||
compareRows(expanded, previous?.expanded, rowFormatter) | ||
if (!isSame) this.setState({ selected, expanded }) | ||
const shouldApplyFilters = autoApplyFilters && !deepEqual(filters, prevProps.filters) | ||
@@ -61,7 +73,2 @@ if (shouldApplyFilters) this.setFilters(filters) | ||
checkIfSame = (selected, prevSelected) => ( | ||
selected.every(({ id }) => prevSelected.some(s => s.id === id)) | ||
&& prevSelected.every(({ id }) => selected.some(s => s.id === id)) | ||
) | ||
handleUpdate = (newValues) => { | ||
@@ -173,7 +180,11 @@ const { page, pageSize, search, filters, sorting } = { ...this.state, ...newValues } | ||
// NOTE: We do not want to refresh table if an item becomes selected or expanded. | ||
setSelected = selected => { | ||
// NOTE: We do not want to refresh table if an item becomes selected. | ||
this.setState({ selected }) | ||
}; | ||
setExpanded = expanded => { | ||
this.setState({ expanded }) | ||
} | ||
toggleSelectAll = type => { | ||
@@ -236,2 +247,3 @@ const { data, selected: currentSelection } = this.state | ||
setSelected: this.setSelected, | ||
setExpanded: this.setExpanded, | ||
toggleSelectAll: this.toggleSelectAll, | ||
@@ -238,0 +250,0 @@ setFilters: this.setFilters, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43985
7
633