TimeUnitColumn.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { createVNode as _createVNode } from "vue";
  2. import { scrollTo, waitElementReady } from '../../utils/uiUtil';
  3. import { useInjectPanel } from '../../PanelContext';
  4. import classNames from '../../../_util/classNames';
  5. import { ref, onBeforeUnmount, watch, defineComponent, nextTick, shallowRef } from 'vue';
  6. export default defineComponent({
  7. name: 'TimeUnitColumn',
  8. props: ['prefixCls', 'units', 'onSelect', 'value', 'active', 'hideDisabledOptions'],
  9. setup(props) {
  10. const {
  11. open
  12. } = useInjectPanel();
  13. const ulRef = shallowRef(null);
  14. const liRefs = ref(new Map());
  15. const scrollRef = ref();
  16. watch(() => props.value, () => {
  17. const li = liRefs.value.get(props.value);
  18. if (li && open.value !== false) {
  19. scrollTo(ulRef.value, li.offsetTop, 120);
  20. }
  21. });
  22. onBeforeUnmount(() => {
  23. var _a;
  24. (_a = scrollRef.value) === null || _a === void 0 ? void 0 : _a.call(scrollRef);
  25. });
  26. watch(open, () => {
  27. var _a;
  28. (_a = scrollRef.value) === null || _a === void 0 ? void 0 : _a.call(scrollRef);
  29. nextTick(() => {
  30. if (open.value) {
  31. const li = liRefs.value.get(props.value);
  32. if (li) {
  33. scrollRef.value = waitElementReady(li, () => {
  34. scrollTo(ulRef.value, li.offsetTop, 0);
  35. });
  36. }
  37. }
  38. });
  39. }, {
  40. immediate: true,
  41. flush: 'post'
  42. });
  43. return () => {
  44. const {
  45. prefixCls,
  46. units,
  47. onSelect,
  48. value,
  49. active,
  50. hideDisabledOptions
  51. } = props;
  52. const cellPrefixCls = `${prefixCls}-cell`;
  53. return _createVNode("ul", {
  54. "class": classNames(`${prefixCls}-column`, {
  55. [`${prefixCls}-column-active`]: active
  56. }),
  57. "ref": ulRef,
  58. "style": {
  59. position: 'relative'
  60. }
  61. }, [units.map(unit => {
  62. if (hideDisabledOptions && unit.disabled) {
  63. return null;
  64. }
  65. return _createVNode("li", {
  66. "key": unit.value,
  67. "ref": element => {
  68. liRefs.value.set(unit.value, element);
  69. },
  70. "class": classNames(cellPrefixCls, {
  71. [`${cellPrefixCls}-disabled`]: unit.disabled,
  72. [`${cellPrefixCls}-selected`]: value === unit.value
  73. }),
  74. "onClick": () => {
  75. if (unit.disabled) {
  76. return;
  77. }
  78. onSelect(unit.value);
  79. }
  80. }, [_createVNode("div", {
  81. "class": `${cellPrefixCls}-inner`
  82. }, [unit.label])]);
  83. })]);
  84. };
  85. }
  86. });