Skip to content

Commit

Permalink
Allow null as a value
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Jun 29, 2021
1 parent f4db8fb commit c14e3b2
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/components/src/utils/hooks/use-controlled-value.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isNil } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -28,11 +23,12 @@ export function useControlledValue< T >( {
onChange,
value: valueProp,
}: Props< T > ): [ T | undefined, ( value: T ) => void ] {
const hasValue = ! isNil( valueProp );
const hasValue = typeof valueProp !== 'undefined';
const initialValue = hasValue ? valueProp : defaultValue;
const [ state, setState ] = useState( initialValue );
const value = hasValue ? valueProp : state;
const setValue = hasValue && ! isNil( onChange ) ? onChange : setState;
const setValue =
hasValue && typeof onChange === 'function' ? onChange : setState;

return [ value, setValue ];
}

0 comments on commit c14e3b2

Please sign in to comment.