index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view :class="classes" @click="handleClick">
  3. <!-- <i class="nut-icon-loading" v-if="loading"></i> -->
  4. <!-- <i :class="icon" v-if="icon && !loading"></i> -->
  5. <slot></slot>
  6. </view>
  7. </template>
  8. <script lang="ts">
  9. import { PropType, CSSProperties, toRefs, computed } from 'vue';
  10. import { createComponent } from '@/utils/create';
  11. const { componentName, create } = createComponent('button');
  12. export type ButtonType = 'default' | 'primary' | 'info' | 'success' | 'warning' | 'danger';
  13. export type ButtonSize = 'large' | 'normal' | 'small';
  14. export type ButtonShape = 'square' | 'round';
  15. export default create({
  16. props: {
  17. color: String,
  18. shape: {
  19. type: String as PropType<ButtonShape>,
  20. default: 'round'
  21. },
  22. plain: {
  23. type: Boolean,
  24. default: false
  25. },
  26. loading: {
  27. type: Boolean,
  28. default: false
  29. },
  30. disabled: {
  31. type: Boolean,
  32. default: false
  33. },
  34. type: {
  35. type: String as PropType<ButtonType>,
  36. default: 'default'
  37. },
  38. size: {
  39. type: String as PropType<ButtonSize>,
  40. default: 'normal'
  41. },
  42. block: {
  43. type: Boolean,
  44. default: false
  45. }
  46. },
  47. components: {},
  48. emits: ['click'],
  49. setup(props, { emit, slots }) {
  50. // setup内部只能访问这4个属性,值得注意的是props必须在上面声明才能在这里取到
  51. console.log('props', props, 'emit', emit, 'slots', slots);
  52. const { type, size, shape, disabled, loading, color, plain, block } = toRefs(props);
  53. // console.log("type", type, "size", size);
  54. const handleClick = (event: MouseEvent) => {
  55. if (!loading.value && !disabled.value) {
  56. emit('click', event);
  57. }
  58. };
  59. const classes = computed(() => {
  60. const prefixCls = componentName;
  61. return {
  62. [componentName]: true,
  63. [`${prefixCls}--${type.value}`]: type.value,
  64. [`${prefixCls}--${size.value}`]: size.value,
  65. [`${prefixCls}--${shape.value}`]: shape.value,
  66. [`${prefixCls}--plain`]: plain.value,
  67. [`${prefixCls}--block`]: block.value,
  68. [`${prefixCls}--disabled`]: disabled.value
  69. };
  70. });
  71. const getStyle = computed(() => {
  72. if (color?.value) {
  73. const style: CSSProperties = {};
  74. if (plain.value) {
  75. style.color = color.value;
  76. style.background = '#fff';
  77. if (!color.value?.includes('gradient')) {
  78. style.borderColor = color.value;
  79. }
  80. } else {
  81. style.color = '#fff';
  82. style.background = color.value;
  83. }
  84. return style;
  85. }
  86. });
  87. return {
  88. handleClick,
  89. disabled,
  90. classes,
  91. getStyle
  92. };
  93. }
  94. });
  95. </script>
  96. <style lang="scss">
  97. @import 'index.scss';
  98. </style>