use-binding
Advanced tools
Comparing version 1.1.2 to 2.0.0-develop.1
@@ -1,2 +0,2 @@ | ||
import { Dispatch } from 'react'; | ||
import { Dispatch } from "react"; | ||
declare type SetBindingStateAction<S, I> = S | ((prevState: I) => S); | ||
@@ -15,3 +15,3 @@ declare type ChangeHandler<T> = (newValue: T) => void; | ||
*/ | ||
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null, fallbackValue: T): [T, Setter<T>]; | ||
export declare function useBinding<T>(defaultValue: T | undefined, value: T | undefined, onChange: ChangeHandler<T> | undefined, fallbackValue: T): [T, Setter<T>]; | ||
/** | ||
@@ -24,3 +24,3 @@ * Return a [value, setValue] pair for a binding. | ||
*/ | ||
export declare function useBinding<T>(defaultValue: T, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null): [T, Setter<T>]; | ||
export declare function useBinding<T>(defaultValue: T, value: T | undefined, onChange: ChangeHandler<T> | undefined): [T, Setter<T>]; | ||
/** | ||
@@ -33,3 +33,3 @@ * Return a [value, setValue] pair for a binding. | ||
*/ | ||
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T, onChange: ChangeHandler<T> | undefined | null): [T, Setter<T>]; | ||
export declare function useBinding<T>(defaultValue: T | undefined, value: T, onChange: ChangeHandler<T> | undefined): [T, Setter<T>]; | ||
/** | ||
@@ -42,3 +42,3 @@ * Return a [value, setValue] pair for a binding. | ||
*/ | ||
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null): [T | null, Setter<T, T | null>]; | ||
export declare function useBinding<T>(defaultValue: T | undefined, value: T | undefined, onChange: ChangeHandler<T> | undefined): [T | undefined, Setter<T, T | undefined>]; | ||
export {}; |
@@ -1,22 +0,27 @@ | ||
import { useState, useRef, useCallback } from 'react'; | ||
import { useRef, useState, useCallback } from 'react'; | ||
function useBinding(defaultValue, value, onChange, fallbackValue) { | ||
var hasControlledValue = true; | ||
var inputValue = null; | ||
if (isNotEmpty(value)) { | ||
var hasControlledValue = false; | ||
var inputValue; | ||
if (isDefined(value)) { | ||
hasControlledValue = true; | ||
inputValue = value; | ||
} | ||
else if (isNotEmpty(defaultValue)) { | ||
hasControlledValue = false; | ||
else if (isDefined(defaultValue)) { | ||
inputValue = defaultValue; | ||
} | ||
else if (isNotEmpty(fallbackValue)) { | ||
else if (isDefined(fallbackValue)) { | ||
inputValue = fallbackValue; | ||
} | ||
var controlledValueRef = useRef(hasControlledValue); | ||
if (controlledValueRef.current !== hasControlledValue) { | ||
console.warn("WARN: A component changed from " + (controlledValueRef.current ? "controlled" : "uncontrolled") + " to " + (hasControlledValue ? "controlled" : "uncontrolled") + "."); | ||
controlledValueRef.current = hasControlledValue; | ||
} | ||
var _a = useState(inputValue), uncontrolledValue = _a[0], setUncontrolledValue = _a[1]; | ||
var changeHandler = onChange || noop; | ||
var currentValueRef = useRef(null); | ||
var currentValueRef = useRef(); | ||
currentValueRef.current = hasControlledValue ? inputValue : uncontrolledValue; | ||
var setter = useCallback(function (newValue) { | ||
var evaluatedNewValue = typeof newValue === 'function' | ||
var evaluatedNewValue = typeof newValue === "function" | ||
? newValue(currentValueRef.current) | ||
@@ -35,4 +40,4 @@ : newValue; | ||
} | ||
function isNotEmpty(value) { | ||
return typeof value !== 'undefined' && value !== null; | ||
function isDefined(value) { | ||
return typeof value !== "undefined"; | ||
} | ||
@@ -39,0 +44,0 @@ |
@@ -8,20 +8,25 @@ 'use strict'; | ||
function useBinding(defaultValue, value, onChange, fallbackValue) { | ||
var hasControlledValue = true; | ||
var inputValue = null; | ||
if (isNotEmpty(value)) { | ||
var hasControlledValue = false; | ||
var inputValue; | ||
if (isDefined(value)) { | ||
hasControlledValue = true; | ||
inputValue = value; | ||
} | ||
else if (isNotEmpty(defaultValue)) { | ||
hasControlledValue = false; | ||
else if (isDefined(defaultValue)) { | ||
inputValue = defaultValue; | ||
} | ||
else if (isNotEmpty(fallbackValue)) { | ||
else if (isDefined(fallbackValue)) { | ||
inputValue = fallbackValue; | ||
} | ||
var controlledValueRef = react.useRef(hasControlledValue); | ||
if (controlledValueRef.current !== hasControlledValue) { | ||
console.warn("WARN: A component changed from " + (controlledValueRef.current ? "controlled" : "uncontrolled") + " to " + (hasControlledValue ? "controlled" : "uncontrolled") + "."); | ||
controlledValueRef.current = hasControlledValue; | ||
} | ||
var _a = react.useState(inputValue), uncontrolledValue = _a[0], setUncontrolledValue = _a[1]; | ||
var changeHandler = onChange || noop; | ||
var currentValueRef = react.useRef(null); | ||
var currentValueRef = react.useRef(); | ||
currentValueRef.current = hasControlledValue ? inputValue : uncontrolledValue; | ||
var setter = react.useCallback(function (newValue) { | ||
var evaluatedNewValue = typeof newValue === 'function' | ||
var evaluatedNewValue = typeof newValue === "function" | ||
? newValue(currentValueRef.current) | ||
@@ -40,4 +45,4 @@ : newValue; | ||
} | ||
function isNotEmpty(value) { | ||
return typeof value !== 'undefined' && value !== null; | ||
function isDefined(value) { | ||
return typeof value !== "undefined"; | ||
} | ||
@@ -44,0 +49,0 @@ |
{ | ||
"name": "use-binding", | ||
"version": "1.1.2", | ||
"version": "2.0.0-develop.1", | ||
"license": "MIT", | ||
@@ -89,3 +89,12 @@ "peerDependencies": { | ||
"url": "https://github.com/elseu/use-binding/issues" | ||
}, | ||
"release": { | ||
"branches": [ | ||
"master", | ||
{ | ||
"name": "develop", | ||
"prerelease": true | ||
} | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
21616
128
1