PickerColumn.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _deepClone = require("../utils/deep-clone");
  5. var _utils = require("../utils");
  6. var _event = require("../utils/event");
  7. var DEFAULT_DURATION = 200;
  8. var _use = (0, _utils.use)('picker-column'),
  9. sfc = _use[0],
  10. bem = _use[1];
  11. var _default = sfc({
  12. props: {
  13. valueKey: String,
  14. className: String,
  15. itemHeight: Number,
  16. defaultIndex: Number,
  17. initialOptions: Array,
  18. visibleItemCount: Number
  19. },
  20. data: function data() {
  21. return {
  22. startY: 0,
  23. offset: 0,
  24. duration: 0,
  25. startOffset: 0,
  26. options: (0, _deepClone.deepClone)(this.initialOptions),
  27. currentIndex: this.defaultIndex
  28. };
  29. },
  30. created: function created() {
  31. this.$parent.children && this.$parent.children.push(this);
  32. this.setIndex(this.currentIndex);
  33. },
  34. destroyed: function destroyed() {
  35. var children = this.$parent.children;
  36. children && children.splice(children.indexOf(this), 1);
  37. },
  38. watch: {
  39. defaultIndex: function defaultIndex() {
  40. this.setIndex(this.defaultIndex);
  41. }
  42. },
  43. computed: {
  44. count: function count() {
  45. return this.options.length;
  46. }
  47. },
  48. methods: {
  49. onTouchStart: function onTouchStart(event) {
  50. this.startY = event.touches[0].clientY;
  51. this.startOffset = this.offset;
  52. this.duration = 0;
  53. },
  54. onTouchMove: function onTouchMove(event) {
  55. (0, _event.preventDefault)(event);
  56. var deltaY = event.touches[0].clientY - this.startY;
  57. this.offset = (0, _utils.range)(this.startOffset + deltaY, -(this.count * this.itemHeight), this.itemHeight);
  58. },
  59. onTouchEnd: function onTouchEnd() {
  60. if (this.offset !== this.startOffset) {
  61. this.duration = DEFAULT_DURATION;
  62. var index = (0, _utils.range)(Math.round(-this.offset / this.itemHeight), 0, this.count - 1);
  63. this.setIndex(index, true);
  64. }
  65. },
  66. adjustIndex: function adjustIndex(index) {
  67. index = (0, _utils.range)(index, 0, this.count);
  68. for (var i = index; i < this.count; i++) {
  69. if (!this.isDisabled(this.options[i])) return i;
  70. }
  71. for (var _i = index - 1; _i >= 0; _i--) {
  72. if (!this.isDisabled(this.options[_i])) return _i;
  73. }
  74. },
  75. isDisabled: function isDisabled(option) {
  76. return (0, _utils.isObj)(option) && option.disabled;
  77. },
  78. getOptionText: function getOptionText(option) {
  79. return (0, _utils.isObj)(option) && this.valueKey in option ? option[this.valueKey] : option;
  80. },
  81. setIndex: function setIndex(index, userAction) {
  82. index = this.adjustIndex(index) || 0;
  83. this.offset = -index * this.itemHeight;
  84. if (index !== this.currentIndex) {
  85. this.currentIndex = index;
  86. userAction && this.$emit('change', index);
  87. }
  88. },
  89. setValue: function setValue(value) {
  90. var options = this.options;
  91. for (var i = 0; i < options.length; i++) {
  92. if (this.getOptionText(options[i]) === value) {
  93. return this.setIndex(i);
  94. }
  95. }
  96. },
  97. getValue: function getValue() {
  98. return this.options[this.currentIndex];
  99. }
  100. },
  101. render: function render(h) {
  102. var _this = this;
  103. var itemHeight = this.itemHeight,
  104. visibleItemCount = this.visibleItemCount;
  105. var columnStyle = {
  106. height: itemHeight * visibleItemCount + 'px'
  107. };
  108. var baseOffset = itemHeight * (visibleItemCount - 1) / 2;
  109. var wrapperStyle = {
  110. transition: this.duration + "ms",
  111. transform: "translate3d(0, " + (this.offset + baseOffset) + "px, 0)",
  112. lineHeight: itemHeight + "px"
  113. };
  114. var optionStyle = {
  115. height: itemHeight + "px"
  116. };
  117. return h("div", {
  118. "style": columnStyle,
  119. "class": [bem(), this.className],
  120. "on": {
  121. "touchstart": this.onTouchStart,
  122. "touchmove": this.onTouchMove,
  123. "touchend": this.onTouchEnd,
  124. "touchcancel": this.onTouchEnd
  125. }
  126. }, [h("ul", {
  127. "style": wrapperStyle
  128. }, [this.options.map(function (option, index) {
  129. return h("li", {
  130. "style": optionStyle,
  131. "class": ['van-ellipsis', bem('item', {
  132. disabled: _this.isDisabled(option),
  133. selected: index === _this.currentIndex
  134. })],
  135. "domProps": {
  136. "innerHTML": _this.getOptionText(option)
  137. },
  138. "on": {
  139. "click": function click() {
  140. _this.setIndex(index, true);
  141. }
  142. }
  143. });
  144. })])]);
  145. }
  146. });
  147. exports["default"] = _default;