uncontrollable
Advanced tools
Comparing version 7.0.2 to 7.1.0
@@ -8,2 +8,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.useUncontrolledProp = useUncontrolledProp; | ||
exports.default = useUncontrolled; | ||
@@ -23,2 +24,31 @@ | ||
function useUncontrolledProp(propValue, defaultValue, handler) { | ||
var wasPropRef = (0, _react.useRef)(propValue !== undefined); | ||
var _useState = (0, _react.useState)(defaultValue), | ||
stateValue = _useState[0], | ||
setState = _useState[1]; | ||
var isProp = propValue !== undefined; | ||
var wasProp = wasPropRef.current; | ||
wasPropRef.current = isProp; | ||
/** | ||
* If a prop switches from controlled to Uncontrolled | ||
* reset its value to the defaultValue | ||
*/ | ||
if (!isProp && wasProp && stateValue !== defaultValue) { | ||
setState(defaultValue); | ||
} | ||
return [isProp ? propValue : stateValue, (0, _react.useCallback)(function (value) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (handler) handler.apply(void 0, [value].concat(args)); | ||
setState(value); | ||
}, [handler])]; | ||
} | ||
function useUncontrolled(props, config) { | ||
@@ -28,37 +58,15 @@ return Object.keys(config).reduce(function (result, fieldName) { | ||
var defaultValue = result[Utils.defaultKey(fieldName)], | ||
propsValue = result[fieldName], | ||
rest = (0, _objectWithoutPropertiesLoose2.default)(result, [Utils.defaultKey(fieldName), fieldName].map(_toPropertyKey)); | ||
var _ref = result, | ||
defaultValue = _ref[Utils.defaultKey(fieldName)], | ||
propsValue = _ref[fieldName], | ||
rest = (0, _objectWithoutPropertiesLoose2.default)(_ref, [Utils.defaultKey(fieldName), fieldName].map(_toPropertyKey)); | ||
var handlerName = config[fieldName]; | ||
var prevProps = (0, _react.useRef)({}); | ||
var _useState = (0, _react.useState)(defaultValue), | ||
stateValue = _useState[0], | ||
setState = _useState[1]; | ||
var _useUncontrolledProp = useUncontrolledProp(propsValue, defaultValue, props[handlerName]), | ||
value = _useUncontrolledProp[0], | ||
handler = _useUncontrolledProp[1]; | ||
var isProp = Utils.isProp(props, fieldName); | ||
var wasProp = Utils.isProp(prevProps.current, fieldName); | ||
prevProps.current = props; | ||
/** | ||
* If a prop switches from controlled to Uncontrolled | ||
* reset its value to the defaultValue | ||
*/ | ||
if (!isProp && wasProp) { | ||
setState(defaultValue); | ||
} | ||
var propsHandler = props[handlerName]; | ||
var handler = (0, _react.useCallback)(function (value) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (propsHandler) propsHandler.apply(void 0, [value].concat(args)); | ||
setState(value); | ||
}, [setState, propsHandler]); | ||
return (0, _extends3.default)({}, rest, (_extends2 = {}, _extends2[fieldName] = isProp ? propsValue : stateValue, _extends2[handlerName] = handler, _extends2)); | ||
return (0, _extends3.default)({}, rest, (_extends2 = {}, _extends2[fieldName] = value, _extends2[handlerName] = handler, _extends2)); | ||
}, props); | ||
} | ||
module.exports = exports["default"]; | ||
} |
@@ -5,11 +5,14 @@ "use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
exports.__esModule = true; | ||
exports.uncontrollable = exports.useUncontrolled = void 0; | ||
exports.useUncontrolledProp = exports.uncontrollable = exports.useUncontrolled = void 0; | ||
var _hook = _interopRequireDefault(require("./hook.js")); | ||
var _hook = _interopRequireWildcard(require("./hook")); | ||
exports.useUncontrolled = _hook.default; | ||
exports.useUncontrolledProp = _hook.useUncontrolledProp; | ||
var _uncontrollable = _interopRequireDefault(require("./uncontrollable.js")); | ||
var _uncontrollable = _interopRequireDefault(require("./uncontrollable")); | ||
exports.uncontrollable = _uncontrollable.default; |
@@ -8,4 +8,32 @@ import _extends from "@babel/runtime/helpers/esm/extends"; | ||
import { useCallback, useState, useRef } from 'react'; | ||
import { useCallback, useRef, useState } from 'react'; | ||
import * as Utils from './utils'; | ||
export function useUncontrolledProp(propValue, defaultValue, handler) { | ||
var wasPropRef = useRef(propValue !== undefined); | ||
var _useState = useState(defaultValue), | ||
stateValue = _useState[0], | ||
setState = _useState[1]; | ||
var isProp = propValue !== undefined; | ||
var wasProp = wasPropRef.current; | ||
wasPropRef.current = isProp; | ||
/** | ||
* If a prop switches from controlled to Uncontrolled | ||
* reset its value to the defaultValue | ||
*/ | ||
if (!isProp && wasProp && stateValue !== defaultValue) { | ||
setState(defaultValue); | ||
} | ||
return [isProp ? propValue : stateValue, useCallback(function (value) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (handler) handler.apply(void 0, [value].concat(args)); | ||
setState(value); | ||
}, [handler])]; | ||
} | ||
export default function useUncontrolled(props, config) { | ||
@@ -15,36 +43,15 @@ return Object.keys(config).reduce(function (result, fieldName) { | ||
var defaultValue = result[Utils.defaultKey(fieldName)], | ||
propsValue = result[fieldName], | ||
rest = _objectWithoutPropertiesLoose(result, [Utils.defaultKey(fieldName), fieldName].map(_toPropertyKey)); | ||
var _ref = result, | ||
defaultValue = _ref[Utils.defaultKey(fieldName)], | ||
propsValue = _ref[fieldName], | ||
rest = _objectWithoutPropertiesLoose(_ref, [Utils.defaultKey(fieldName), fieldName].map(_toPropertyKey)); | ||
var handlerName = config[fieldName]; | ||
var prevProps = useRef({}); | ||
var _useState = useState(defaultValue), | ||
stateValue = _useState[0], | ||
setState = _useState[1]; | ||
var _useUncontrolledProp = useUncontrolledProp(propsValue, defaultValue, props[handlerName]), | ||
value = _useUncontrolledProp[0], | ||
handler = _useUncontrolledProp[1]; | ||
var isProp = Utils.isProp(props, fieldName); | ||
var wasProp = Utils.isProp(prevProps.current, fieldName); | ||
prevProps.current = props; | ||
/** | ||
* If a prop switches from controlled to Uncontrolled | ||
* reset its value to the defaultValue | ||
*/ | ||
if (!isProp && wasProp) { | ||
setState(defaultValue); | ||
} | ||
var propsHandler = props[handlerName]; | ||
var handler = useCallback(function (value) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (propsHandler) propsHandler.apply(void 0, [value].concat(args)); | ||
setState(value); | ||
}, [setState, propsHandler]); | ||
return _extends({}, rest, (_extends2 = {}, _extends2[fieldName] = isProp ? propsValue : stateValue, _extends2[handlerName] = handler, _extends2)); | ||
return _extends({}, rest, (_extends2 = {}, _extends2[fieldName] = value, _extends2[handlerName] = handler, _extends2)); | ||
}, props); | ||
} |
@@ -1,2 +0,2 @@ | ||
export { default as useUncontrolled } from './hook.js'; | ||
export { default as uncontrollable } from './uncontrollable.js'; | ||
export { default as useUncontrolled, useUncontrolledProp } from './hook'; | ||
export { default as uncontrollable } from './uncontrollable'; |
{ | ||
"name": "uncontrollable", | ||
"version": "7.0.2", | ||
"version": "7.1.0", | ||
"description": "Wrap a controlled react component, to allow specific prop/handler pairs to be uncontrolled", | ||
@@ -9,3 +9,6 @@ "author": { | ||
}, | ||
"repository": "jquense/uncontrollable", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/jquense/uncontrollable.git" | ||
}, | ||
"license": "MIT", | ||
@@ -22,2 +25,5 @@ "main": "cjs/index.js", | ||
], | ||
"publishConfig": { | ||
"directory": "lib" | ||
}, | ||
"peerDependencies": { | ||
@@ -30,12 +36,13 @@ "react": ">=15.0.0" | ||
"dependencies": { | ||
"@babel/runtime": "^7.4.5", | ||
"@babel/runtime": "^7.6.3", | ||
"@types/react": "^16.9.11", | ||
"invariant": "^2.2.4", | ||
"react-lifecycles-compat": "^3.0.4" | ||
}, | ||
"release": { | ||
"pkgRoot": "lib", | ||
"extends": [ | ||
"@4c/semantic-release-config" | ||
] | ||
} | ||
"bugs": { | ||
"url": "https://github.com/jquense/uncontrollable/issues" | ||
}, | ||
"readme": "ERROR: No README data found!", | ||
"homepage": "https://github.com/jquense/uncontrollable#readme", | ||
"_id": "uncontrollable@7.0.2" | ||
} |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
35714
17
527
0
0
6
5
+ Added@types/react@^16.9.11
+ Added@types/prop-types@15.7.13(transitive)
+ Added@types/react@16.14.62(transitive)
+ Added@types/scheduler@0.16.8(transitive)
+ Addedcsstype@3.1.3(transitive)
Updated@babel/runtime@^7.6.3