@fluentui/react-utilities 9.0.0-alpha.9 → 9.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +1132 -1
- package/CHANGELOG.md +492 -2
- package/dist/react-utilities.d.ts +335 -67
- package/lib/compose/getSlots.d.ts +13 -4
- package/lib/compose/getSlots.js +44 -32
- package/lib/compose/getSlots.js.map +1 -1
- package/lib/compose/index.d.ts +1 -2
- package/lib/compose/index.js +2 -2
- package/lib/compose/index.js.map +1 -1
- package/lib/compose/nullRender.js +1 -1
- package/lib/compose/nullRender.js.map +1 -1
- package/lib/compose/resolveShorthand.d.ts +12 -0
- package/lib/compose/resolveShorthand.js +31 -0
- package/lib/compose/resolveShorthand.js.map +1 -0
- package/lib/compose/types.d.ts +95 -21
- package/lib/compose/types.js +5 -0
- package/lib/compose/types.js.map +1 -1
- package/lib/hooks/index.d.ts +9 -1
- package/lib/hooks/index.js +9 -1
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/useBoolean.d.ts +2 -2
- package/lib/hooks/useBoolean.js +16 -11
- package/lib/hooks/useBoolean.js.map +1 -1
- package/lib/hooks/useConst.js +15 -12
- package/lib/hooks/useConst.js.map +1 -1
- package/lib/hooks/useControllableState.d.ts +21 -0
- package/lib/hooks/useControllableState.js +68 -0
- package/lib/hooks/useControllableState.js.map +1 -0
- package/lib/hooks/useControllableValue.js +47 -50
- package/lib/hooks/useControllableValue.js.map +1 -1
- package/lib/hooks/useEventCallback.d.ts +2 -2
- package/lib/hooks/useEventCallback.js +14 -17
- package/lib/hooks/useEventCallback.js.map +1 -1
- package/lib/hooks/useFirstMount.d.ts +11 -0
- package/lib/hooks/useFirstMount.js +23 -0
- package/lib/hooks/useFirstMount.js.map +1 -0
- package/lib/hooks/useForceUpdate.d.ts +4 -0
- package/lib/hooks/useForceUpdate.js +9 -0
- package/lib/hooks/useForceUpdate.js.map +1 -0
- package/lib/hooks/useId.d.ts +7 -3
- package/lib/hooks/useId.js +13 -17
- package/lib/hooks/useId.js.map +1 -1
- package/lib/hooks/useIsomorphicLayoutEffect.d.ts +9 -0
- package/lib/hooks/useIsomorphicLayoutEffect.js +13 -4
- package/lib/hooks/useIsomorphicLayoutEffect.js.map +1 -1
- package/lib/hooks/useMergedRefs.js +16 -20
- package/lib/hooks/useMergedRefs.js.map +1 -1
- package/lib/hooks/useMount.d.ts +6 -0
- package/lib/hooks/useMount.js +17 -0
- package/lib/hooks/useMount.js.map +1 -0
- package/lib/hooks/useOnClickOutside.d.ts +14 -4
- package/lib/hooks/useOnClickOutside.js +129 -18
- package/lib/hooks/useOnClickOutside.js.map +1 -1
- package/lib/hooks/useOnScrollOutside.d.ts +5 -0
- package/lib/hooks/useOnScrollOutside.js +36 -0
- package/lib/hooks/useOnScrollOutside.js.map +1 -0
- package/lib/hooks/usePrevious.d.ts +1 -0
- package/lib/hooks/usePrevious.js +9 -0
- package/lib/hooks/usePrevious.js.map +1 -0
- package/lib/hooks/useTimeout.d.ts +8 -0
- package/lib/hooks/useTimeout.js +29 -0
- package/lib/hooks/useTimeout.js.map +1 -0
- package/lib/hooks/useUnmount.d.ts +6 -0
- package/lib/hooks/useUnmount.js +17 -0
- package/lib/hooks/useUnmount.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/ssr/SSRContext.d.ts +33 -0
- package/lib/ssr/SSRContext.js +65 -0
- package/lib/ssr/SSRContext.js.map +1 -0
- package/lib/ssr/canUseDOM.d.ts +4 -0
- package/lib/ssr/canUseDOM.js +8 -0
- package/lib/ssr/canUseDOM.js.map +1 -0
- package/lib/ssr/index.d.ts +2 -0
- package/lib/ssr/index.js +3 -0
- package/lib/ssr/index.js.map +1 -0
- package/lib/tsdoc-metadata.json +1 -1
- package/lib/utils/applyTriggerPropsToChildren.d.ts +5 -0
- package/lib/utils/applyTriggerPropsToChildren.js +16 -0
- package/lib/utils/applyTriggerPropsToChildren.js.map +1 -0
- package/lib/utils/clamp.d.ts +8 -0
- package/lib/utils/clamp.js +9 -0
- package/lib/utils/clamp.js.map +1 -0
- package/lib/utils/getNativeElementProps.d.ts +22 -0
- package/lib/utils/getNativeElementProps.js +48 -24
- package/lib/utils/getNativeElementProps.js.map +1 -1
- package/lib/utils/getRTLSafeKey.d.ts +4 -0
- package/lib/utils/getRTLSafeKey.js +21 -0
- package/lib/utils/getRTLSafeKey.js.map +1 -0
- package/lib/utils/index.d.ts +5 -0
- package/lib/utils/index.js +5 -0
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/omit.d.ts +1 -1
- package/lib/utils/omit.js +9 -7
- package/lib/utils/omit.js.map +1 -1
- package/lib/utils/onlyChild.d.ts +5 -0
- package/lib/utils/onlyChild.js +13 -0
- package/lib/utils/onlyChild.js.map +1 -0
- package/lib/utils/properties.js +80 -282
- package/lib/utils/properties.js.map +1 -1
- package/lib/utils/shouldPreventDefaultOnKeyDown.d.ts +7 -0
- package/lib/utils/shouldPreventDefaultOnKeyDown.js +20 -0
- package/lib/utils/shouldPreventDefaultOnKeyDown.js.map +1 -0
- package/lib-commonjs/compose/getSlots.d.ts +13 -4
- package/lib-commonjs/compose/getSlots.js +57 -35
- package/lib-commonjs/compose/getSlots.js.map +1 -1
- package/lib-commonjs/compose/index.d.ts +1 -2
- package/lib-commonjs/compose/index.js +12 -4
- package/lib-commonjs/compose/index.js.map +1 -1
- package/lib-commonjs/compose/nullRender.js +9 -2
- package/lib-commonjs/compose/nullRender.js.map +1 -1
- package/lib-commonjs/compose/resolveShorthand.d.ts +12 -0
- package/lib-commonjs/compose/resolveShorthand.js +41 -0
- package/lib-commonjs/compose/resolveShorthand.js.map +1 -0
- package/lib-commonjs/compose/types.d.ts +95 -21
- package/lib-commonjs/compose/types.js +8 -1
- package/lib-commonjs/compose/types.js.map +1 -1
- package/lib-commonjs/hooks/index.d.ts +9 -1
- package/lib-commonjs/hooks/index.js +31 -4
- package/lib-commonjs/hooks/index.js.map +1 -1
- package/lib-commonjs/hooks/useBoolean.d.ts +2 -2
- package/lib-commonjs/hooks/useBoolean.js +27 -14
- package/lib-commonjs/hooks/useBoolean.js.map +1 -1
- package/lib-commonjs/hooks/useConst.js +24 -14
- package/lib-commonjs/hooks/useConst.js.map +1 -1
- package/lib-commonjs/hooks/useControllableState.d.ts +21 -0
- package/lib-commonjs/hooks/useControllableState.js +78 -0
- package/lib-commonjs/hooks/useControllableState.js.map +1 -0
- package/lib-commonjs/hooks/useControllableValue.js +58 -53
- package/lib-commonjs/hooks/useControllableValue.js.map +1 -1
- package/lib-commonjs/hooks/useEventCallback.d.ts +2 -2
- package/lib-commonjs/hooks/useEventCallback.js +26 -20
- package/lib-commonjs/hooks/useEventCallback.js.map +1 -1
- package/lib-commonjs/hooks/useFirstMount.d.ts +11 -0
- package/lib-commonjs/hooks/useFirstMount.js +33 -0
- package/lib-commonjs/hooks/useFirstMount.js.map +1 -0
- package/lib-commonjs/hooks/useForceUpdate.d.ts +4 -0
- package/lib-commonjs/hooks/useForceUpdate.js +19 -0
- package/lib-commonjs/hooks/useForceUpdate.js.map +1 -0
- package/lib-commonjs/hooks/useId.d.ts +7 -3
- package/lib-commonjs/hooks/useId.js +25 -21
- package/lib-commonjs/hooks/useId.js.map +1 -1
- package/lib-commonjs/hooks/useIsomorphicLayoutEffect.d.ts +9 -0
- package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js +22 -6
- package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js.map +1 -1
- package/lib-commonjs/hooks/useMergedRefs.js +25 -22
- package/lib-commonjs/hooks/useMergedRefs.js.map +1 -1
- package/lib-commonjs/hooks/useMount.d.ts +6 -0
- package/lib-commonjs/hooks/useMount.js +27 -0
- package/lib-commonjs/hooks/useMount.js.map +1 -0
- package/lib-commonjs/hooks/useOnClickOutside.d.ts +14 -4
- package/lib-commonjs/hooks/useOnClickOutside.js +141 -21
- package/lib-commonjs/hooks/useOnClickOutside.js.map +1 -1
- package/lib-commonjs/hooks/useOnScrollOutside.d.ts +5 -0
- package/lib-commonjs/hooks/useOnScrollOutside.js +47 -0
- package/lib-commonjs/hooks/useOnScrollOutside.js.map +1 -0
- package/lib-commonjs/hooks/usePrevious.d.ts +1 -0
- package/lib-commonjs/hooks/usePrevious.js +19 -0
- package/lib-commonjs/hooks/usePrevious.js.map +1 -0
- package/lib-commonjs/hooks/useTimeout.d.ts +8 -0
- package/lib-commonjs/hooks/useTimeout.js +40 -0
- package/lib-commonjs/hooks/useTimeout.js.map +1 -0
- package/lib-commonjs/hooks/useUnmount.d.ts +6 -0
- package/lib-commonjs/hooks/useUnmount.js +27 -0
- package/lib-commonjs/hooks/useUnmount.js.map +1 -0
- package/lib-commonjs/index.d.ts +1 -0
- package/lib-commonjs/index.js +11 -2
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/ssr/SSRContext.d.ts +33 -0
- package/lib-commonjs/ssr/SSRContext.js +80 -0
- package/lib-commonjs/ssr/SSRContext.js.map +1 -0
- package/lib-commonjs/ssr/canUseDOM.d.ts +4 -0
- package/lib-commonjs/ssr/canUseDOM.js +17 -0
- package/lib-commonjs/ssr/canUseDOM.js.map +1 -0
- package/lib-commonjs/ssr/index.d.ts +2 -0
- package/lib-commonjs/ssr/index.js +12 -0
- package/lib-commonjs/ssr/index.js.map +1 -0
- package/lib-commonjs/utils/applyTriggerPropsToChildren.d.ts +5 -0
- package/lib-commonjs/utils/applyTriggerPropsToChildren.js +27 -0
- package/lib-commonjs/utils/applyTriggerPropsToChildren.js.map +1 -0
- package/lib-commonjs/utils/clamp.d.ts +8 -0
- package/lib-commonjs/utils/clamp.js +18 -0
- package/lib-commonjs/utils/clamp.js.map +1 -0
- package/lib-commonjs/utils/getNativeElementProps.d.ts +22 -0
- package/lib-commonjs/utils/getNativeElementProps.js +58 -25
- package/lib-commonjs/utils/getNativeElementProps.js.map +1 -1
- package/lib-commonjs/utils/getRTLSafeKey.d.ts +4 -0
- package/lib-commonjs/utils/getRTLSafeKey.js +30 -0
- package/lib-commonjs/utils/getRTLSafeKey.js.map +1 -0
- package/lib-commonjs/utils/index.d.ts +5 -0
- package/lib-commonjs/utils/index.js +19 -2
- package/lib-commonjs/utils/index.js.map +1 -1
- package/lib-commonjs/utils/omit.d.ts +1 -1
- package/lib-commonjs/utils/omit.js +16 -8
- package/lib-commonjs/utils/omit.js.map +1 -1
- package/lib-commonjs/utils/onlyChild.d.ts +5 -0
- package/lib-commonjs/utils/onlyChild.js +23 -0
- package/lib-commonjs/utils/onlyChild.js.map +1 -0
- package/lib-commonjs/utils/properties.js +84 -280
- package/lib-commonjs/utils/properties.js.map +1 -1
- package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.d.ts +7 -0
- package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.js +30 -0
- package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.js.map +1 -0
- package/package.json +11 -9
- package/NOTICE.txt +0 -0
- package/config/api-extractor.json +0 -3
- package/config/tests.js +0 -7
- package/etc/react-utilities.api.md +0 -207
- package/just.config.ts +0 -3
- package/lib/compose/makeMergeProps.d.ts +0 -15
- package/lib/compose/makeMergeProps.js +0 -96
- package/lib/compose/makeMergeProps.js.map +0 -1
- package/lib/compose/resolveShorthandProps.d.ts +0 -7
- package/lib/compose/resolveShorthandProps.js +0 -27
- package/lib/compose/resolveShorthandProps.js.map +0 -1
- package/lib-amd/compose/getSlots.d.ts +0 -20
- package/lib-amd/compose/getSlots.js +0 -51
- package/lib-amd/compose/getSlots.js.map +0 -1
- package/lib-amd/compose/index.d.ts +0 -5
- package/lib-amd/compose/index.js +0 -9
- package/lib-amd/compose/index.js.map +0 -1
- package/lib-amd/compose/makeMergeProps.d.ts +0 -15
- package/lib-amd/compose/makeMergeProps.js +0 -98
- package/lib-amd/compose/makeMergeProps.js.map +0 -1
- package/lib-amd/compose/nullRender.d.ts +0 -4
- package/lib-amd/compose/nullRender.js +0 -9
- package/lib-amd/compose/nullRender.js.map +0 -1
- package/lib-amd/compose/resolveShorthandProps.d.ts +0 -7
- package/lib-amd/compose/resolveShorthandProps.js +0 -29
- package/lib-amd/compose/resolveShorthandProps.js.map +0 -1
- package/lib-amd/compose/types.d.ts +0 -29
- package/lib-amd/compose/types.js +0 -5
- package/lib-amd/compose/types.js.map +0 -1
- package/lib-amd/hooks/index.d.ts +0 -8
- package/lib-amd/hooks/index.js +0 -13
- package/lib-amd/hooks/index.js.map +0 -1
- package/lib-amd/hooks/useBoolean.d.ts +0 -17
- package/lib-amd/hooks/useBoolean.js +0 -26
- package/lib-amd/hooks/useBoolean.js.map +0 -1
- package/lib-amd/hooks/useConst.d.ts +0 -12
- package/lib-amd/hooks/useConst.js +0 -31
- package/lib-amd/hooks/useConst.js.map +0 -1
- package/lib-amd/hooks/useControllableValue.d.ts +0 -20
- package/lib-amd/hooks/useControllableValue.js +0 -42
- package/lib-amd/hooks/useControllableValue.js.map +0 -1
- package/lib-amd/hooks/useEventCallback.d.ts +0 -13
- package/lib-amd/hooks/useEventCallback.js +0 -33
- package/lib-amd/hooks/useEventCallback.js.map +0 -1
- package/lib-amd/hooks/useId.d.ts +0 -11
- package/lib-amd/hooks/useId.js +0 -35
- package/lib-amd/hooks/useId.js.map +0 -1
- package/lib-amd/hooks/useIsomorphicLayoutEffect.d.ts +0 -2
- package/lib-amd/hooks/useIsomorphicLayoutEffect.js +0 -9
- package/lib-amd/hooks/useIsomorphicLayoutEffect.js.map +0 -1
- package/lib-amd/hooks/useMergedRefs.d.ts +0 -13
- package/lib-amd/hooks/useMergedRefs.js +0 -33
- package/lib-amd/hooks/useMergedRefs.js.map +0 -1
- package/lib-amd/hooks/useOnClickOutside.d.ts +0 -19
- package/lib-amd/hooks/useOnClickOutside.js +0 -27
- package/lib-amd/hooks/useOnClickOutside.js.map +0 -1
- package/lib-amd/index.d.ts +0 -3
- package/lib-amd/index.js +0 -8
- package/lib-amd/index.js.map +0 -1
- package/lib-amd/utils/getNativeElementProps.d.ts +0 -9
- package/lib-amd/utils/getNativeElementProps.js +0 -40
- package/lib-amd/utils/getNativeElementProps.js.map +0 -1
- package/lib-amd/utils/index.d.ts +0 -3
- package/lib-amd/utils/index.js +0 -8
- package/lib-amd/utils/index.js.map +0 -1
- package/lib-amd/utils/omit.d.ts +0 -14
- package/lib-amd/utils/omit.js +0 -30
- package/lib-amd/utils/omit.js.map +0 -1
- package/lib-amd/utils/properties.d.ts +0 -148
- package/lib-amd/utils/properties.js +0 -416
- package/lib-amd/utils/properties.js.map +0 -1
- package/lib-commonjs/compose/makeMergeProps.d.ts +0 -15
- package/lib-commonjs/compose/makeMergeProps.js +0 -98
- package/lib-commonjs/compose/makeMergeProps.js.map +0 -1
- package/lib-commonjs/compose/resolveShorthandProps.d.ts +0 -7
- package/lib-commonjs/compose/resolveShorthandProps.js +0 -29
- package/lib-commonjs/compose/resolveShorthandProps.js.map +0 -1
- package/src/compose/README.md +0 -279
package/lib/compose/getSlots.js
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
-
import { getNativeElementProps, omit } from '../utils/index';
|
3
2
|
import { nullRender } from './nullRender';
|
3
|
+
import { omit } from '../utils/omit';
|
4
4
|
/**
|
5
5
|
* Given the state and an array of slot names, will break out `slots` and `slotProps`
|
6
6
|
* collections.
|
7
7
|
*
|
8
|
-
* The root is
|
8
|
+
* The root is derived from a mix of `components` props and `as` prop.
|
9
9
|
*
|
10
10
|
* Slots will render as null if they are rendered as primitives with undefined children.
|
11
11
|
*
|
@@ -17,34 +17,46 @@ import { nullRender } from './nullRender';
|
|
17
17
|
* @param slotNames - Name of which props are slots
|
18
18
|
* @returns An object containing the `slots` map and `slotProps` map.
|
19
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
|
-
|
20
|
+
|
21
|
+
export function getSlots(state, slotNames = ['root']) {
|
22
|
+
const slots = {};
|
23
|
+
const slotProps = {};
|
24
|
+
|
25
|
+
for (const slotName of slotNames) {
|
26
|
+
const [slot, props] = getSlot(state, slotName);
|
27
|
+
slots[slotName] = slot;
|
28
|
+
slotProps[slotName] = props;
|
29
|
+
}
|
30
|
+
|
31
|
+
return {
|
32
|
+
slots,
|
33
|
+
slotProps: slotProps
|
34
|
+
};
|
35
|
+
}
|
36
|
+
|
37
|
+
function getSlot(state, slotName) {
|
38
|
+
var _a, _b, _c;
|
39
|
+
|
40
|
+
if (state[slotName] === undefined) {
|
41
|
+
return [nullRender, undefined];
|
42
|
+
}
|
43
|
+
|
44
|
+
const {
|
45
|
+
children,
|
46
|
+
as: asProp,
|
47
|
+
...rest
|
48
|
+
} = state[slotName];
|
49
|
+
const slot = ((_a = state.components) === null || _a === void 0 ? void 0 : _a[slotName]) === undefined || typeof state.components[slotName] === 'string' ? asProp || ((_b = state.components) === null || _b === void 0 ? void 0 : _b[slotName]) || 'div' : state.components[slotName];
|
50
|
+
|
51
|
+
if (typeof children === 'function') {
|
52
|
+
const render = children;
|
53
|
+
return [React.Fragment, {
|
54
|
+
children: render(slot, rest)
|
55
|
+
}];
|
56
|
+
}
|
57
|
+
|
58
|
+
const shouldOmitAsProp = typeof slot === 'string' && ((_c = state[slotName]) === null || _c === void 0 ? void 0 : _c.as);
|
59
|
+
const slotProps = shouldOmitAsProp ? omit(state[slotName], ['as']) : state[slotName];
|
60
|
+
return [slot, slotProps];
|
61
|
+
}
|
50
62
|
//# sourceMappingURL=getSlots.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["../../src/compose/getSlots.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ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hC,GAEV,KAAK,CAAC,UAAN,CAAiB,QAAjB,CAFJ;;AAIA,MAAI,OAAO,QAAP,KAAoB,UAAxB,EAAoC;AAClC,UAAM,MAAM,GAAG,QAAf;AACA,WAAO,CACL,KAAK,CAAC,QADD,EAEJ;AACC,MAAA,QAAQ,EAAE,MAAM,CAAC,IAAD,EAAO,IAAP;AADjB,KAFI,CAAP;AAMD;;AAED,QAAM,gBAAgB,GAAG,OAAO,IAAP,KAAgB,QAAhB,KAAwB,CAAA,EAAA,GAAI,KAAK,CAAC,QAAD,CAAT,MAAmB,IAAnB,IAAmB,EAAA,KAAA,KAAA,CAAnB,GAAmB,KAAA,CAAnB,GAAmB,EAAA,CAAE,EAA7C,CAAzB;AACA,QAAM,SAAS,GAAI,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,QAAD,CAAN,EAAmB,CAAC,IAAD,CAAnB,CAAP,GAAoC,KAAK,CAAC,QAAD,CAA5E;AAEA,SAAO,CAAC,IAAD,EAAO,SAAP,CAAP;AACD","sourceRoot":""}
|
package/lib/compose/index.d.ts
CHANGED
package/lib/compose/index.js
CHANGED
package/lib/compose/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/compose/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,SAAS,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"nullRender.js","sourceRoot":"
|
1
|
+
{"version":3,"file":"nullRender.js","sourceRoot":"","sources":["../../src/compose/nullRender.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { DefaultObjectShorthandProps, ShorthandProps } from './types';
|
2
|
+
export declare type ResolveShorthandOptions<Props extends Record<string, any>, Required extends boolean = false> = {
|
3
|
+
required?: Required;
|
4
|
+
defaultProps?: Props;
|
5
|
+
};
|
6
|
+
/**
|
7
|
+
* Resolves ShorthandProps into ObjectShorthandProps, to ensure normalization of the signature
|
8
|
+
* being passed down to getSlots method
|
9
|
+
* @param value - the base ShorthandProps
|
10
|
+
* @param options - options to resolve ShorthandProps
|
11
|
+
*/
|
12
|
+
export declare function resolveShorthand<Props extends DefaultObjectShorthandProps, Required extends boolean = false>(value: ShorthandProps<Props>, options?: ResolveShorthandOptions<Props, Required>): Required extends false ? Props | undefined : Props;
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { isValidElement } from 'react';
|
2
|
+
/**
|
3
|
+
* Resolves ShorthandProps into ObjectShorthandProps, to ensure normalization of the signature
|
4
|
+
* being passed down to getSlots method
|
5
|
+
* @param value - the base ShorthandProps
|
6
|
+
* @param options - options to resolve ShorthandProps
|
7
|
+
*/
|
8
|
+
|
9
|
+
export function resolveShorthand(value, options) {
|
10
|
+
const {
|
11
|
+
required = false,
|
12
|
+
defaultProps
|
13
|
+
} = options || {};
|
14
|
+
|
15
|
+
if (value === null || value === undefined && !required) {
|
16
|
+
return undefined;
|
17
|
+
}
|
18
|
+
|
19
|
+
let resolvedShorthand = {};
|
20
|
+
|
21
|
+
if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value) || /*#__PURE__*/isValidElement(value)) {
|
22
|
+
resolvedShorthand.children = value;
|
23
|
+
} else if (typeof value === 'object') {
|
24
|
+
resolvedShorthand = value;
|
25
|
+
}
|
26
|
+
|
27
|
+
return defaultProps ? { ...defaultProps,
|
28
|
+
...resolvedShorthand
|
29
|
+
} : resolvedShorthand;
|
30
|
+
}
|
31
|
+
//# sourceMappingURL=resolveShorthand.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/compose/resolveShorthand.ts"],"names":[],"mappings":"AAAA,SAAS,cAAT,QAA+B,OAA/B;AASA;;;;;AAKG;;AACH,OAAM,SAAU,gBAAV,CACJ,KADI,EAEJ,OAFI,EAE8C;AAElD,QAAM;AAAE,IAAA,QAAQ,GAAG,KAAb;AAAoB,IAAA;AAApB,MAAqC,OAAO,IAAI,EAAtD;;AACA,MAAI,KAAK,KAAK,IAAV,IAAmB,KAAK,KAAK,SAAV,IAAuB,CAAC,QAA/C,EAA0D;AACxD,WAAO,SAAP;AACD;;AAED,MAAI,iBAAiB,GAAG,EAAxB;;AAEA,MAAI,OAAO,KAAP,KAAiB,QAAjB,IAA6B,OAAO,KAAP,KAAiB,QAA9C,IAA0D,KAAK,CAAC,OAAN,CAAc,KAAd,CAA1D,iBAAkF,cAAc,CAAC,KAAD,CAApG,EAA6G;AAC3G,IAAA,iBAAiB,CAAC,QAAlB,GAA6B,KAA7B;AACD,GAFD,MAEO,IAAI,OAAO,KAAP,KAAiB,QAArB,EAA+B;AACpC,IAAA,iBAAiB,GAAG,KAApB;AACD;;AAED,SAAQ,YAAY,GAAG,EAAE,GAAG,YAAL;AAAmB,OAAG;AAAtB,GAAH,GAA+C,iBAAnE;AAGD","sourceRoot":""}
|
package/lib/compose/types.d.ts
CHANGED
@@ -1,29 +1,103 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
+
export declare type ShorthandRenderFunction<Props> = (Component: React.ElementType<Props>, props: Omit<Props, 'children' | 'as'>) => React.ReactNode;
|
3
|
+
export declare type ObjectShorthandPropsRecord = Record<string, DefaultObjectShorthandProps | undefined>;
|
4
|
+
export declare type ShorthandProps<Props extends DefaultObjectShorthandProps> = React.ReactChild | React.ReactNodeArray | React.ReactPortal | number | null | undefined | Props;
|
2
5
|
/**
|
3
|
-
*
|
6
|
+
* Matches any shorthand props type.
|
7
|
+
*
|
8
|
+
* This should ONLY be used in type templates as in `extends DefaultObjectShorthandProps`;
|
9
|
+
* it shouldn't be used as the type of a slot.
|
4
10
|
*/
|
5
|
-
export declare type
|
11
|
+
export declare type DefaultObjectShorthandProps = ObjectShorthandProps<Pick<React.HTMLAttributes<HTMLElement>, 'children' | 'className' | 'style'> & {
|
12
|
+
as?: keyof JSX.IntrinsicElements;
|
13
|
+
}>;
|
6
14
|
/**
|
7
|
-
*
|
15
|
+
* Defines the slot props for a slot that supports a Component type.
|
16
|
+
*
|
17
|
+
* For intrinsic elements like 'div', use {@link IntrinsicShorthandProps} instead.
|
8
18
|
*/
|
9
|
-
export declare type
|
10
|
-
export interface ComponentProps {
|
11
|
-
as?: React.ElementType;
|
12
|
-
className?: string;
|
19
|
+
export declare type ObjectShorthandProps<Props extends {
|
13
20
|
children?: React.ReactNode;
|
14
|
-
}
|
15
|
-
|
16
|
-
export declare type ShorthandProps<TProps extends ComponentProps = {}> = React.ReactChild | React.ReactNodeArray | React.ReactPortal | boolean | number | null | undefined | (TProps & ComponentProps & {
|
17
|
-
children?: TProps['children'] | ShorthandRenderFunction<TProps>;
|
18
|
-
});
|
19
|
-
export declare type ObjectShorthandProps<TProps extends ComponentProps = {}> = TProps & {
|
20
|
-
children?: TProps['children'] | ShorthandRenderFunction<TProps>;
|
21
|
+
} = {}> = Props & {
|
22
|
+
children?: Props['children'] | ShorthandRenderFunction<Props>;
|
21
23
|
};
|
22
|
-
|
23
|
-
|
24
|
-
}
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
/**
|
25
|
+
* Define the slot arguments for a slot that supports one or more intrinsic element types, such as 'div'.
|
26
|
+
* For slots that support custom components, use {@link ObjectShorthandProps} instead.
|
27
|
+
*
|
28
|
+
* The first param is the slot's default type if no `as` prop is specified.
|
29
|
+
* The second param is an optional union of alternative types that can be specified for the `as` prop.
|
30
|
+
*
|
31
|
+
* ```
|
32
|
+
* IntrinsicShorthandProps<'div'> // Slot is always div
|
33
|
+
* IntrinsicShorthandProps<'button', 'a'> // Defaults to button, but allows as="a" with anchor-specific props
|
34
|
+
* IntrinsicShorthandProps<'label', 'span' | 'div'>; // Defaults to label, but allows as="span" or as="div"
|
35
|
+
* ```
|
36
|
+
*/
|
37
|
+
export declare type IntrinsicShorthandProps<DefaultAs extends keyof JSX.IntrinsicElements, AlternateAs extends keyof JSX.IntrinsicElements = never> = IsSingleton<DefaultAs> extends false ? 'Error: first parameter to IntrinsicShorthandProps must be a single element type, not a union of types' : ({
|
38
|
+
as?: DefaultAs;
|
39
|
+
} & ObjectShorthandProps<React.PropsWithRef<JSX.IntrinsicElements[DefaultAs]>>) | {
|
40
|
+
[As in AlternateAs]: {
|
41
|
+
as: As;
|
42
|
+
} & ObjectShorthandProps<React.PropsWithRef<JSX.IntrinsicElements[As]>>;
|
43
|
+
}[AlternateAs];
|
44
|
+
/**
|
45
|
+
* Evaluates to true if the given type contains exactly one string, or false if it is a union of strings.
|
46
|
+
*
|
47
|
+
* ```
|
48
|
+
* IsSingleton<'a'> // true
|
49
|
+
* IsSingleton<'a' | 'b' | 'c'> // false
|
50
|
+
* ```
|
51
|
+
*/
|
52
|
+
export declare type IsSingleton<T extends string> = {
|
53
|
+
[K in T]: Exclude<T, K> extends never ? true : false;
|
54
|
+
}[T];
|
55
|
+
/**
|
56
|
+
* Helper type for inferring the type of the as prop from a Props type.
|
57
|
+
*
|
58
|
+
* For example:
|
59
|
+
* ```
|
60
|
+
* type Example<T> = T extends AsIntrinsicElement<infer As> ? As : never;
|
61
|
+
* ```
|
62
|
+
*/
|
63
|
+
export declare type AsIntrinsicElement<As extends keyof JSX.IntrinsicElements> = {
|
64
|
+
as?: As;
|
29
65
|
};
|
66
|
+
/**
|
67
|
+
* Converts a union type (`A | B | C`) to an intersection type (`A & B & C`)
|
68
|
+
*/
|
69
|
+
export declare type UnionToIntersection<U> = (U extends unknown ? (x: U) => U : never) extends (x: infer I) => U ? I : never;
|
70
|
+
/**
|
71
|
+
* Removes the 'ref' prop from the given Props type, leaving unions intact (such as the discriminated union created by
|
72
|
+
* IntrinsicShorthandProps). This allows IntrinsicShorthandProps to be used with React.forwardRef.
|
73
|
+
*
|
74
|
+
* The conditional "extends unknown" (always true) exploits a quirk in the way TypeScript handles conditional
|
75
|
+
* types, to prevent unions from being expanded.
|
76
|
+
*/
|
77
|
+
export declare type PropsWithoutRef<P> = 'ref' extends keyof P ? (P extends unknown ? Omit<P, 'ref'> : P) : P;
|
78
|
+
export declare type ComponentProps<Shorthands extends ObjectShorthandPropsRecord, Primary extends keyof Shorthands = 'root'> = Omit<{
|
79
|
+
[Key in keyof Shorthands]?: ShorthandProps<NonNullable<Shorthands[Key]>>;
|
80
|
+
}, Primary & 'root'> & PropsWithoutRef<Shorthands[Primary]>;
|
81
|
+
export declare type ComponentState<Shorthands extends ObjectShorthandPropsRecord> = {
|
82
|
+
components?: {
|
83
|
+
[Key in keyof Shorthands]-?: React.ComponentType<NonNullable<Shorthands[Key]> extends ObjectShorthandProps<infer P> ? P : NonNullable<Shorthands[Key]>> | (NonNullable<Shorthands[Key]> extends AsIntrinsicElement<infer As> ? As : keyof JSX.IntrinsicElements);
|
84
|
+
};
|
85
|
+
} & Shorthands;
|
86
|
+
/**
|
87
|
+
* This is part of a hack to infer the element type from a native element *props* type.
|
88
|
+
* The only place the original element is found in a native props type (at least that's workable
|
89
|
+
* for inference) is in the event handlers, so some of the helper types use this event handler
|
90
|
+
* name to infer the original element type.
|
91
|
+
*
|
92
|
+
* Notes:
|
93
|
+
* - Using an extremely obscure event handler reduces the likelihood that its signature will be
|
94
|
+
* modified in any component's props.
|
95
|
+
* - Inferring based on a single prop name instead of a larger type like `DOMAttributes<T>` should be
|
96
|
+
* less expensive for typescript to evaluate and is less likely to result in type expansion in .d.ts.
|
97
|
+
*/
|
98
|
+
declare type ObscureEventName = 'onLostPointerCaptureCapture';
|
99
|
+
/**
|
100
|
+
* Return type for `React.forwardRef`, including inference of the proper typing for the ref.
|
101
|
+
*/
|
102
|
+
export declare type ForwardRefComponent<Props> = ObscureEventName extends keyof Props ? Required<Props>[ObscureEventName] extends React.PointerEventHandler<infer Element> ? React.ForwardRefExoticComponent<Props & React.RefAttributes<Element>> : never : never;
|
103
|
+
export {};
|
package/lib/compose/types.js
CHANGED
@@ -1 +1,6 @@
|
|
1
|
+
export {}; // A definition like this would also work, but typescript is more likely to unnecessarily expand
|
2
|
+
// the props type with this version (and it's likely much more expensive to evaluate)
|
3
|
+
// export type ForwardRefComponent<Props> = Props extends React.DOMAttributes<infer Element>
|
4
|
+
// ? React.ForwardRefExoticComponent<Props> & React.RefAttributes<Element>
|
5
|
+
// : never;
|
1
6
|
//# sourceMappingURL=types.js.map
|
package/lib/compose/types.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["../../src/compose/types.ts"],"names":[],"mappings":"WAgJA;AACA;AACA;AACA;AACA","sourceRoot":""}
|
package/lib/hooks/index.d.ts
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
+
export * from './useControllableState';
|
1
2
|
export * from './useBoolean';
|
2
3
|
export * from './useConst';
|
3
4
|
export * from './useControllableValue';
|
4
5
|
export * from './useEventCallback';
|
5
|
-
export
|
6
|
+
export * from './useFirstMount';
|
7
|
+
export * from './useId';
|
6
8
|
export * from './useIsomorphicLayoutEffect';
|
7
9
|
export * from './useMergedRefs';
|
10
|
+
export * from './useMount';
|
8
11
|
export * from './useOnClickOutside';
|
12
|
+
export * from './useOnScrollOutside';
|
13
|
+
export * from './usePrevious';
|
14
|
+
export * from './useTimeout';
|
15
|
+
export * from './useUnmount';
|
16
|
+
export * from './useForceUpdate';
|
package/lib/hooks/index.js
CHANGED
@@ -1,9 +1,17 @@
|
|
1
|
+
export * from './useControllableState';
|
1
2
|
export * from './useBoolean';
|
2
3
|
export * from './useConst';
|
3
4
|
export * from './useControllableValue';
|
4
5
|
export * from './useEventCallback';
|
5
|
-
export
|
6
|
+
export * from './useFirstMount';
|
7
|
+
export * from './useId';
|
6
8
|
export * from './useIsomorphicLayoutEffect';
|
7
9
|
export * from './useMergedRefs';
|
10
|
+
export * from './useMount';
|
8
11
|
export * from './useOnClickOutside';
|
12
|
+
export * from './useOnScrollOutside';
|
13
|
+
export * from './usePrevious';
|
14
|
+
export * from './useTimeout';
|
15
|
+
export * from './useUnmount';
|
16
|
+
export * from './useForceUpdate';
|
9
17
|
//# sourceMappingURL=index.js.map
|
package/lib/hooks/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AACxB,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC"}
|
@@ -1,12 +1,12 @@
|
|
1
1
|
/** Updater callbacks returned by `useBoolean`. */
|
2
|
-
export
|
2
|
+
export declare type UseBooleanCallbacks = {
|
3
3
|
/** Set the value to true. Always has the same identity. */
|
4
4
|
setTrue: () => void;
|
5
5
|
/** Set the value to false. Always has the same identity. */
|
6
6
|
setFalse: () => void;
|
7
7
|
/** Toggle the value. Always has the same identity. */
|
8
8
|
toggle: () => void;
|
9
|
-
}
|
9
|
+
};
|
10
10
|
/**
|
11
11
|
* Hook to store a value and generate callbacks for setting the value to true or false.
|
12
12
|
* The identity of the callbacks will always stay the same.
|
package/lib/hooks/useBoolean.js
CHANGED
@@ -7,17 +7,22 @@ import { useConst } from './useConst';
|
|
7
7
|
* @param initialState - Initial value
|
8
8
|
* @returns Array with the current value and an object containing the updater callbacks.
|
9
9
|
*/
|
10
|
+
|
10
11
|
export function useBoolean(initialState) {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
const [value, setValue] = React.useState(initialState);
|
13
|
+
const setTrue = useConst(() => () => {
|
14
|
+
setValue(true);
|
15
|
+
});
|
16
|
+
const setFalse = useConst(() => () => {
|
17
|
+
setValue(false);
|
18
|
+
});
|
19
|
+
const toggle = useConst(() => () => {
|
20
|
+
setValue(currentValue => !currentValue);
|
21
|
+
});
|
22
|
+
return [value, {
|
23
|
+
setTrue,
|
24
|
+
setFalse,
|
25
|
+
toggle
|
26
|
+
}];
|
22
27
|
}
|
23
28
|
//# sourceMappingURL=useBoolean.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["../../src/hooks/useBoolean.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,QAAT,QAAyB,YAAzB;AAYA;;;;;;AAMG;;AACH,OAAM,SAAU,UAAV,CAAqB,YAArB,EAA0C;AAC9C,QAAM,CAAC,KAAD,EAAQ,QAAR,IAAoB,KAAK,CAAC,QAAN,CAAe,YAAf,CAA1B;AAEA,QAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,MAAK;AAClC,IAAA,QAAQ,CAAC,IAAD,CAAR;AACD,GAFuB,CAAxB;AAGA,QAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,MAAK;AACnC,IAAA,QAAQ,CAAC,KAAD,CAAR;AACD,GAFwB,CAAzB;AAGA,QAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,MAAK;AACjC,IAAA,QAAQ,CAAC,YAAY,IAAI,CAAC,YAAlB,CAAR;AACD,GAFsB,CAAvB;AAIA,SAAO,CAAC,KAAD,EAAQ;AAAE,IAAA,OAAF;AAAW,IAAA,QAAX;AAAqB,IAAA;AAArB,GAAR,CAAP;AACD","sourceRoot":""}
|
package/lib/hooks/useConst.js
CHANGED
@@ -10,18 +10,21 @@ import * as React from 'react';
|
|
10
10
|
* only the value/function passed in the first time this is called is respected.
|
11
11
|
* @returns The value. The identity of this value will always be the same.
|
12
12
|
*/
|
13
|
+
|
13
14
|
export function useConst(initialValue) {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
}
|
25
|
-
|
15
|
+
// Use useRef to store the value because it's the least expensive built-in hook that works here
|
16
|
+
// (we could also use `const [value] = React.useState(initialValue)` but that's more expensive
|
17
|
+
// internally due to reducer handling which we don't need)
|
18
|
+
const ref = React.useRef();
|
19
|
+
|
20
|
+
if (ref.current === undefined) {
|
21
|
+
// Box the value in an object so we can tell if it's initialized even if the initializer
|
22
|
+
// returns/is undefined
|
23
|
+
ref.current = {
|
24
|
+
value: typeof initialValue === 'function' ? initialValue() : initialValue
|
25
|
+
};
|
26
|
+
}
|
27
|
+
|
28
|
+
return ref.current.value;
|
26
29
|
}
|
27
30
|
//# sourceMappingURL=useConst.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["../../src/hooks/useConst.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAEA;;;;;;;;;;AAUG;;AACH,OAAM,SAAU,QAAV,CAAsB,YAAtB,EAAiD;AACrD;AACA;AACA;AACA,QAAM,GAAG,GAAG,KAAK,CAAC,MAAN,EAAZ;;AACA,MAAI,GAAG,CAAC,OAAJ,KAAgB,SAApB,EAA+B;AAC7B;AACA;AACA,IAAA,GAAG,CAAC,OAAJ,GAAc;AACZ,MAAA,KAAK,EAAE,OAAO,YAAP,KAAwB,UAAxB,GAAsC,YAAyB,EAA/D,GAAoE;AAD/D,KAAd;AAGD;;AACD,SAAO,GAAG,CAAC,OAAJ,CAAY,KAAnB;AACD","sourceRoot":""}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
export declare type UseControllableStateOptions<State> = {
|
3
|
+
/**
|
4
|
+
* User provided default state or factory initializer
|
5
|
+
*/
|
6
|
+
defaultState?: State | (() => State);
|
7
|
+
/**
|
8
|
+
* User provided controllable state, undefined state means internal state will be used
|
9
|
+
*/
|
10
|
+
state: State | undefined;
|
11
|
+
/**
|
12
|
+
* Used to initialize state if all user provided states are undefined
|
13
|
+
*/
|
14
|
+
initialState: State;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* A useState 'like' hook that allows optional user control
|
18
|
+
* Useful for components which allow uncontrolled and controlled behaviour for users
|
19
|
+
* @returns - https://reactjs.org/docs/hooks-state.html
|
20
|
+
*/
|
21
|
+
export declare const useControllableState: <State>(options: UseControllableStateOptions<State>) => [State, React.Dispatch<React.SetStateAction<State>>];
|
@@ -0,0 +1,68 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
import { useConst } from './useConst';
|
3
|
+
|
4
|
+
function isUndefined(state) {
|
5
|
+
return typeof state === 'undefined';
|
6
|
+
}
|
7
|
+
|
8
|
+
function isFactoryDispatch(newState) {
|
9
|
+
return typeof newState === 'function';
|
10
|
+
}
|
11
|
+
/**
|
12
|
+
* A useState 'like' hook that allows optional user control
|
13
|
+
* Useful for components which allow uncontrolled and controlled behaviour for users
|
14
|
+
* @returns - https://reactjs.org/docs/hooks-state.html
|
15
|
+
*/
|
16
|
+
|
17
|
+
|
18
|
+
export const useControllableState = options => {
|
19
|
+
const isControlled = useIsControlled(options.state);
|
20
|
+
const initialState = isUndefined(options.defaultState) ? options.initialState : options.defaultState;
|
21
|
+
const [internalState, setInternalState] = React.useState(initialState);
|
22
|
+
const state = isControlled ? options.state : internalState;
|
23
|
+
const stateRef = React.useRef(state);
|
24
|
+
React.useEffect(() => {
|
25
|
+
stateRef.current = state;
|
26
|
+
}, [state]); // To match the behavior of the setter returned by React.useState, this callback's identity
|
27
|
+
// should never change. This means it MUST NOT directly reference variables that can change.
|
28
|
+
|
29
|
+
const setState = React.useCallback(newState => {
|
30
|
+
// React dispatch can use a factory
|
31
|
+
// https://reactjs.org/docs/hooks-reference.html#functional-updates
|
32
|
+
if (isFactoryDispatch(newState)) {
|
33
|
+
stateRef.current = newState(stateRef.current);
|
34
|
+
} else {
|
35
|
+
stateRef.current = newState;
|
36
|
+
}
|
37
|
+
|
38
|
+
setInternalState(stateRef.current);
|
39
|
+
}, []);
|
40
|
+
return [state, setState];
|
41
|
+
};
|
42
|
+
/**
|
43
|
+
* Helper hook to handle previous comparison of controlled/uncontrolled
|
44
|
+
* Prints an error when isControlled value switches between subsequent renders
|
45
|
+
* @returns - whether the value is controlled
|
46
|
+
*/
|
47
|
+
|
48
|
+
const useIsControlled = controlledValue => {
|
49
|
+
const isControlled = useConst(controlledValue !== undefined);
|
50
|
+
|
51
|
+
if (process.env.NODE_ENV !== 'production') {
|
52
|
+
// We don't want these warnings in production even though it is against native behaviour
|
53
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
54
|
+
React.useEffect(() => {
|
55
|
+
if (isControlled !== (controlledValue !== undefined)) {
|
56
|
+
const error = new Error();
|
57
|
+
const controlWarning = isControlled ? 'a controlled value to be uncontrolled' : 'an uncontrolled value to be controlled';
|
58
|
+
const undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined'; // eslint-disable-next-line no-console
|
59
|
+
|
60
|
+
console.error([// Default react error
|
61
|
+
'A component is changing ' + controlWarning + '. This is likely caused by the value', 'changing from ' + undefinedWarning + ' value, which should not happen.', 'Decide between using a controlled or uncontrolled input element for the lifetime of the component.', 'More info: https://reactjs.org/link/controlled-components', error.stack].join(' '));
|
62
|
+
}
|
63
|
+
}, [isControlled, controlledValue]);
|
64
|
+
}
|
65
|
+
|
66
|
+
return isControlled;
|
67
|
+
};
|
68
|
+
//# sourceMappingURL=useControllableState.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/useContro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