hooks.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { useEvent as useStableCallback } from '../../_util/hooks/useEvent';
  2. import dayjs from 'dayjs';
  3. import { useCallback } from 'functional-mini/component';
  4. export var useFormatValue = function (props) {
  5. function defaultFormat(date, valueStrs) {
  6. var format = props.format, splitCharacter = props.splitCharacter;
  7. if (format && valueStrs && valueStrs[0] && valueStrs[1]) {
  8. return valueStrs.join("".concat(splitCharacter));
  9. }
  10. return '';
  11. }
  12. var onFormat = useStableCallback(function (realValue) {
  13. var onFormat = props.onFormat, format = props.format;
  14. var formatValueByProps = onFormat &&
  15. onFormat(realValue, realValue
  16. ? realValue.map(function (v) { return (v ? dayjs(v).format(format) : null); })
  17. : null);
  18. if (typeof formatValueByProps !== 'undefined') {
  19. return formatValueByProps;
  20. }
  21. return defaultFormat(realValue, realValue
  22. ? realValue.map(function (v) { return (v ? dayjs(v).format(format) : null); })
  23. : null);
  24. });
  25. return onFormat;
  26. };
  27. export function useMinAndMax() {
  28. var getMin = useStableCallback(function (min) {
  29. return min ? dayjs(min) : dayjs().subtract(10, 'year');
  30. });
  31. var getMax = useStableCallback(function (max) {
  32. return max ? dayjs(max) : dayjs().add(10, 'year');
  33. });
  34. return {
  35. getMin: getMin,
  36. getMax: getMax,
  37. };
  38. }
  39. function defaultFormatLabel(type, value) {
  40. var suffixMap = {
  41. year: '年',
  42. month: '月',
  43. day: '日',
  44. hour: '时',
  45. minute: '分',
  46. second: '秒',
  47. };
  48. return "".concat(value).concat(suffixMap[type]);
  49. }
  50. export var useFormatLabel = function (onFormatLabel) {
  51. return useCallback(function (type, value) {
  52. if (typeof onFormatLabel === 'function') {
  53. var formatValueByProps = onFormatLabel(type, value);
  54. if (typeof formatValueByProps !== 'undefined') {
  55. return String(formatValueByProps);
  56. }
  57. }
  58. return defaultFormatLabel(type, value);
  59. }, [onFormatLabel]);
  60. };