uni-button-x.uvue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <view class="btn" :class="btnClass" @click="onClick">
  3. <text class="btn-text" :class="textClass">{{ loading ? loadingText : text }}</text>
  4. </view>
  5. </template>
  6. <script setup lang="uts">
  7. type ButtonType = 'primary' | 'default' | 'danger'
  8. const props = defineProps({
  9. text: {
  10. type: String,
  11. default: '按钮'
  12. },
  13. loadingText: {
  14. type: String,
  15. default: '加载中...'
  16. },
  17. loading: {
  18. type: Boolean,
  19. default: false
  20. },
  21. disabled: {
  22. type: Boolean,
  23. default: false
  24. },
  25. type: {
  26. type: String,
  27. default: 'primary'
  28. }
  29. })
  30. const emit = defineEmits(['click'])
  31. const btnClass = computed(() : string => {
  32. if (props.disabled || props.loading) {
  33. return 'btn-disabled'
  34. }
  35. if (props.type == 'danger') {
  36. return 'btn-danger'
  37. }
  38. if (props.type == 'default') {
  39. return 'btn-default'
  40. }
  41. return 'btn-primary'
  42. })
  43. const textClass = computed(() : string => {
  44. if (props.type == 'default') {
  45. return 'btn-text-default'
  46. }
  47. return 'btn-text-light'
  48. })
  49. function onClick() : void {
  50. if (props.disabled || props.loading) {
  51. return
  52. }
  53. emit('click')
  54. }
  55. </script>
  56. <style>
  57. .btn {
  58. display: flex;
  59. flex-direction: row;
  60. align-items: center;
  61. justify-content: center;
  62. height: 44px;
  63. padding-left: 16px;
  64. padding-right: 16px;
  65. border-radius: 8px;
  66. }
  67. .btn-primary {
  68. background-color: #007aff;
  69. }
  70. .btn-danger {
  71. background-color: #ff3b30;
  72. }
  73. .btn-default {
  74. background-color: #f2f2f2;
  75. }
  76. .btn-disabled {
  77. background-color: #c7c7cc;
  78. }
  79. .btn-text {
  80. font-size: 16px;
  81. }
  82. .btn-text-light {
  83. color: #ffffff;
  84. }
  85. .btn-text-default {
  86. color: #333333;
  87. }
  88. </style>