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

uncontrollable

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uncontrollable - npm Package Compare versions

Comparing version 7.0.2 to 7.1.0

cjs/hook.d.ts

72

cjs/hook.js

@@ -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"
}
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