UNPKG

@react-awesome-query-builder-dev/ui

Version:
95 lines (94 loc) 3.78 kB
import React from "react"; import mapValues from "lodash/mapValues"; import { shallowEqual } from "./stuff"; var getReactContainerType = function getReactContainerType(el) { if (el._reactRootContainer) { return "root"; } if (Object.getOwnPropertyNames(el).filter(function (k) { return k.startsWith("__reactContainer"); }).length > 0) { return "container"; } return undefined; }; var _getReactRootNodeType = function getReactRootNodeType(node) { if (!node) { return undefined; } var type = getReactContainerType(node); if (type !== undefined) { return type; } else { return _getReactRootNodeType(node.parentNode); } }; export var isUsingLegacyReactDomRender = function isUsingLegacyReactDomRender(node) { return _getReactRootNodeType(node) === "root"; }; export var liteShouldComponentUpdate = function liteShouldComponentUpdate(self, config) { return function (nextProps, nextState) { var prevProps = self.props; var prevState = self.state; var should = nextProps != prevProps || nextState != prevState; if (should) { if (prevState == nextState && prevProps != nextProps) { var chs = []; for (var k in nextProps) { var changed = nextProps[k] != prevProps[k]; if (changed) { if (config[k] == "ignore") changed = false;else if (config[k] == "shallow_deep") changed = !shallowEqual(nextProps[k], prevProps[k], true);else if (config[k] == "shallow") changed = !shallowEqual(nextProps[k], prevProps[k]);else if (typeof config[k] == "function") changed = config[k](nextProps[k], prevProps[k], nextProps, prevProps); } if (changed) chs.push(k); } if (!chs.length) should = false; } } return should; }; }; export var pureShouldComponentUpdate = function pureShouldComponentUpdate(self) { return function (nextProps, nextState) { return !shallowEqual(self.props, nextProps) || !shallowEqual(self.state, nextState); }; }; var canUseOldComponentWillReceiveProps = function canUseOldComponentWillReceiveProps() { var v = React.version.split(".").map(parseInt.bind(null, 10)); return v[0] == 16 && v[1] < 3 || v[0] < 16; }; export var useOnPropsChanged = function useOnPropsChanged(obj) { // 1. `shouldComponentUpdate` should be called after `componentWillReceiveProps` // 2. `shouldComponentUpdate` should not be used for PureComponent // Because `useOnPropsChanged` can only be applied to `Component` not `PureComponent`, make it pure now if (!obj.shouldComponentUpdate) { obj.shouldComponentUpdate = pureShouldComponentUpdate(obj); } if (canUseOldComponentWillReceiveProps()) { // Use old method obj.componentWillReceiveProps = function (nextProps) { obj.onPropsChanged(nextProps); }; } else { // Simulate `componentWillReceiveProps` with `shouldComponentUpdate` var origShouldComponentUpdate = obj.shouldComponentUpdate; var newShouldComponentUpdate = function newShouldComponentUpdate(nextProps, nextState) { var shouldNotify = !shallowEqual(obj.props, nextProps); if (shouldNotify) { obj.onPropsChanged(nextProps); } var shouldUpdate = origShouldComponentUpdate.call(obj, nextProps, nextState); return shouldUpdate; }; obj.shouldComponentUpdate = newShouldComponentUpdate.bind(obj); } }; export var bindActionCreators = function bindActionCreators(actionCreators, config, dispatch) { return mapValues(actionCreators, function (actionCreator) { return function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return dispatch(actionCreator.apply(void 0, [config].concat(args))); }; }); };