index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. const defaults = {
  4. prefixCls: 'wux-dialog',
  5. title: '',
  6. content: '',
  7. buttons: [],
  8. verticalButtons: !1,
  9. resetOnClose: false,
  10. closable: false,
  11. mask: true,
  12. maskClosable: true,
  13. zIndex: 1000,
  14. }
  15. const defaultOptions = {
  16. onCancel() {},
  17. cancelText: '取消',
  18. cancelType: 'default',
  19. onConfirm() {},
  20. confirmText: '确定',
  21. confirmType: 'primary',
  22. }
  23. baseComponent({
  24. useFunc: true,
  25. data: defaults,
  26. computed: {
  27. classes() {
  28. const { prefixCls, buttons: btns, verticalButtons } = this.data
  29. const prompt = `${prefixCls}__prompt`
  30. const input = `${prefixCls}__input`
  31. const buttons = classNames(`${prefixCls}__buttons`, {
  32. [`${prefixCls}__buttons--${verticalButtons ? 'vertical' : 'horizontal'}`]: true,
  33. })
  34. const button = btns.map((button) => {
  35. const wrap = classNames(`${prefixCls}__button`, {
  36. [`${prefixCls}__button--${button.type || 'default'}`]: button.type || 'default',
  37. [`${prefixCls}__button--bold`]: button.bold,
  38. [`${button.className}`]: button.className,
  39. })
  40. const hover = button.hoverClass && button.hoverClass !== 'default' ? button.hoverClass : `${prefixCls}__button--hover`
  41. return {
  42. wrap,
  43. hover,
  44. }
  45. })
  46. return {
  47. prompt,
  48. input,
  49. buttons,
  50. button,
  51. }
  52. },
  53. },
  54. methods: {
  55. /**
  56. * 组件关闭时重置其内部数据
  57. */
  58. onClosed() {
  59. if (this.data.resetOnClose) {
  60. const params = {
  61. ...this.$$mergeOptionsToData(defaults),
  62. prompt: null,
  63. }
  64. this.$$setData(params)
  65. }
  66. },
  67. /**
  68. * 点击 x 或 mask 回调
  69. */
  70. onClose() {
  71. this.hide()
  72. },
  73. /**
  74. * 隐藏
  75. */
  76. hide(cb) {
  77. this.$$setData({ in: false })
  78. if (typeof cb === 'function') {
  79. cb.call(this)
  80. }
  81. },
  82. /**
  83. * 显示
  84. */
  85. show(opts = {}) {
  86. const options = this.$$mergeOptionsAndBindMethods(Object.assign({}, defaults, opts))
  87. this.$$setData({ in: true, ...options })
  88. return this.hide.bind(this)
  89. },
  90. /**
  91. * 按钮点击事件
  92. */
  93. buttonTapped(e) {
  94. const { index } = e.currentTarget.dataset
  95. const button = this.data.buttons[index]
  96. this.hide(() => typeof button.onTap === 'function' && button.onTap(e))
  97. },
  98. /**
  99. * 当键盘输入时,触发 input 事件
  100. */
  101. bindinput(e) {
  102. this.$$setData({
  103. 'prompt.response': e.detail.value,
  104. })
  105. },
  106. /**
  107. * 显示dialog组件
  108. * @param {Object} opts 配置项
  109. * @param {String} opts.title 提示标题
  110. * @param {String} opts.content 提示文本
  111. * @param {Boolean} opts.verticalButtons 是否显示垂直按钮布局
  112. * @param {Array} opts.buttons 按钮
  113. * @param {String} opts.buttons.text 按钮的文字
  114. * @param {String} opts.buttons.type 按钮的类型
  115. * @param {Boolean} opts.buttons.bold 是否加粗按钮的文字
  116. * @param {Function} opts.buttons.onTap 按钮的点击事件
  117. */
  118. open(opts = {}) {
  119. return this.show(opts)
  120. },
  121. /**
  122. * 显示dialog组件
  123. * @param {Object} opts 配置项
  124. * @param {String} opts.title 提示标题
  125. * @param {String} opts.content 提示文本
  126. * @param {String} opts.confirmText 确定按钮的文字,默认为"确定"
  127. * @param {String} opts.confirmType 确定按钮的类型
  128. * @param {Function} opts.onConfirm 确定按钮的点击事件
  129. */
  130. alert(opts = {}) {
  131. return this.open(Object.assign({
  132. buttons: [{
  133. text: opts.confirmText || defaultOptions.confirmText,
  134. type: opts.confirmType || defaultOptions.confirmType,
  135. onTap: (e) => {
  136. typeof opts.onConfirm === 'function' && opts.onConfirm(e)
  137. },
  138. }, ],
  139. }, opts))
  140. },
  141. /**
  142. * 显示dialog组件
  143. * @param {Object} opts 配置项
  144. * @param {String} opts.title 提示标题
  145. * @param {String} opts.content 提示文本
  146. * @param {String} opts.confirmText 确定按钮的文字,默认为"确定"
  147. * @param {String} opts.confirmType 确定按钮的类型
  148. * @param {Function} opts.onConfirm 确定按钮的点击事件
  149. * @param {String} opts.cancelText 取消按钮的文字,默认为"取消"
  150. * @param {String} opts.cancelType 取消按钮的类型
  151. * @param {Function} opts.onCancel 取消按钮的点击事件
  152. */
  153. confirm(opts = {}) {
  154. return this.open(Object.assign({
  155. buttons: [{
  156. text: opts.cancelText || defaultOptions.cancelText,
  157. type: opts.cancelType || defaultOptions.cancelType,
  158. onTap: (e) => {
  159. typeof opts.onCancel === 'function' && opts.onCancel(e)
  160. },
  161. },
  162. {
  163. text: opts.confirmText || defaultOptions.confirmText,
  164. type: opts.confirmType || defaultOptions.confirmType,
  165. onTap: (e) => {
  166. typeof opts.onConfirm === 'function' && opts.onConfirm(e)
  167. },
  168. },
  169. ],
  170. }, opts))
  171. },
  172. /**
  173. * 显示dialog组件
  174. * @param {Object} opts 配置项
  175. * @param {String} opts.title 提示标题
  176. * @param {String} opts.content 提示文本
  177. * @param {String} opts.fieldtype input 的类型,有效值:text, number, idcard, digit
  178. * @param {Boolean} opts.password 是否是密码类型
  179. * @param {String} opts.defaultText 默认值
  180. * @param {String} opts.placeholder 输入框为空时占位符
  181. * @param {Number} opts.maxlength 最大输入长度,设置为 -1 的时候不限制最大长度
  182. * @param {String} opts.confirmText 确定按钮的文字,默认为"确定"
  183. * @param {String} opts.confirmType 确定按钮的类型
  184. * @param {Function} opts.onConfirm 确定按钮的点击事件
  185. * @param {String} opts.cancelText 取消按钮的文字,默认为"取消"
  186. * @param {String} opts.cancelType 取消按钮的类型
  187. * @param {Function} opts.onCancel 取消按钮的点击事件
  188. */
  189. prompt(opts = {}) {
  190. const prompt = {
  191. fieldtype: opts.fieldtype ? opts.fieldtype : 'text',
  192. password: !!opts.password,
  193. response: opts.defaultText ? opts.defaultText : '',
  194. placeholder: opts.placeholder ? opts.placeholder : '',
  195. maxlength: opts.maxlength ? parseInt(opts.maxlength) : '',
  196. }
  197. return this.open(Object.assign({
  198. prompt: prompt,
  199. buttons: [{
  200. text: opts.cancelText || defaultOptions.cancelText,
  201. type: opts.cancelType || defaultOptions.cancelType,
  202. onTap: (e) => {
  203. typeof opts.onCancel === 'function' && opts.onCancel(e)
  204. },
  205. },
  206. {
  207. text: opts.confirmText || defaultOptions.confirmText,
  208. type: opts.confirmType || defaultOptions.confirmType,
  209. onTap: (e) => {
  210. typeof opts.onConfirm === 'function' && opts.onConfirm(e, this.data.prompt.response)
  211. },
  212. },
  213. ],
  214. }, opts))
  215. },
  216. },
  217. })