@react-stately/utils 3.0.0-nightly.1373 → 3.0.0-nightly.1379

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/dist/main.js CHANGED
@@ -40,6 +40,7 @@ function $28a294aa55e111af$export$40bfa8c7b0832715(value1, defaultValue, onChang
40
40
  if (!isControlled) stateRef.current = value;
41
41
  };
42
42
  if (typeof value2 === 'function') {
43
+ console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');
43
44
  // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
44
45
  // when someone using useControlledState calls setControlledState(myFunc)
45
46
  // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACsB,CAAC;IACnE,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,qBAAQ,CAAC,MAAK,IAAI,YAAY;IAChE,GAAG,CAAC,GAAG,GAAG,mBAAM,CAAC,MAAK,KAAK,SAAS;IACpC,GAAG,CAAC,aAAa,GAAG,GAAG,CAAC,OAAO;IAC/B,GAAG,CAAC,YAAY,GAAG,MAAK,KAAK,SAAS;IACtC,EAA2C,AAA3C,yCAA2C;IAC3C,GAAG,CAAC,QAAQ,GAAG,mBAAM,CAAC,UAAU;IAChC,EAAE,EAAE,aAAa,KAAK,YAAY,EAChC,OAAO,CAAC,IAAI,EAAE,+BAA+B,EAAE,aAAa,GAAG,CAAY,cAAG,CAAc,cAAC,IAAI,EAAE,YAAY,GAAG,CAAY,cAAG,CAAc,cAAC,CAAC;IAGnJ,GAAG,CAAC,OAAO,GAAG,YAAY;IAE1B,GAAG,CAAC,QAAQ,GAAG,wBAAW,EAAE,MAAK,KAAK,IAAI,GAAK,CAAC;QAC9C,GAAG,CAAC,cAAc,IAAI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AAE,CAAC;QAClB,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS;QAChC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,GAAG,IAAI,GAAG;IACrD,CAAC;IAED,MAAM,CAAC,YAAY;AACrB,CAAC;SAGe,wCAAa,CAAC,KAAa,EAAE,MAAc,EAAE,IAAY,GAAG,EAAE,EAAU,CAAC;IACvF,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM;IAEjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,IAAI,GAAG;AACtC,CAAC;;","sources":["packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/useControlledState.ts","packages/@react-stately/utils/src/number.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './useControlledState';\nexport * from './number';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useRef, useState} from 'react';\n\nexport function useControlledState<T>(\n value: T,\n defaultValue: T,\n onChange: (value: T, ...args: any[]) => void\n): [T, (value: T | ((prevState: T) => T), ...args: any[]) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n let ref = useRef(value !== undefined);\n let wasControlled = ref.current;\n let isControlled = value !== undefined;\n // Internal state reference for useCallback\n let stateRef = useRef(stateValue);\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n\n ref.current = isControlled;\n\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(stateRef.current, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n stateRef.current = value;\n }\n };\n\n if (typeof value === 'function') {\n // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates\n // when someone using useControlledState calls setControlledState(myFunc)\n // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc\n // if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning\n // otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same\n let updateFunction = (oldValue, ...functionArgs) => {\n let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);\n onChangeCaller(interceptedValue, ...args);\n if (!isControlled) {\n return interceptedValue;\n }\n return oldValue;\n };\n setStateValue(updateFunction);\n } else {\n if (!isControlled) {\n setStateValue(value);\n }\n onChangeCaller(value, ...args);\n }\n }, [isControlled, onChange]);\n\n // If a controlled component's value prop changes, we need to update stateRef\n if (isControlled) {\n stateRef.current = value;\n } else {\n value = stateValue;\n }\n\n return [value, setValue];\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\n/**\n * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.\n */\nexport function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {\n let newValue = Math.min(Math.max(value, min), max);\n return newValue;\n}\n\nexport function snapValueToStep(value: number, min: number, max: number, step: number): number {\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder;\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor((max - min) / step) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(max / step) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n let string = step.toString();\n let index = string.indexOf('.');\n let precision = index >= 0 ? string.length - index : 0;\n\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n snappedValue = Math.round(snappedValue * pow) / pow;\n }\n\n return snappedValue;\n}\n\n/* Takes a value and rounds off to the number of digits. */\nexport function toFixedNumber(value: number, digits: number, base: number = 10): number {\n const pow = Math.pow(base, digits);\n\n return Math.round(value * pow) / pow;\n}\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACF,CAAC;IAC3C,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,qBAAQ,CAAC,MAAK,IAAI,YAAY;IAChE,GAAG,CAAC,GAAG,GAAG,mBAAM,CAAC,MAAK,KAAK,SAAS;IACpC,GAAG,CAAC,aAAa,GAAG,GAAG,CAAC,OAAO;IAC/B,GAAG,CAAC,YAAY,GAAG,MAAK,KAAK,SAAS;IACtC,EAA2C,AAA3C,yCAA2C;IAC3C,GAAG,CAAC,QAAQ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AAG,KAAK,YAAY,GAAG,GAAG,EAC1C,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,IAAI,IAAI;IAG9C,EAAgE,AAAhE,8DAAgE;IAChE,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ;IAC1B,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,CAAG;IAC9B,GAAG,CAAC,SAAS,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,KAAK,GAAG,CAAC;IAEtD,EAAE,EAAE,SAAS,GAAG,CAAC,EAAE,CAAC;QAClB,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS;QAChC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,GAAG,IAAI,GAAG;IACrD,CAAC;IAED,MAAM,CAAC,YAAY;AACrB,CAAC;SAGe,wCAAa,CAAC,KAAa,EAAE,MAAc,EAAE,IAAY,GAAG,EAAE,EAAU,CAAC;IACvF,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM;IAEjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,IAAI,GAAG;AACtC,CAAC;;","sources":["packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/useControlledState.ts","packages/@react-stately/utils/src/number.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './useControlledState';\nexport * from './number';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useRef, useState} from 'react';\n\nexport function useControlledState<T>(\n value: T,\n defaultValue: T,\n onChange: (value: T, ...args: any[]) => void\n): [T, (value: T, ...args: any[]) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n let ref = useRef(value !== undefined);\n let wasControlled = ref.current;\n let isControlled = value !== undefined;\n // Internal state reference for useCallback\n let stateRef = useRef(stateValue);\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n\n ref.current = isControlled;\n\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(stateRef.current, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n stateRef.current = value;\n }\n };\n\n if (typeof value === 'function') {\n console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');\n // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates\n // when someone using useControlledState calls setControlledState(myFunc)\n // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc\n // if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning\n // otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same\n let updateFunction = (oldValue, ...functionArgs) => {\n let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);\n onChangeCaller(interceptedValue, ...args);\n if (!isControlled) {\n return interceptedValue;\n }\n return oldValue;\n };\n setStateValue(updateFunction);\n } else {\n if (!isControlled) {\n setStateValue(value);\n }\n onChangeCaller(value, ...args);\n }\n }, [isControlled, onChange]);\n\n // If a controlled component's value prop changes, we need to update stateRef\n if (isControlled) {\n stateRef.current = value;\n } else {\n value = stateValue;\n }\n\n return [value, setValue];\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\n/**\n * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.\n */\nexport function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {\n let newValue = Math.min(Math.max(value, min), max);\n return newValue;\n}\n\nexport function snapValueToStep(value: number, min: number, max: number, step: number): number {\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder;\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor((max - min) / step) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(max / step) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n let string = step.toString();\n let index = string.indexOf('.');\n let precision = index >= 0 ? string.length - index : 0;\n\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n snappedValue = Math.round(snappedValue * pow) / pow;\n }\n\n return snappedValue;\n}\n\n/* Takes a value and rounds off to the number of digits. */\nexport function toFixedNumber(value: number, digits: number, base: number = 10): number {\n const pow = Math.pow(base, digits);\n\n return Math.round(value * pow) / pow;\n}\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -24,6 +24,7 @@ function $8b1a26670e051641$export$40bfa8c7b0832715(value1, defaultValue, onChang
24
24
  if (!isControlled) stateRef.current = value;
25
25
  };
26
26
  if (typeof value2 === 'function') {
27
+ console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');
27
28
  // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
28
29
  // when someone using useControlledState calls setControlledState(myFunc)
29
30
  // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACsB,CAAC;IACnE,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,eAAQ,CAAC,MAAK,IAAI,YAAY;IAChE,GAAG,CAAC,GAAG,GAAG,aAAM,CAAC,MAAK,KAAK,SAAS;IACpC,GAAG,CAAC,aAAa,GAAG,GAAG,CAAC,OAAO;IAC/B,GAAG,CAAC,YAAY,GAAG,MAAK,KAAK,SAAS;IACtC,EAA2C,AAA3C,yCAA2C;IAC3C,GAAG,CAAC,QAAQ,GAAG,aAAM,CAAC,UAAU;IAChC,EAAE,EAAE,aAAa,KAAK,YAAY,EAChC,OAAO,CAAC,IAAI,EAAE,+BAA+B,EAAE,aAAa,GAAG,CAAY,cAAG,CAAc,cAAC,IAAI,EAAE,YAAY,GAAG,CAAY,cAAG,CAAc,cAAC,CAAC;IAGnJ,GAAG,CAAC,OAAO,GAAG,YAAY;IAE1B,GAAG,CAAC,QAAQ,GAAG,kBAAW,EAAE,MAAK,KAAK,IAAI,GAAK,CAAC;QAC9C,GAAG,CAAC,cAAc,IAAI,KAAK,KAAK,YAAY,GAAK,CAAC;YAChD,EAAE,EAAE,QAAQ,EACV,CAAC;gBAAD,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,KAAK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G,IAAI,GAAG;IACrD,CAAC;IAED,MAAM,CAAC,YAAY;AACrB,CAAC;SAGe,wCAAa,CAAC,KAAa,EAAE,MAAc,EAAE,IAAY,GAAG,EAAE,EAAU,CAAC;IACvF,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM;IAEjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,IAAI,GAAG;AACtC,CAAC;;","sources":["packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/useControlledState.ts","packages/@react-stately/utils/src/number.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './useControlledState';\nexport * from './number';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useRef, useState} from 'react';\n\nexport function useControlledState<T>(\n value: T,\n defaultValue: T,\n onChange: (value: T, ...args: any[]) => void\n): [T, (value: T | ((prevState: T) => T), ...args: any[]) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n let ref = useRef(value !== undefined);\n let wasControlled = ref.current;\n let isControlled = value !== undefined;\n // Internal state reference for useCallback\n let stateRef = useRef(stateValue);\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n\n ref.current = isControlled;\n\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(stateRef.current, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n stateRef.current = value;\n }\n };\n\n if (typeof value === 'function') {\n // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates\n // when someone using useControlledState calls setControlledState(myFunc)\n // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc\n // if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning\n // otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same\n let updateFunction = (oldValue, ...functionArgs) => {\n let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);\n onChangeCaller(interceptedValue, ...args);\n if (!isControlled) {\n return interceptedValue;\n }\n return oldValue;\n };\n setStateValue(updateFunction);\n } else {\n if (!isControlled) {\n setStateValue(value);\n }\n onChangeCaller(value, ...args);\n }\n }, [isControlled, onChange]);\n\n // If a controlled component's value prop changes, we need to update stateRef\n if (isControlled) {\n stateRef.current = value;\n } else {\n value = stateValue;\n }\n\n return [value, setValue];\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\n/**\n * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.\n */\nexport function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {\n let newValue = Math.min(Math.max(value, min), max);\n return newValue;\n}\n\nexport function snapValueToStep(value: number, min: number, max: number, step: number): number {\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder;\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor((max - min) / step) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(max / step) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n let string = step.toString();\n let index = string.indexOf('.');\n let precision = index >= 0 ? string.length - index : 0;\n\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n snappedValue = Math.round(snappedValue * pow) / pow;\n }\n\n return snappedValue;\n}\n\n/* Takes a value and rounds off to the number of digits. */\nexport function toFixedNumber(value: number, digits: number, base: number = 10): number {\n const pow = Math.pow(base, digits);\n\n return Math.round(value * pow) / pow;\n}\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACF,CAAC;IAC3C,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,eAAQ,CAAC,MAAK,IAAI,YAAY;IAChE,GAAG,CAAC,GAAG,GAAG,aAAM,CAAC,MAAK,KAAK,SAAS;IACpC,GAAG,CAAC,aAAa,GAAG,GAAG,CAAC,OAAO;IAC/B,GAAG,CAAC,YAAY,GAAG,MAAK,KAAK,SAAS;IACtC,EAA2C,AAA3C,yCAA2C;IAC3C,GAAG,CAAC,QAAQ,GAAG,aAAM,CAAC,UAAU;IAChC,EAAE,EAAE,aAAa,KAAK,YAAY,EAChC,OAAO,CAAC,IAAI,EAAE,+BAA+B,EAAE,aAAa,GAAG,CAAY,cAAG,CAAc,cAAC,IAAI,EAAE,YAAY,GA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AAI,CAAC,QAAQ;IAC1B,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,CAAG;IAC9B,GAAG,CAAC,SAAS,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,KAAK,GAAG,CAAC;IAEtD,EAAE,EAAE,SAAS,GAAG,CAAC,EAAE,CAAC;QAClB,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS;QAChC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,GAAG,IAAI,GAAG;IACrD,CAAC;IAED,MAAM,CAAC,YAAY;AACrB,CAAC;SAGe,wCAAa,CAAC,KAAa,EAAE,MAAc,EAAE,IAAY,GAAG,EAAE,EAAU,CAAC;IACvF,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM;IAEjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,IAAI,GAAG;AACtC,CAAC;;","sources":["packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/useControlledState.ts","packages/@react-stately/utils/src/number.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './useControlledState';\nexport * from './number';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useRef, useState} from 'react';\n\nexport function useControlledState<T>(\n value: T,\n defaultValue: T,\n onChange: (value: T, ...args: any[]) => void\n): [T, (value: T, ...args: any[]) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n let ref = useRef(value !== undefined);\n let wasControlled = ref.current;\n let isControlled = value !== undefined;\n // Internal state reference for useCallback\n let stateRef = useRef(stateValue);\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n\n ref.current = isControlled;\n\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(stateRef.current, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n stateRef.current = value;\n }\n };\n\n if (typeof value === 'function') {\n console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');\n // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates\n // when someone using useControlledState calls setControlledState(myFunc)\n // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc\n // if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning\n // otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same\n let updateFunction = (oldValue, ...functionArgs) => {\n let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);\n onChangeCaller(interceptedValue, ...args);\n if (!isControlled) {\n return interceptedValue;\n }\n return oldValue;\n };\n setStateValue(updateFunction);\n } else {\n if (!isControlled) {\n setStateValue(value);\n }\n onChangeCaller(value, ...args);\n }\n }, [isControlled, onChange]);\n\n // If a controlled component's value prop changes, we need to update stateRef\n if (isControlled) {\n stateRef.current = value;\n } else {\n value = stateValue;\n }\n\n return [value, setValue];\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\n/**\n * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.\n */\nexport function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {\n let newValue = Math.min(Math.max(value, min), max);\n return newValue;\n}\n\nexport function snapValueToStep(value: number, min: number, max: number, step: number): number {\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder;\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor((max - min) / step) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(max / step) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n let string = step.toString();\n let index = string.indexOf('.');\n let precision = index >= 0 ? string.length - index : 0;\n\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n snappedValue = Math.round(snappedValue * pow) / pow;\n }\n\n return snappedValue;\n}\n\n/* Takes a value and rounds off to the number of digits. */\nexport function toFixedNumber(value: number, digits: number, base: number = 10): number {\n const pow = Math.pow(base, digits);\n\n return Math.round(value * pow) / pow;\n}\n"],"names":[],"version":3,"file":"module.js.map"}
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export function useControlledState<T>(value: T, defaultValue: T, onChange: (value: T, ...args: any[]) => void): [T, (value: T | ((prevState: T) => T), ...args: any[]) => void];
1
+ export function useControlledState<T>(value: T, defaultValue: T, onChange: (value: T, ...args: any[]) => void): [T, (value: T, ...args: any[]) => void];
2
2
  /**
3
3
  * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
4
4
  */
@@ -1 +1 @@
1
- {"mappings":"AAcA,mCAAmC,CAAC,EAClC,KAAK,EAAE,CAAC,EACR,YAAY,EAAE,CAAC,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAC3C,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,CAwDjE;AC9DD;;GAEG;AACH,sBAAsB,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAkB,EAAE,GAAG,GAAE,MAAiB,GAAG,MAAM,CAG5F;AAED,gCAAgC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA2B7F;AAGD,8BAA8B,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,MAAW,GAAG,MAAM,CAItF","sources":["packages/@react-stately/utils/src/packages/@react-stately/utils/src/useControlledState.ts","packages/@react-stately/utils/src/packages/@react-stately/utils/src/number.ts","packages/@react-stately/utils/src/packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/index.ts"],"sourcesContent":[null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './useControlledState';\nexport * from './number';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":"AAcA,mCAAmC,CAAC,EAClC,KAAK,EAAE,CAAC,EACR,YAAY,EAAE,CAAC,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAC3C,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,CAyDzC;AC/DD;;GAEG;AACH,sBAAsB,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAkB,EAAE,GAAG,GAAE,MAAiB,GAAG,MAAM,CAG5F;AAED,gCAAgC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA2B7F;AAGD,8BAA8B,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,MAAW,GAAG,MAAM,CAItF","sources":["packages/@react-stately/utils/src/packages/@react-stately/utils/src/useControlledState.ts","packages/@react-stately/utils/src/packages/@react-stately/utils/src/number.ts","packages/@react-stately/utils/src/packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/index.ts"],"sourcesContent":[null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './useControlledState';\nexport * from './number';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/utils",
3
- "version": "3.0.0-nightly.1373+5115b76b6",
3
+ "version": "3.0.0-nightly.1379+3366ae36b",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -25,5 +25,5 @@
25
25
  "publishConfig": {
26
26
  "access": "public"
27
27
  },
28
- "gitHead": "5115b76b63cba6843d5e7de2ef5ec3a41a1bff3c"
28
+ "gitHead": "3366ae36b3ddefbf67bfd084caaf58bb89ed345b"
29
29
  }
@@ -16,7 +16,7 @@ export function useControlledState<T>(
16
16
  value: T,
17
17
  defaultValue: T,
18
18
  onChange: (value: T, ...args: any[]) => void
19
- ): [T, (value: T | ((prevState: T) => T), ...args: any[]) => void] {
19
+ ): [T, (value: T, ...args: any[]) => void] {
20
20
  let [stateValue, setStateValue] = useState(value || defaultValue);
21
21
  let ref = useRef(value !== undefined);
22
22
  let wasControlled = ref.current;
@@ -42,6 +42,7 @@ export function useControlledState<T>(
42
42
  };
43
43
 
44
44
  if (typeof value === 'function') {
45
+ console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');
45
46
  // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
46
47
  // when someone using useControlledState calls setControlledState(myFunc)
47
48
  // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc