@fluentui/react-utilities 0.0.0-nightlyf3ce1fbe1d20211110.1 → 0.0.0-nightlyf6116328d620220124.1
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 +99 -9
- package/CHANGELOG.md +26 -6
- package/dist/react-utilities.d.ts +68 -38
- package/lib/compose/getSlots.d.ts +5 -4
- package/lib/compose/getSlots.js +6 -4
- package/lib/compose/getSlots.js.map +1 -1
- package/lib/compose/types.d.ts +3 -4
- package/lib/compose/types.js.map +1 -1
- package/lib/hooks/index.d.ts +1 -1
- package/lib/hooks/index.js +1 -1
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/useControllableState.d.ts +16 -6
- package/lib/hooks/useControllableState.js +13 -8
- package/lib/hooks/useControllableState.js.map +1 -1
- package/lib/hooks/useOnClickOutside.js.map +1 -1
- package/lib/hooks/useTriggerElement.d.ts +18 -0
- package/lib/hooks/useTriggerElement.js +90 -0
- package/lib/hooks/useTriggerElement.js.map +1 -0
- package/lib/utils/applyTriggerPropsToChildren.d.ts +1 -1
- package/lib/utils/getNativeElementProps.d.ts +22 -0
- package/lib/utils/getNativeElementProps.js +22 -0
- package/lib/utils/getNativeElementProps.js.map +1 -1
- package/lib/utils/getReactCallbackName.d.ts +25 -0
- package/lib/utils/getReactCallbackName.js +26 -0
- package/lib/utils/getReactCallbackName.js.map +1 -0
- package/lib/utils/onlyChild.d.ts +1 -1
- package/lib/utils/properties.js +40 -20
- package/lib/utils/properties.js.map +1 -1
- package/lib-commonjs/compose/getSlots.d.ts +5 -4
- package/lib-commonjs/compose/getSlots.js +6 -4
- package/lib-commonjs/compose/getSlots.js.map +1 -1
- package/lib-commonjs/compose/types.d.ts +3 -4
- package/lib-commonjs/compose/types.js.map +1 -1
- package/lib-commonjs/hooks/index.d.ts +1 -1
- package/lib-commonjs/hooks/index.js +2 -2
- package/lib-commonjs/hooks/index.js.map +1 -1
- package/lib-commonjs/hooks/useControllableState.d.ts +16 -6
- package/lib-commonjs/hooks/useControllableState.js +13 -8
- package/lib-commonjs/hooks/useControllableState.js.map +1 -1
- package/lib-commonjs/hooks/useOnClickOutside.js.map +1 -1
- package/lib-commonjs/hooks/useTriggerElement.d.ts +18 -0
- package/lib-commonjs/hooks/useTriggerElement.js +105 -0
- package/lib-commonjs/hooks/useTriggerElement.js.map +1 -0
- package/lib-commonjs/utils/applyTriggerPropsToChildren.d.ts +1 -1
- package/lib-commonjs/utils/getNativeElementProps.d.ts +22 -0
- package/lib-commonjs/utils/getNativeElementProps.js +25 -1
- package/lib-commonjs/utils/getNativeElementProps.js.map +1 -1
- package/lib-commonjs/utils/getReactCallbackName.d.ts +25 -0
- package/lib-commonjs/utils/getReactCallbackName.js +35 -0
- package/lib-commonjs/utils/getReactCallbackName.js.map +1 -0
- package/lib-commonjs/utils/onlyChild.d.ts +1 -1
- package/lib-commonjs/utils/properties.js +40 -20
- package/lib-commonjs/utils/properties.js.map +1 -1
- package/package.json +6 -11
- package/lib/hooks/useControllableValue.d.ts +0 -20
- package/lib/hooks/useControllableValue.js +0 -57
- package/lib/hooks/useControllableValue.js.map +0 -1
- package/lib-commonjs/hooks/useControllableValue.d.ts +0 -20
- package/lib-commonjs/hooks/useControllableValue.js +0 -68
- package/lib-commonjs/hooks/useControllableValue.js.map +0 -1
@@ -1,20 +0,0 @@
|
|
1
|
-
import * as React from 'react';
|
2
|
-
export declare type ChangeCallback<TElement extends HTMLElement, TValue, TEvent extends React.SyntheticEvent<TElement> | undefined> = (ev: TEvent, newValue: TValue | undefined) => void;
|
3
|
-
/**
|
4
|
-
* Default value can be a value or an initializer
|
5
|
-
*/
|
6
|
-
declare type DefaultValue<TValue> = TValue | (() => TValue);
|
7
|
-
/**
|
8
|
-
* Hook to manage a value that could be either controlled or uncontrolled, such as a checked state or
|
9
|
-
* text box string.
|
10
|
-
* @param controlledValue - The controlled value passed in the props. This value will always be used if provided,
|
11
|
-
* and the internal state will be updated to reflect it.
|
12
|
-
* @param defaultUncontrolledValue - Initial value for the internal state in the uncontrolled case.
|
13
|
-
* @returns An array of the current value and an updater callback. Like `React.useState`, the updater
|
14
|
-
* callback always has the same identity, and it can take either a new value, or a function which
|
15
|
-
* is passed the previous value and returns the new value.
|
16
|
-
* @see https://reactjs.org/docs/uncontrolled-components.html
|
17
|
-
*/
|
18
|
-
export declare function useControllableValue<TValue, TElement extends HTMLElement>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>): Readonly<[TValue, (update: React.SetStateAction<TValue>) => void]>;
|
19
|
-
export declare function useControllableValue<TValue, TElement extends HTMLElement, TEvent extends React.SyntheticEvent<TElement> | undefined>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>, onChange: ChangeCallback<TElement, TValue, TEvent>): Readonly<[TValue, (update: React.SetStateAction<TValue>, ev?: React.FormEvent<TElement>) => void]>;
|
20
|
-
export {};
|
@@ -1,57 +0,0 @@
|
|
1
|
-
import * as React from 'react';
|
2
|
-
import { useConst } from './useConst';
|
3
|
-
export function useControllableValue(controlledValue, defaultUncontrolledValue, onChange) {
|
4
|
-
const [value, setValue] = React.useState(defaultUncontrolledValue);
|
5
|
-
const isControlled = useIsControlled(controlledValue);
|
6
|
-
const currentValue = isControlled ? controlledValue : value; // Duplicate the current value and onChange in refs so they're accessible from
|
7
|
-
// setValueOrCallOnChange without creating a new callback every time
|
8
|
-
|
9
|
-
const valueRef = React.useRef(currentValue);
|
10
|
-
const onChangeRef = React.useRef(onChange);
|
11
|
-
React.useEffect(() => {
|
12
|
-
valueRef.current = currentValue;
|
13
|
-
onChangeRef.current = onChange;
|
14
|
-
}); // To match the behavior of the setter returned by React.useState, this callback's identity
|
15
|
-
// should never change. This means it MUST NOT directly reference variables that can change.
|
16
|
-
|
17
|
-
const setValueOrCallOnChange = useConst(() => (update, ev) => {
|
18
|
-
// Assuming here that TValue is not a function, because a controllable value will typically
|
19
|
-
// be something a user can enter as input
|
20
|
-
const newValue = typeof update === 'function' ? update(valueRef.current) : update;
|
21
|
-
|
22
|
-
if (onChangeRef.current) {
|
23
|
-
onChangeRef.current(ev, newValue);
|
24
|
-
}
|
25
|
-
|
26
|
-
if (!isControlled) {
|
27
|
-
setValue(newValue);
|
28
|
-
}
|
29
|
-
});
|
30
|
-
return [currentValue, setValueOrCallOnChange];
|
31
|
-
}
|
32
|
-
/**
|
33
|
-
* Helper hook to handle previous comparison of controlled/uncontrolled
|
34
|
-
* Prints an error when isControlled value switches between subsequent renders
|
35
|
-
*/
|
36
|
-
|
37
|
-
const useIsControlled = controlledValue => {
|
38
|
-
const isControlled = useConst(controlledValue !== undefined);
|
39
|
-
|
40
|
-
if (process.env.NODE_ENV !== 'production') {
|
41
|
-
// We don't want these warnings in production even though it is against native behaviour
|
42
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
43
|
-
React.useEffect(() => {
|
44
|
-
if (isControlled !== (controlledValue !== undefined)) {
|
45
|
-
const error = new Error();
|
46
|
-
const controlWarning = isControlled ? 'a controlled value to be uncontrolled' : 'an uncontrolled value to be controlled';
|
47
|
-
const undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined'; // eslint-disable-next-line no-console
|
48
|
-
|
49
|
-
console.error([// Default react error
|
50
|
-
'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(' '));
|
51
|
-
}
|
52
|
-
}, [isControlled, controlledValue]);
|
53
|
-
}
|
54
|
-
|
55
|
-
return isControlled;
|
56
|
-
};
|
57
|
-
//# sourceMappingURL=useControllableValue.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/useControllableValue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,QAAT,QAAyB,YAAzB;AAqCA,OAAM,SAAU,oBAAV,CAKJ,e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M,eAAe,KAAK,SAA1B,CAAhB,EAAsD;AACpD,cAAM,KAAK,GAAG,IAAI,KAAJ,EAAd;AAEA,cAAM,cAAc,GAAG,YAAY,GAC/B,uCAD+B,GAE/B,wCAFJ;AAIA,cAAM,gBAAgB,GAAG,YAAY,GAAG,yBAAH,GAA+B,wBAApE,CAPoD,CASpD;;AACA,QAAA,OAAO,CAAC,KAAR,CACE,CACE;AACA,qCAA6B,cAA7B,GAA8C,sCAFhD,EAGE,mBAAmB,gBAAnB,GAAsC,kCAHxC,EAIE,oGAJF,EAKE,2DALF,EAME,KAAK,CAAC,KANR,EAOE,IAPF,CAOO,GAPP,CADF;AAUD;AACF,KAtBD,EAsBG,CAAC,YAAD,EAAe,eAAf,CAtBH;AAuBD;;AAED,SAAO,YAAP;AACD,CAhCD","sourceRoot":""}
|
@@ -1,20 +0,0 @@
|
|
1
|
-
import * as React from 'react';
|
2
|
-
export declare type ChangeCallback<TElement extends HTMLElement, TValue, TEvent extends React.SyntheticEvent<TElement> | undefined> = (ev: TEvent, newValue: TValue | undefined) => void;
|
3
|
-
/**
|
4
|
-
* Default value can be a value or an initializer
|
5
|
-
*/
|
6
|
-
declare type DefaultValue<TValue> = TValue | (() => TValue);
|
7
|
-
/**
|
8
|
-
* Hook to manage a value that could be either controlled or uncontrolled, such as a checked state or
|
9
|
-
* text box string.
|
10
|
-
* @param controlledValue - The controlled value passed in the props. This value will always be used if provided,
|
11
|
-
* and the internal state will be updated to reflect it.
|
12
|
-
* @param defaultUncontrolledValue - Initial value for the internal state in the uncontrolled case.
|
13
|
-
* @returns An array of the current value and an updater callback. Like `React.useState`, the updater
|
14
|
-
* callback always has the same identity, and it can take either a new value, or a function which
|
15
|
-
* is passed the previous value and returns the new value.
|
16
|
-
* @see https://reactjs.org/docs/uncontrolled-components.html
|
17
|
-
*/
|
18
|
-
export declare function useControllableValue<TValue, TElement extends HTMLElement>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>): Readonly<[TValue, (update: React.SetStateAction<TValue>) => void]>;
|
19
|
-
export declare function useControllableValue<TValue, TElement extends HTMLElement, TEvent extends React.SyntheticEvent<TElement> | undefined>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>, onChange: ChangeCallback<TElement, TValue, TEvent>): Readonly<[TValue, (update: React.SetStateAction<TValue>, ev?: React.FormEvent<TElement>) => void]>;
|
20
|
-
export {};
|
@@ -1,68 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.useControllableValue = void 0;
|
7
|
-
|
8
|
-
const React = /*#__PURE__*/require("react");
|
9
|
-
|
10
|
-
const useConst_1 = /*#__PURE__*/require("./useConst");
|
11
|
-
|
12
|
-
function useControllableValue(controlledValue, defaultUncontrolledValue, onChange) {
|
13
|
-
const [value, setValue] = React.useState(defaultUncontrolledValue);
|
14
|
-
const isControlled = useIsControlled(controlledValue);
|
15
|
-
const currentValue = isControlled ? controlledValue : value; // Duplicate the current value and onChange in refs so they're accessible from
|
16
|
-
// setValueOrCallOnChange without creating a new callback every time
|
17
|
-
|
18
|
-
const valueRef = React.useRef(currentValue);
|
19
|
-
const onChangeRef = React.useRef(onChange);
|
20
|
-
React.useEffect(() => {
|
21
|
-
valueRef.current = currentValue;
|
22
|
-
onChangeRef.current = onChange;
|
23
|
-
}); // To match the behavior of the setter returned by React.useState, this callback's identity
|
24
|
-
// should never change. This means it MUST NOT directly reference variables that can change.
|
25
|
-
|
26
|
-
const setValueOrCallOnChange = useConst_1.useConst(() => (update, ev) => {
|
27
|
-
// Assuming here that TValue is not a function, because a controllable value will typically
|
28
|
-
// be something a user can enter as input
|
29
|
-
const newValue = typeof update === 'function' ? update(valueRef.current) : update;
|
30
|
-
|
31
|
-
if (onChangeRef.current) {
|
32
|
-
onChangeRef.current(ev, newValue);
|
33
|
-
}
|
34
|
-
|
35
|
-
if (!isControlled) {
|
36
|
-
setValue(newValue);
|
37
|
-
}
|
38
|
-
});
|
39
|
-
return [currentValue, setValueOrCallOnChange];
|
40
|
-
}
|
41
|
-
|
42
|
-
exports.useControllableValue = useControllableValue;
|
43
|
-
/**
|
44
|
-
* Helper hook to handle previous comparison of controlled/uncontrolled
|
45
|
-
* Prints an error when isControlled value switches between subsequent renders
|
46
|
-
*/
|
47
|
-
|
48
|
-
const useIsControlled = controlledValue => {
|
49
|
-
const isControlled = useConst_1.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=useControllableValue.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/useControllableValue.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;;AACA,MAAA,UAAA,gBAAA,OAAA,CAAA,YAAA,CAAA;;AAqCA,SAAgB,oBAAhB,CAKE,eALF,EAME,wBANF,EAOE,QAPF,EAOqD;AAEnD,QAAM,CAAC,KAAD,EAAQ,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