index.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Dialog } from 'vant'
  2. import { defineComponent, PropType, reactive, watch } from 'vue'
  3. import styles from './index.module.less'
  4. export default defineComponent({
  5. name: 'o-dialog',
  6. props: {
  7. show: {
  8. type: Boolean,
  9. default: false
  10. },
  11. message: {
  12. type: String,
  13. default: ''
  14. },
  15. title: {
  16. type: String,
  17. default: ''
  18. },
  19. confirmButtonText: {
  20. type: String,
  21. default: '确认'
  22. },
  23. allowHtml: {
  24. type: Boolean,
  25. default: false
  26. },
  27. cancelButtonText: {
  28. type: String,
  29. default: '取消'
  30. },
  31. showConfirmButton: {
  32. type: Boolean,
  33. default: true
  34. },
  35. showCancelButton: {
  36. type: Boolean,
  37. default: false
  38. },
  39. messageAlign: {
  40. type: String as PropType<'left' | 'center' | 'right'>,
  41. default: 'center'
  42. },
  43. dialogMarginTop: {
  44. type: String,
  45. default: '0px'
  46. }
  47. },
  48. emits: ['cancel', 'confirm', 'update:show'],
  49. setup(props, { emit, slots }) {
  50. const state = reactive({
  51. show: props.show || false
  52. })
  53. // 监听状态
  54. watch(
  55. () => props.show,
  56. () => {
  57. state.show = props.show
  58. }
  59. )
  60. return () => (
  61. <Dialog
  62. class={styles.oDialog}
  63. style={{
  64. marginTop: props.dialogMarginTop
  65. }}
  66. v-model:show={state.show}
  67. message={props.message}
  68. allowHtml={props.allowHtml}
  69. messageAlign={props.messageAlign}
  70. confirmButtonText={props.confirmButtonText}
  71. cancelButtonText={props.cancelButtonText}
  72. showConfirmButton={props.showConfirmButton}
  73. showCancelButton={props.showCancelButton}
  74. onConfirm={() => {
  75. emit('update:show', false)
  76. emit('confirm')
  77. }}
  78. onCancel={() => {
  79. emit('update:show', false)
  80. emit('cancel')
  81. }}
  82. >
  83. {{
  84. title: () =>
  85. props.title && (
  86. <div class={styles.dialogTitle}>
  87. <i></i>
  88. {props.title}
  89. </div>
  90. )
  91. }}
  92. </Dialog>
  93. )
  94. }
  95. })