index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. cancelButtonText: {
  24. type: String,
  25. default: '取消'
  26. },
  27. showConfirmButton: {
  28. type: Boolean,
  29. default: true
  30. },
  31. showCancelButton: {
  32. type: Boolean,
  33. default: false
  34. },
  35. messageAlign: {
  36. type: String as PropType<'left' | 'center' | 'right'>,
  37. default: 'center'
  38. },
  39. dialogMarginTop: {
  40. type: String,
  41. default: '0px'
  42. }
  43. },
  44. emits: ['cancel', 'confirm', 'update:show'],
  45. setup(props, { emit }) {
  46. const state = reactive({
  47. show: props.show || false
  48. })
  49. // 监听状态
  50. watch(
  51. () => props.show,
  52. () => {
  53. state.show = props.show
  54. }
  55. )
  56. return () => (
  57. <Dialog
  58. class={styles.oDialog}
  59. style={{
  60. marginTop: props.dialogMarginTop
  61. }}
  62. v-model:show={state.show}
  63. message={props.message}
  64. messageAlign={props.messageAlign}
  65. confirmButtonText={props.confirmButtonText}
  66. cancelButtonText={props.cancelButtonText}
  67. showConfirmButton={props.showConfirmButton}
  68. showCancelButton={props.showCancelButton}
  69. onConfirm={() => {
  70. emit('update:show', false)
  71. emit('confirm')
  72. }}
  73. onCancel={() => {
  74. emit('update:show', false)
  75. emit('cancel')
  76. }}
  77. >
  78. {{
  79. title: () => (
  80. <div class={styles.dialogTitle}>
  81. <i></i>
  82. {props.title}
  83. </div>
  84. )
  85. }}
  86. </Dialog>
  87. )
  88. }
  89. })