-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
66 lines (57 loc) · 2.07 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {useState, useCallback} from 'react';
import useConstant from 'use-constant';
// Controlled
export default function useOptionalState<Value>(opts: {
controlledValue: Value;
initialValue?: Value | undefined;
onChange?(value: Value): void;
}): [Value, (value: Value) => void];
// Uncontrolled with initial value
export default function useOptionalState<Value>(opts: {
controlledValue?: Value | undefined;
initialValue: Value;
onChange?(value: Value): void;
}): [Value | undefined, (value: Value) => void];
// Uncontrolled without initial value
export default function useOptionalState<Value>(opts: {
controlledValue?: Value | undefined;
initialValue?: Value;
onChange?(value: Value): void;
}): [Value | undefined, (value: Value) => void];
/**
* Enables a component state to be either controlled or uncontrolled.
*/
export default function useOptionalState<Value>({
controlledValue,
initialValue,
onChange
}: {
controlledValue?: Value | undefined;
initialValue?: Value | undefined;
onChange?(value: Value): void;
}) {
const isControlled = controlledValue !== undefined;
const initialIsControlled = useConstant(() => isControlled);
const [stateValue, setStateValue] = useState(initialValue);
if (__DEV__) {
if (initialIsControlled && !isControlled) {
throw new Error(
'Can not change from controlled to uncontrolled mode. If `undefined` needs to be used for controlled values, please use `null` instead.'
);
}
if (!initialIsControlled && isControlled) {
throw new Error(
'Can not change from uncontrolled to controlled mode. Please supply an initial value other than `undefined` to make the state controlled over its lifetime. If `undefined` needs to be used for controlled values, please use `null` instead.'
);
}
}
const value = isControlled ? controlledValue : stateValue;
const onValueChange = useCallback(
(nextValue: Value) => {
if (!isControlled) setStateValue(nextValue);
if (onChange) onChange(nextValue);
},
[isControlled, onChange]
);
return [value, onValueChange];
}