PickerColumn.js 4.2 KB

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