index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { useEvent, useMemo, useState } from 'functional-mini/component';
  2. import { mountComponent } from '../../_util/component';
  3. import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
  4. import { useComponentUpdateEffect } from '../../_util/hooks/useLayoutEffect';
  5. import { useMixState } from '../../_util/hooks/useMixState';
  6. import { resolveEventValues } from '../../_util/platform';
  7. import { CascaderFunctionalProps } from './props';
  8. import { defaultFormat, getterColumns, getValidValue } from './utils';
  9. var CascaderPicker = function (props) {
  10. var _a = useMixState(props.defaultValue, {
  11. value: props.value,
  12. }), realValue = _a[0], _b = _a[1], isRealValueControlled = _b.isControlled, updateRealValue = _b.update;
  13. var _c = useState(function () {
  14. var value = props.value || props.defaultValue || [];
  15. var columns = getterColumns(props.value || props.defaultValue, props.options);
  16. return { columns: columns, value: value };
  17. }), _d = _c[0], value = _d.value, columns = _d.columns, setState = _c[1];
  18. var _e = useComponentEvent(props), triggerEventOnly = _e.triggerEventOnly, triggerEventValues = _e.triggerEventValues, triggerEvent = _e.triggerEvent;
  19. useComponentUpdateEffect(function () {
  20. var newColumns = getterColumns(props.value, props.options);
  21. var value = getValidValue(props.value, newColumns);
  22. setState({ value: value, columns: newColumns });
  23. }, [
  24. props.value,
  25. props.options,
  26. /**
  27. * 这里不要删
  28. *
  29. * 1. picker 触发 onOk
  30. * 2. 更新 realValue
  31. * 3. picker 触发 onFormat (此时 realValue 未更新)
  32. * 4. 依赖里的 realValue 更新
  33. * 5. 触发组件再次渲染
  34. * 6. 此时 onFormat 读取到最新的realValue
  35. */
  36. realValue,
  37. ]);
  38. function getOptionByValue(value, options) {
  39. var _a;
  40. if (!((value === null || value === void 0 ? void 0 : value.length) > 0))
  41. return null;
  42. var result = [];
  43. var item = options.find(function (v) { return v.value === value[0]; });
  44. var _loop_1 = function (i) {
  45. if (!item) {
  46. return { value: null };
  47. }
  48. result.push({
  49. value: item.value,
  50. label: item.label,
  51. });
  52. item = (_a = item.children) === null || _a === void 0 ? void 0 : _a.find(function (v) { return v.value === value[i + 1]; });
  53. };
  54. for (var i = 0; i < value.length; i++) {
  55. var state_1 = _loop_1(i);
  56. if (typeof state_1 === "object")
  57. return state_1.value;
  58. }
  59. return result;
  60. }
  61. useEvent('onCancel', function (e) {
  62. triggerEventOnly('cancel', e);
  63. });
  64. var formattedValueText = useMemo(function () {
  65. var onFormat = props.onFormat;
  66. if (typeof onFormat === 'function') {
  67. var formatValueByProps = onFormat(realValue, getOptionByValue(realValue, props.options));
  68. if (typeof formatValueByProps !== 'undefined') {
  69. return formatValueByProps;
  70. }
  71. }
  72. return defaultFormat(realValue, getOptionByValue(realValue, props.options));
  73. }, [realValue]);
  74. useEvent('onVisibleChange', function (visible) {
  75. if (visible) {
  76. var newColumns = getterColumns(realValue, props.options);
  77. var currentValue = getValidValue(realValue, newColumns);
  78. setState({ value: currentValue, columns: newColumns });
  79. }
  80. triggerEvent('visibleChange', visible);
  81. });
  82. useEvent('onOk', function () {
  83. // 完成时再次校验value,避免visible状态下props无效
  84. var validValue = getValidValue(value, columns);
  85. if (!isRealValueControlled) {
  86. updateRealValue(validValue);
  87. }
  88. triggerEventValues('ok', [
  89. validValue,
  90. getOptionByValue(validValue, props.options),
  91. ]);
  92. });
  93. useEvent('onChange', function (event) {
  94. var selectedValue = resolveEventValues(event)[0];
  95. var newColumns = getterColumns(selectedValue, props.options);
  96. var value = getValidValue(selectedValue, newColumns);
  97. setState({ value: value, columns: newColumns });
  98. triggerEventValues('change', [
  99. selectedValue,
  100. getOptionByValue(selectedValue, props.options),
  101. ]);
  102. });
  103. return {
  104. formattedValueText: formattedValueText,
  105. currentValue: value,
  106. columns: columns,
  107. };
  108. };
  109. mountComponent(CascaderPicker, CascaderFunctionalProps);