g-confirm.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <uni-popup ref="popup" type="dialog" :class="`popup-${options.mode}`">
  3. <uni-popup-dialog
  4. :type="options.type"
  5. :mode="options.mode"
  6. :maxlength="options.maxlength"
  7. :showClose="options.showClose"
  8. :placeholder="options.placeholder"
  9. :cancelText="options.cancelText"
  10. :confirmText="options.confirmText"
  11. :title="options.title"
  12. :content="options.content"
  13. :before-close="options.beforeClose"
  14. v-model="options.value"
  15. @confirm="handleConfirm"
  16. @close="handleCancel"
  17. ></uni-popup-dialog>
  18. </uni-popup>
  19. </template>
  20. <script setup lang="ts">
  21. import { cloneDeep } from "lodash";
  22. import { ref } from "vue";
  23. const OPTIONS: ConfirmOptions = {
  24. type: "success",
  25. cancelText: "取消",
  26. confirmText: "确定",
  27. title: "提示",
  28. content: "提示信息",
  29. beforeClose: false,
  30. mode: "base",
  31. maxlength: 20,
  32. showClose: true,
  33. placeholder: "20字以内",
  34. value: "",
  35. key: "default",
  36. };
  37. const popup = ref<PopupInstance | null>(null);
  38. const options = ref<ConfirmOptions>(cloneDeep(OPTIONS));
  39. function show(params: Partial<ConfirmOptions>) {
  40. options.value = cloneDeep(OPTIONS);
  41. updateObject(options.value, params);
  42. popup.value?.open();
  43. }
  44. function close() {
  45. popup.value?.close();
  46. }
  47. function updateObject<T>(target: T, updates: Partial<T>): void {
  48. Object.keys(updates).forEach((key) => {
  49. const typedKey = key as keyof T;
  50. const value = updates[typedKey];
  51. if (Object.prototype.hasOwnProperty.call(target, typedKey)) {
  52. target[typedKey] = value!;
  53. }
  54. });
  55. }
  56. const emit = defineEmits<{
  57. (e: "handleConfirm", key: string, inputValue: string): void;
  58. (e: "handleCancel"): void;
  59. }>();
  60. function handleConfirm() {
  61. emit("handleConfirm", options.value.key, options.value.value.trim());
  62. }
  63. function handleCancel() {
  64. emit("handleCancel");
  65. }
  66. defineExpose({
  67. show,
  68. close,
  69. });
  70. </script>
  71. <style lang="scss" scoped>
  72. ::v-deep {
  73. .uni-popup-dialog {
  74. background-color: var(--theme-bg-color-deep);
  75. color: var(--theme-primary-color);
  76. .uni-dialog-content {
  77. color: var(--theme-primary-color-light-1);
  78. }
  79. .uni-dialog-title-text {
  80. color: var(--theme-primary-color);
  81. }
  82. .uni-dialog-input {
  83. background-color: var(--theme-bg-color);
  84. border: unset;
  85. }
  86. .uni-dialog-button-group {
  87. border: unset;
  88. }
  89. .uni-dialog-button {
  90. height: 100rpx;
  91. border-radius: 50rpx;
  92. line-height: 100rpx;
  93. text-align: center;
  94. background-color: #fae0da;
  95. border: unset;
  96. margin: 0 24rpx 24rpx;
  97. .uni-dialog-button-text {
  98. color: #d44842;
  99. }
  100. }
  101. .uni-border-left {
  102. background-color: #d44842;
  103. .uni-dialog-button-text {
  104. color: #fef8f2;
  105. }
  106. }
  107. }
  108. }
  109. </style>