index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import { fieldNamesProps } from '../helpers/mixins/fieldNamesBehavior'
  4. baseComponent({
  5. externalClasses: ['wux-scroll-view-class'],
  6. properties: {
  7. prefixCls: {
  8. type: String,
  9. value: 'wux-cascader',
  10. },
  11. defaultValue: {
  12. type: Array,
  13. value: [],
  14. },
  15. value: {
  16. type: Array,
  17. value: [],
  18. observer(newVal) {
  19. if (this.data.controlled) {
  20. this.setActiveValue(newVal)
  21. this.setInnerValue(newVal)
  22. }
  23. },
  24. },
  25. controlled: {
  26. type: Boolean,
  27. value: false,
  28. },
  29. title: {
  30. type: String,
  31. value: '',
  32. },
  33. cancelText: {
  34. type: String,
  35. value: '取消',
  36. },
  37. confirmText: {
  38. type: String,
  39. value: '确定',
  40. },
  41. options: {
  42. type: Array,
  43. value: [],
  44. },
  45. full: {
  46. type: Boolean,
  47. value: false,
  48. },
  49. height: {
  50. type: [String, Number],
  51. value: 'auto',
  52. },
  53. chooseTitle: {
  54. type: String,
  55. value: '请选择',
  56. },
  57. visible: {
  58. type: Boolean,
  59. value: false,
  60. observer(shouldRender) {
  61. if (shouldRender) {
  62. this.setShouldRender(true)
  63. }
  64. },
  65. },
  66. skipAnimation: {
  67. type: Boolean,
  68. value: false,
  69. },
  70. ...fieldNamesProps,
  71. },
  72. data: {
  73. shouldRender: false,
  74. innerValue: [],
  75. activeValue: [],
  76. },
  77. computed: {
  78. classes: ['prefixCls', function(prefixCls) {
  79. const wrap = classNames(prefixCls)
  80. const hd = `${prefixCls}__hd`
  81. const bd = `${prefixCls}__bd`
  82. const toolbar = `${prefixCls}__toolbar`
  83. const inner = `${prefixCls}__inner`
  84. const cancel = classNames(`${prefixCls}__button`, {
  85. [`${prefixCls}__button--cancel`]: true,
  86. })
  87. const confirm = classNames(`${prefixCls}__button`, {
  88. [`${prefixCls}__button--confirm`]: true,
  89. })
  90. const hover = `${prefixCls}__button--hover`
  91. const title = `${prefixCls}__title`
  92. return {
  93. wrap,
  94. hd,
  95. bd,
  96. toolbar,
  97. inner,
  98. cancel,
  99. confirm,
  100. hover,
  101. title,
  102. }
  103. }],
  104. },
  105. methods: {
  106. setShouldRender(shouldRender) {
  107. if (this.data.shouldRender !== shouldRender) {
  108. this.setData({
  109. shouldRender,
  110. })
  111. }
  112. },
  113. setActiveValue(activeValue, forceTrigger) {
  114. if (this.data.activeValue !== activeValue || forceTrigger) {
  115. this.setData({
  116. activeValue,
  117. })
  118. }
  119. },
  120. setInnerValue(innerValue) {
  121. if (this.data.innerValue !== innerValue) {
  122. this.setData({
  123. innerValue,
  124. })
  125. }
  126. },
  127. getValue(value = this.data.activeValue) {
  128. this.cascaderView = this.cascaderView || this.querySelector('#wux-cascader-view')
  129. return this.cascaderView && this.cascaderView.getValue(value)
  130. },
  131. /**
  132. * 切换面板的回调
  133. */
  134. onTabsChange(e) {
  135. this.triggerEvent('tabsChange', e.detail)
  136. },
  137. /**
  138. * 叶子节点加载的回调
  139. */
  140. onLoadOptions(e) {
  141. this.triggerEvent('load', e.detail)
  142. },
  143. /**
  144. * 选项改变时触发
  145. */
  146. onChange(e) {
  147. const { visible } = this.data
  148. const props = e.detail
  149. const { value: innerValue } = props
  150. this.setInnerValue(innerValue)
  151. if (visible) {
  152. this.triggerEvent('change', props)
  153. }
  154. },
  155. /**
  156. * 组件关闭时的回调函数
  157. */
  158. close() {
  159. this.triggerEvent('close')
  160. },
  161. /**
  162. * 组件关闭时重置其内部数据
  163. */
  164. onClosed() {
  165. const { activeValue: innerValue } = this.data
  166. this.setInnerValue(innerValue)
  167. this.setShouldRender(false)
  168. },
  169. /**
  170. * 点击确定按钮时的回调函数
  171. */
  172. onConfirm() {
  173. const { innerValue: activeValue } = this.data
  174. if (!this.data.controlled) {
  175. this.setActiveValue(activeValue, true)
  176. }
  177. this.triggerEvent('confirm', { ...this.getValue(activeValue) })
  178. this.close()
  179. },
  180. /**
  181. * 点击取消按钮时的回调函数
  182. */
  183. onCancel() {
  184. this.triggerEvent('cancel', { ...this.getValue() })
  185. this.close()
  186. },
  187. /**
  188. * 阻止移动触摸
  189. */
  190. noop() {},
  191. },
  192. attached() {
  193. const { defaultValue, value, controlled, visible: shouldRender } = this.data
  194. const activeValue = controlled ? value : defaultValue
  195. this.setActiveValue(activeValue)
  196. this.setInnerValue(activeValue)
  197. this.setShouldRender(shouldRender)
  198. },
  199. })