Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lessondesk/react-table-context

Package Overview
Dependencies
Maintainers
6
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lessondesk/react-table-context - npm Package Compare versions

Comparing version 1.0.3 to 1.1.0

2

dist/react-table-context.js

@@ -1,1 +0,1 @@

function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=require("react"),a=e(t),i=e(require("hash-sum"));module.exports=function(e){void 0===e&&(e=function(){return Promise.resolve([])});var r=t.createContext(),n=function(t){function n(){var a=this;t.call(this),this.checkIfSame=function(e,t){return e.every(function(e){var a=e.id;return t.some(function(e){return e.id===a})})&&t.every(function(t){var a=t.id;return e.some(function(e){return e.id===a})})},this.handleUpdate=function(){var t=a.state,i=a.props.getCacheKey({meta:t.meta,page:t.page,pageSize:t.pageSize,search:t.search,filters:t.filters,sorting:t.sorting,key:a.key});if(a.cache.has(i)){var r=a.cache.get(i);a.setState(Object.assign({},r))}else a.setState({isLoading:!0},function(){e(Object.assign({},a.state)).then(function(e){var t=[],r={};if(e.data&&e.meta?(t=e.data,r=e.meta):r={count:(t=e).length},!Array.isArray(t))throw new Error("Invalid data provided. Expected array, but got "+typeof t);a.setState(function(e){var a=e.page*e.pageSize;return Object.assign({},e,{data:t,meta:r,firstPage:0===e.page,pageData:t.slice(a,a+e.pageSize),isEmpty:0===t.length,isLoading:!1})},function(){var e=a.state;a.cache.set(i,{data:a.state.data,firstPage:e.firstPage,isEmpty:e.isEmpty,isLoading:e.isLoading,pageData:e.pageData,unappliedFilters:e.unappliedFilters,meta:e.meta,page:e.page,pageSize:e.pageSize,search:e.search,filters:e.filters})})}).catch(function(e){console.error("Init table context error: ",e),a.props.onError(e),a.setState({error:e,isLoading:!1})})})},this.setSearch=function(e){a.setState({search:e,page:0},function(){return a.handleUpdate()})},this.setPage=function(e){a.setState({page:e},function(){return a.handleUpdate()})},this.setPageSize=function(e){a.setState({pageSize:e},function(){return a.handleUpdate()})},this.setSelected=function(e){a.setState({selected:e})},this.toggleSelectAll=function(e){var t=a.state,i=t.data,r=t.selected,n=i.every(function(e){var t=e.id;return r.some(function(e){return e.id===t})})?r.filter(function(e){var t=e.id;return!i.some(function(e){return e.id===t})}):r.concat(i),s=n.filter(function(e,t){var a=e.id;return n.findIndex(function(e){return e.id===a})===t}),o=e?s.map(function(t){return Object.assign({},t,{tableName:t.tableName||e})}):s;a.setState({selected:o})},this.setFilters=function(e,t){void 0===t&&(t=!1),a.setState({filters:e,unappliedFilters:e,page:0,data:t?[]:a.state.data},function(){return a.handleUpdate()})},this.setUnappliedFilters=function(e){a.setState({unappliedFilters:e})},this.applyFilters=function(){a.setFilters(a.state.unappliedFilters)},this.setSorting=function(e){a.setState({sorting:e},function(){return a.handleUpdate()})},this.refresh=function(){a.handleUpdate()},this.state={page:0,pageSize:10,firstPage:!0,isLoading:!1,isEmpty:!0,data:[],meta:{},error:null,filters:{},unappliedFilters:{},sorting:{},pageData:[],selected:[],search:""},this.key=i(Date.now()),this.cache=new Map}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n.prototype.componentDidMount=function(){var e=this,t=this.props;this.setState({pageSize:t.pageSize,filters:t.filters,selected:t.selected},function(){return e.handleUpdate()})},n.prototype.componentDidUpdate=function(e){var t=this.props.selected;this.checkIfSame(t,e.selected)||this.setState({selected:t})},n.prototype.render=function(){var e=Object.assign({},this.state,{setSearch:this.setSearch,setPage:this.setPage,setSorting:this.setSorting,setPageSize:this.setPageSize,refresh:this.refresh,setSelected:this.setSelected,toggleSelectAll:this.toggleSelectAll,setFilters:this.setFilters,setUnappliedFilters:this.setUnappliedFilters,applyFilters:this.applyFilters});return a.createElement(r.Provider,{value:e},this.props.children)},n}(t.Component);return n.defaultProps={onError:console.error,pageSize:10,filters:{},selected:[],getCacheKey:function(e){return i(e)}},[n,r.Consumer]};
function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=require("react"),a=e(t),i=e(require("hash-sum")),r=require("fast-equals");module.exports=function(e){void 0===e&&(e=function(){return Promise.resolve([])});var s=t.createContext(),n=function(t){function n(){var a=this;t.call(this),this.checkIfSame=function(e,t){return e.every(function(e){var a=e.id;return t.some(function(e){return e.id===a})})&&t.every(function(t){var a=t.id;return e.some(function(e){return e.id===a})})},this.handleUpdate=function(){var t=a.state,i=a.props.getCacheKey({meta:t.meta,page:t.page,pageSize:t.pageSize,search:t.search,filters:t.filters,sorting:t.sorting,key:a.key});if(a.cache.has(i)){var r=a.cache.get(i);a.setState(Object.assign({},r))}else a.setState({isLoading:!0},function(){e(Object.assign({},a.state)).then(function(e){var t=[],r={};if(e.data&&e.meta?(t=e.data,r=e.meta):r={count:(t=e).length},!Array.isArray(t))throw new Error("Invalid data provided. Expected array, but got "+typeof t);a.setState(function(e){var a=e.page*e.pageSize;return Object.assign({},e,{data:t,meta:r,firstPage:0===e.page,pageData:t.slice(a,a+e.pageSize),isEmpty:0===t.length,isLoading:!1})},function(){var e=a.state;a.cache.set(i,{data:a.state.data,firstPage:e.firstPage,isEmpty:e.isEmpty,isLoading:e.isLoading,pageData:e.pageData,unappliedFilters:e.unappliedFilters,meta:e.meta,page:e.page,pageSize:e.pageSize,search:e.search,filters:e.filters})})}).catch(function(e){console.error("Init table context error: ",e),a.props.onError(e),a.setState({error:e,isLoading:!1})})})},this.setSearch=function(e){a.setState({search:e,page:0},function(){return a.handleUpdate()})},this.setPage=function(e){a.setState({page:e},function(){return a.handleUpdate()})},this.setPageSize=function(e){a.setState({pageSize:e},function(){return a.handleUpdate()})},this.setSelected=function(e){a.setState({selected:e})},this.toggleSelectAll=function(e){var t=a.state,i=t.data,r=t.selected,s=i.every(function(e){var t=e.id;return r.some(function(e){return e.id===t})})?r.filter(function(e){var t=e.id;return!i.some(function(e){return e.id===t})}):r.concat(i),n=s.filter(function(e,t){var a=e.id;return s.findIndex(function(e){return e.id===a})===t}),o=e?n.map(function(t){return Object.assign({},t,{tableName:t.tableName||e})}):n;a.setState({selected:o})},this.setFilters=function(e,t){void 0===t&&(t=!1),a.setState({filters:e,unappliedFilters:e,page:0,data:t?[]:a.state.data},function(){return a.handleUpdate()})},this.setUnappliedFilters=function(e){a.setState({unappliedFilters:e})},this.applyFilters=function(){a.setFilters(a.state.unappliedFilters)},this.setSorting=function(e){a.setState({sorting:e},function(){return a.handleUpdate()})},this.refresh=function(){a.handleUpdate()},this.state={page:0,pageSize:10,firstPage:!0,isLoading:!1,isEmpty:!0,data:[],meta:{},error:null,filters:{},unappliedFilters:{},sorting:{},pageData:[],selected:[],search:""},this.key=i(Date.now()),this.cache=new Map}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n.prototype.componentDidMount=function(){var e=this,t=this.props;this.setState({pageSize:t.pageSize,filters:t.filters,selected:t.selected},function(){return e.handleUpdate()})},n.prototype.componentDidUpdate=function(e){var t=this.props,a=t.selected,i=t.filters,s=t.autoApplyFilters;this.checkIfSame(a,e.selected)||this.setState({selected:a}),s&&!r.deepEqual(i,e.filters)&&this.setFilters(i,!0)},n.prototype.render=function(){var e=Object.assign({},this.state,{setSearch:this.setSearch,setPage:this.setPage,setSorting:this.setSorting,setPageSize:this.setPageSize,refresh:this.refresh,setSelected:this.setSelected,toggleSelectAll:this.toggleSelectAll,setFilters:this.setFilters,setUnappliedFilters:this.setUnappliedFilters,applyFilters:this.applyFilters});return a.createElement(s.Provider,{value:e},this.props.children)},n}(t.Component);return n.defaultProps={onError:console.error,pageSize:10,filters:{},selected:[],getCacheKey:function(e){return i(e)}},[n,s.Consumer]};
{
"name": "@lessondesk/react-table-context",
"description": "Flexible Table Provider & Consumer",
"version": "1.0.3",
"version": "1.1.0",
"main": "dist/react-table-context.js",

@@ -35,2 +35,3 @@ "source": "src/index.js",

"dependencies": {
"fast-equals": "^2.0.0",
"hash-sum": "^2.0.0"

@@ -37,0 +38,0 @@ },

import React, { createContext, Component } from 'react'
import hash from 'hash-sum'
import { deepEqual } from 'fast-equals'

@@ -36,3 +37,3 @@ export default function initTableContext(getData = () => Promise.resolve([])) {

selected: [],
getCacheKey: state => hash(state)
getCacheKey: state => hash(state),
};

@@ -46,5 +47,8 @@

componentDidUpdate(prevProps) {
const { selected } = this.props
const { selected, filters, autoApplyFilters } = this.props
const isSame = this.checkIfSame(selected, prevProps.selected)
if (!isSame) this.setState({ selected })
const shouldApplyFilters = autoApplyFilters && !deepEqual(filters, prevProps.filters)
if (shouldApplyFilters) this.setFilters(filters, true)
}

@@ -242,2 +246,2 @@

return [TableProvider, TableContext.Consumer]
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc