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.1 to 1.0.2

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 s=t.createContext(),r=function(t){function r(){var a=this;t.call(this),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 s=a.cache.get(i);a.setState(Object.assign({},s))}else a.setState({isLoading:!0},function(){e(Object.assign({},a.state)).then(function(e){var t=[],s={};if(e.data&&e.meta?(t=e.data,s=e.meta):s={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:s,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(e),a.props.onError(e),a.setState(e)})})},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,s=t.selected,r=i.every(function(e){var t=e.id;return s.some(function(e){return e.id===t})})?s.filter(function(e){var t=e.id;return!i.some(function(e){return e.id===t})}):s.concat(i),n=r.filter(function(e,t){var a=e.id;return r.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&&(r.__proto__=t),(r.prototype=Object.create(t&&t.prototype)).constructor=r,r.prototype.componentDidMount=function(){var e=this,t=this.props;this.setState({pageSize:t.pageSize,filters:t.filters,selected:t.selected},function(){return e.handleUpdate()})},r.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)},r}(t.Component);return r.defaultProps={onError:console.error,pageSize:10,filters:{},selected:[],getCacheKey:function(e){return i(e)}},[r,s.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"));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(e),a.props.onError(e),a.setState(e)})})},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]};
{
"name": "@lessondesk/react-table-context",
"description": "Flexible Table Provider & Consumer",
"version": "1.0.1",
"version": "1.0.2",
"main": "dist/react-table-context.js",

@@ -6,0 +6,0 @@ "source": "src/index.js",

@@ -44,2 +44,13 @@ import React, { createContext, Component } from 'react'

componentDidUpdate (prevProps) {
const { selected } = this.props
const isSame = this.checkIfSame(selected, prevProps.selected)
if (!isSame) this.setState({ selected })
}
checkIfSame = (selected, prevSelected) => (
selected.every(({id}) => prevSelected.some(s => s.id === id))
&& prevSelected.every(({id}) => selected.some(s => s.id === id))
)
handleUpdate = () => {

@@ -46,0 +57,0 @@ const { meta, page, pageSize, search, filters, sorting } = this.state

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