useCount.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _typeof from "@babel/runtime/helpers/esm/typeof";
  4. var _excluded = ["show"];
  5. import * as React from 'react';
  6. /**
  7. * Cut `value` by the `count.max` prop.
  8. */
  9. export function inCountRange(value, countConfig) {
  10. if (!countConfig.max) {
  11. return true;
  12. }
  13. var count = countConfig.strategy(value);
  14. return count <= countConfig.max;
  15. }
  16. export default function useCount(count, showCount) {
  17. return React.useMemo(function () {
  18. var mergedConfig = {};
  19. if (showCount) {
  20. mergedConfig.show = _typeof(showCount) === 'object' && showCount.formatter ? showCount.formatter : !!showCount;
  21. }
  22. mergedConfig = _objectSpread(_objectSpread({}, mergedConfig), count);
  23. var _ref = mergedConfig,
  24. show = _ref.show,
  25. rest = _objectWithoutProperties(_ref, _excluded);
  26. return _objectSpread(_objectSpread({}, rest), {}, {
  27. show: !!show,
  28. showFormatter: typeof show === 'function' ? show : undefined,
  29. strategy: rest.strategy || function (value) {
  30. return value.length;
  31. }
  32. });
  33. }, [count, showCount]);
  34. }