use-binding
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -0,2 +1,5 @@ | ||
import { Dispatch } from 'react'; | ||
declare type SetBindingStateAction<S, I> = S | ((prevState: I) => S); | ||
declare type ChangeHandler<T> = (newValue: T) => void; | ||
declare type Setter<T, I = T> = Dispatch<SetBindingStateAction<T, I>>; | ||
/** | ||
@@ -12,3 +15,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, fallbackValue: T): [T, ChangeHandler<T>]; | ||
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null, fallbackValue: T): [T, Setter<T>]; | ||
/** | ||
@@ -21,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, ChangeHandler<T>]; | ||
export declare function useBinding<T>(defaultValue: T, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null): [T, Setter<T>]; | ||
/** | ||
@@ -30,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, ChangeHandler<T>]; | ||
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T, onChange: ChangeHandler<T> | undefined | null): [T, Setter<T>]; | ||
/** | ||
@@ -39,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, ChangeHandler<T>]; | ||
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 {}; |
@@ -1,2 +0,2 @@ | ||
import { useState } from 'react'; | ||
import { useState, useRef, useCallback } from 'react'; | ||
@@ -18,12 +18,15 @@ function useBinding(defaultValue, value, onChange, fallbackValue) { | ||
var changeHandler = onChange || noop; | ||
if (hasControlledValue) { | ||
return [inputValue, changeHandler]; | ||
} | ||
return [ | ||
uncontrolledValue, | ||
function (newValue) { | ||
changeHandler(newValue); | ||
setUncontrolledValue(newValue); | ||
}, | ||
]; | ||
var currentValueRef = useRef(null); | ||
currentValueRef.current = hasControlledValue ? inputValue : uncontrolledValue; | ||
var setter = useCallback(function (newValue) { | ||
var evaluatedNewValue = typeof newValue === 'function' | ||
? newValue(currentValueRef.current) | ||
: newValue; | ||
currentValueRef.current = evaluatedNewValue; | ||
changeHandler(evaluatedNewValue); | ||
if (!hasControlledValue) { | ||
setUncontrolledValue(evaluatedNewValue); | ||
} | ||
}, [currentValueRef, changeHandler, hasControlledValue]); | ||
return [currentValueRef.current, setter]; | ||
} | ||
@@ -30,0 +33,0 @@ function noop(_) { |
@@ -22,12 +22,15 @@ 'use strict'; | ||
var changeHandler = onChange || noop; | ||
if (hasControlledValue) { | ||
return [inputValue, changeHandler]; | ||
} | ||
return [ | ||
uncontrolledValue, | ||
function (newValue) { | ||
changeHandler(newValue); | ||
setUncontrolledValue(newValue); | ||
}, | ||
]; | ||
var currentValueRef = react.useRef(null); | ||
currentValueRef.current = hasControlledValue ? inputValue : uncontrolledValue; | ||
var setter = react.useCallback(function (newValue) { | ||
var evaluatedNewValue = typeof newValue === 'function' | ||
? newValue(currentValueRef.current) | ||
: newValue; | ||
currentValueRef.current = evaluatedNewValue; | ||
changeHandler(evaluatedNewValue); | ||
if (!hasControlledValue) { | ||
setUncontrolledValue(evaluatedNewValue); | ||
} | ||
}, [currentValueRef, changeHandler, hasControlledValue]); | ||
return [currentValueRef.current, setter]; | ||
} | ||
@@ -34,0 +37,0 @@ function noop(_) { |
{ | ||
"name": "use-binding", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "peerDependencies": { |
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
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
19851
118